Android Camera(二)

亡梦爱人 提交于 2019-11-30 06:49:10

Android Camera一文简单介绍了如何在自己的App里实时显示Camera,但有时我们的App不只是要显示图像,可能还要添加其他信息,如在指定位置画Rectangle等等。

 stackoverflow中有人问到如何在camera的SurfaceView中画矩形,最后,提问者根据别人的 建议给出了如下答案:

新建一个与Camera重叠的SurfaceView,然后将该SurfaceView设置透明背景,然后就可以绘制你想绘制的代码图形了。

我测试了下,发现,新建View也是可以的。

layout如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="320dp"
        android:layout_height="240dp" />
    <view.TestView
        android:id="@+id/TransparentView"
        android:layout_width="320dp"
        android:layout_height="240dp" />
</RelativeLayout>

TestView位于包view下:

public class TestView extends View {
    public TestView(Context ct){
        super(ct);
    }

    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint pt = new Paint();
        pt.setColor(Color.BLUE);
        pt.setTextSize(80);
        canvas.drawColor(Color.alpha(0));
        canvas.drawText("Hello world",100,100,pt);
        invalidate();
    }
}

最后效果图

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!