• Android Video Tutorials

Android - TextureView



如果您想顯示即時影片流或任何內容流,例如影片或 OpenGL 場景,您可以使用 Android 提供的 TextureView 來實現。

為了使用 TextureView,您需要做的就是獲取其 SurfaceTexture。然後可以使用 SurfaceTexture 來渲染內容。為此,您只需例項化此類的物件並實現 SurfaceTextureListener 介面即可。其語法如下所示:

private TextureView myTexture;
public class MainActivity extends Activity implements SurfaceTextureListener{
   protected void onCreate(Bundle savedInstanceState) {
      myTexture = new TextureView(this);
      myTexture.setSurfaceTextureListener(this);
      setContentView(myTexture);
   }
}

之後,您需要做的是覆蓋其方法。這些方法列出如下:

@Override
public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1, int arg2) {
}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,int arg2) {
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
}

在 TextureView 中顯示的任何檢視都可以透過使用 setAlphasetRotation 方法進行旋轉,並調整其 alpha 屬性。其語法如下所示:

myTexture.setAlpha(1.0f);
myTexture.setRotation(90.0f);

除了這些方法之外,TextureView 類中還提供了其他方法。它們列出如下:

序號 方法及描述
1

getSurfaceTexture()

此方法返回此檢視使用的 SurfaceTexture。

2

getBitmap(int width, int height)

此方法返回關聯的表面紋理內容的 Bitmap 表示形式。

3

getTransform(Matrix transform)

此方法返回與此紋理檢視關聯的變換。

4

isOpaque()

此方法指示此 View 是否不透明。

5

lockCanvas()

此方法開始編輯表面中的畫素。

6

setOpaque(boolean opaque)

此方法指示此 TextureView 的內容是否不透明。

7

setTransform(Matrix transform)

此方法設定與該紋理檢視關聯的變換。

8

unlockCanvasAndPost(Canvas canvas)

此方法完成編輯表面中的畫素。

示例

以下示例演示了 TextureView 類的用法。它建立一個基本的應用程式,允許您在 TextureView 中檢視相機並更改其角度、方向等。

要嘗試此示例,您需要在存在攝像頭的實際裝置上執行它。

步驟 描述
1 您將使用 Android Studio IDE 建立一個 Android 應用程式,並將其命名為 TextureView,位於包 com.example.textureview 下。
2 修改 src/MainActivity.java 檔案以新增 Activity 程式碼。
3 修改佈局 XML 檔案 res/layout/activity_main.xml,如果需要新增任何 GUI 元件。
5 執行應用程式並選擇一個正在執行的 Android 裝置,將應用程式安裝在其上並驗證結果。

以下是 src/com.example.textureview/MainActivity.java 的內容。

package com.example.textureview;

import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;

import android.view.Gravity;
import android.view.Menu;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends Activity implements SurfaceTextureListener {
   private TextureView myTexture;
   private Camera mCamera;

   @SuppressLint("NewApi")
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      myTexture = new TextureView(this);
      myTexture.setSurfaceTextureListener(this);
      setContentView(myTexture);
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
   
   @SuppressLint("NewApi")
   @Override
   public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1, int arg2) {
      mCamera = Camera.open();
      Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
      
      myTexture.setLayoutParams(new FrameLayout.LayoutParams(
      previewSize.width, previewSize.height, Gravity.CENTER));
      
      try {
         mCamera.setPreviewTexture(arg0);
      } catch (IOException t) {
      }
		
      mCamera.startPreview();
      myTexture.setAlpha(1.0f);
      myTexture.setRotation(90.0f);
   }

   @Override
   public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
      mCamera.stopPreview();
      mCamera.release();
      return true;
   }

   @Override
   public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
   int arg2) {
      // TODO Auto-generated method stub
   }
	
   @Override
   public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
      // TODO Auto-generated method stub
   }
}

以下是 activity_main.xml 的內容。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

   <TextureView
      android:id="@+id/textureView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
</RelativeLayout>

以下是 AndroidManifest.xml 的預設內容。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.textureview" >

   <uses-permission android:name="android.permission.CAMERA"/>
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity 
         android:name="com.example.textureview.MainActivity"
         android:label="@string/app_name" >
         
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
       </activity>
       
   </application>
</manifest>

讓我們嘗試執行您的 TextureView 應用程式。我假設您已將您的實際 Android 移動裝置連線到您的計算機。要從 Android Studio 執行該應用程式,請開啟您的專案中的某個 Activity 檔案,然後單擊工具欄中的執行 Eclipse Run Icon 圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗以選擇您要執行 Android 應用程式的位置。

Anroid TextureView Tutorial

選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示以下螢幕。此螢幕的 alpha 屬性設定為 0.5,旋轉設定為 45

Anroid TextureView Tutorial

此螢幕的 alpha 屬性設定為 1.5,旋轉設定為 45

Anroid TextureView Tutorial

此螢幕的 alpha 屬性設定為 1.0,旋轉設定為 90

Anroid TextureView Tutorial
廣告

© . All rights reserved.