实现指示器点击聚焦在相机[android]

时间:2015-10-09 05:46:14

标签: android android-camera autofocus

我有自己的相机Android应用程序,执行捏缩放和点击聚焦。点按即可聚焦我需要在触摸屏幕时绘制矩形或圆形指示,并在我触摸的任何地方重绘。如果自动对焦完成,我还想更改颜色。

到目前为止,我已经完成了在中心的屏幕上绘制矩形,但如果我触摸屏幕上的某个地方,我无法重绘它。

实施它的任何想法或建议都会很感激

我的代码是

@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;
        }
    }
}
在SurfaceCreated()

中正确调用了

onDraw()

1 个答案:

答案 0 :(得分:0)

如果要求类似于显示类似于录制按钮的要求,则在表面视图上添加按钮显示并在需要时隐藏