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