Android Camera2 Touch-to-Focus

Man Camera2 APIs are hard to master...

I've scanned SO for a way to get touch-to-focus to work on Android and could not find a solution that works.
These were very partial, or provided only scaffold code which I couldn't use:

Finally I was able to do it by myself this way:


  • ovmeygat

    what is mPolygonDrawerView ?

  • royshikron

    It's leftover from other code. I'll remove. Thanks!

  • Mikhail P.

    It is not entirely correct to do this:

    final int y = (int)((motionEvent.getX() / (float)view.getWidth()) * (float)sensorArraySize.height());

    final int x = (int)((motionEvent.getY() / (float)view.getHeight()) * (float)sensorArraySize.width());

    The problem is that aspect ratio of the sensor might not be equal to the aspect ratio of the texture view used for camera preview. What you see on the preview may correspond to just a part of the camera sensor, so in general case you need a more accurate mapping from texture view coordinates to active array coordinates.

    This link might be helpful:

  • Swastik Devs

    if i keep the repeat off then the preview gets stuck
    and if i keep it continous then it keeps on focusing and de focusing