Implement indicator on tap to focus in camera [and

2019-06-08 11:58发布

问题:

I have my own camera android app which performs pinch zoom and tap to focus. In tap to focus i need to draw a rectangle or circle indicator when i touch the screen and redraw wherever i touched.One more thing i want to change the color if auto focus has done.

So far i have done to draw rectangle on screen at center but i couldn't redraw it if i touched somewhere on the screen.

Any idea or suggestion to implement it would be thankful

My codes are

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.rotate(ui_rotation, canvas.getWidth() / 2, canvas.getHeight() / 2);

    final float scale = getResources().getDisplayMetrics().density;
    int text_y = (int) (20 * scale + 0.5f); // convert dps to pixels
    // fine tuning to adjust placement of text with respect to the GUI, depending on orientation
    int text_extra_offset_y = 0;
    if (ui_rotation == 0) {
        text_extra_offset_y = (int) (0.5 * text_y);
    } else if (ui_rotation == 180) {
        text_extra_offset_y = (int) (2.5 * text_y);
    } else if (ui_rotation == 90 || ui_rotation == 270) {
        text_extra_offset_y = -(int) (0.5 * text_y);
    }


    if (previewCamera != null) {
        if (this.isOnTimer()) {
            long remaining_time = (take_photo_time - System.currentTimeMillis() + 999) / 1000;
            if (remaining_time >= 0) {
                p.setColor(Color.YELLOW);
                p.setTextSize(42 * scale + 0.5f); // convert dps to pixels
                p.setTextAlign(Paint.Align.CENTER);
                canvas.drawText("" + remaining_time, canvas.getWidth() / 2, canvas.getHeight() / 2, p);
            }
        }
    } else if (previewCamera == null) {

        p.setColor(Color.YELLOW);
        p.setTextSize(14 * scale + 0.5f); // convert dps to pixels
        p.setTextAlign(Paint.Align.CENTER);
        int pixels_offset = (int) (20 * scale + 0.5f); // convert dps to pixels
    }
    if (this.has_zoom && previewCamera != null) {
        float zoom_ratio = this.zoom_ratios.get(zoom_factor) / 100.0f;
        // only show when actually zoomed in
        if (zoom_ratio > 1.0f + 1.0e-5f) {
            // Convert the dps to pixels, based on density scale
            int pixels_offset_y = 2 * text_y + text_extra_offset_y;
            p.setColor(Color.WHITE);
            p.setTextSize(14 * scale + 0.5f); // convert dps to pixels
            p.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("Zoom: " + zoom_ratio + "x", canvas.getWidth() / 2, canvas.getHeight() - pixels_offset_y, p);
        }
    }
    if (previewCamera != null) {
        int pixels_offset_y = 1 * text_y + text_extra_offset_y;
        p.setColor(Color.WHITE);
        p.setTextSize(14 * scale + 0.5f); // convert dps to pixels
        p.setTextAlign(Paint.Align.CENTER);
        long time_now = System.currentTimeMillis();
    }

    canvas.restore();

    if (this.focus_success == FOCUS_DONE) {
        int size = (int) (50 * scale + 0.5f); // convert dps to pixels
        if (this.focus_success == FOCUS_SUCCESS)
            p.setColor(Color.GREEN);
        else if (this.focus_success == FOCUS_FAILED)
            p.setColor(Color.RED);
        else
            p.setColor(Color.GREEN);
        p.setStyle(Paint.Style.STROKE);
        int pos_x = 0;
        int pos_y = 0;
        if (has_focus_area) {
            pos_x = focus_screen_x;
            pos_y = focus_screen_y;
        } else {
            pos_x = canvas.getWidth() / 2;
            pos_y = canvas.getHeight() / 2;
        }
        canvas.drawRect(pos_x - size, pos_y - size, pos_x + size, pos_y + size, p);
        if (focus_complete_time != -1 && System.currentTimeMillis() > focus_complete_time + 1000) {
            focus_success = FOCUS_DONE;
        }
    }
}

onDraw() called properly in SurfaceCreated()

回答1:

If the requirement is something like show something similar to recording button then,On your surface view add a button show and hide as and when it's required