轻按即可实现指示器以将焦点对准相机[android] [英] Implement indicator on tap to focus in camera [android]

查看:80
本文介绍了轻按即可实现指示器以将焦点对准相机[android]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我自己的相机android应用,该应用执行捏变焦并点按以进行聚焦.在点击聚焦时,我需要在触摸屏幕时绘制一个矩形或圆形指示器,然后在任何触摸位置重新绘制.如果自动聚焦完成,我还想更改颜色.

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

我的代码是

@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()在SurfaceCreated()中正确调用

onDraw() called properly in SurfaceCreated()

推荐答案

如果要求类似于显示与录制按钮类似的东西,则在您的表面视图上按需显示和隐藏按钮

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

这篇关于轻按即可实现指示器以将焦点对准相机[android]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆