Android Camera(二)

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

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

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

我测试了下,发现,新建View也是能够的。ide

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下:spa

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

最后效果图.net

相关文章
相关标签/搜索