簡體   English   中英

Android:想要在相機預覽上添加輪廓疊加層

[英]Android: Want to put an silhouette overlay on a Camera preview

我想知道如何在相機預覽上放置一個輪廓。 到目前為止,我已經有以下示例工作,它只是預覽相機。

http://developer.android.com/reference/android/view/TextureView.html

我正在嘗試進行相機預覽,我在其中顯示了一個輪廓,以便使用應用程序的人知道應該拍攝照片的位置,然后點擊一個按鈕,拍攝照片,當然沒有剪影在圖片里。

如何才能做到這一點? 我似乎找不到任何關於如何在紋理視圖上放置疊加層的示例。

使用您鏈接的TextureView文檔中的示例,我們只需創建一個布局XML文件來保存我們的TextureView並在FrameLayout疊加ImageView 然后,我們使用此布局的資源ID調用ActivitysetContentView()方法,而不是示例中動態創建的TextureView

布局文件main.xml

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

    <TextureView android:id="@+id/texture_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00000000"
        android:src="@drawable/ic_launcher" />

</FrameLayout>

和示例Activity

public class LiveCameraActivity extends Activity
    implements TextureView.SurfaceTextureListener {

    private Camera mCamera;
    private TextureView mTextureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mTextureView = (TextureView) findViewById(R.id.texture_view);
        mTextureView.setSurfaceTextureListener(this);
    }

    // The TextureView.SurfaceTextureListener methods
    // are the same as shown in the example.

    ...
}

布局中的疊加ImageView使用默認啟動器圖標作為其源圖像,因為它具有透明度,至少在我的IDE上。 您稍后會用您的輪廓替換此圖像。 TextureView之后列出的ImageView將出現在它的頂部,因為View是按照它們在布局中列出的相同順序繪制的,其中第一個View列在底部; 即,在z軸上離你最遠。

Activity ,我們只是將布局加載為內容View ,並獲取對在TextureView創建的TextureView的引用。 其他一切都是一樣的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM