Android-textureview

提供:Dev Guides
移動先:案内検索

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) {
}

テクスチャビューに表示されるビューはすべて回転でき、そのアルファプロパティは setAlpha および setRotation メソッドを使用して調整できます。 その構文は以下のとおりです-

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

これらのメソッドとは別に、TextureViewクラスで使用できる他のメソッドがあります。 それらは以下にリストされています-

Sr.No Method & description
1

getSurfaceTexture()

このメソッドは、このビューで使用されるSurfaceTextureを返します。

2

getBitmap(int width, int height)

このメソッドは、関連付けられた表面テクスチャのコンテンツのビットマップ表現を返します。

3

getTransform(Matrix transform)

このメソッドは、このテクスチャビューに関連付けられた変換を返します。

4

isOpaque()

このメソッドは、このビューが不透明かどうかを示します。

5

lockCanvas()

このメソッドは、表面のピクセルの編集を開始します

6

setOpaque(boolean opaque)

このメソッドは、このTextureViewのコンテンツが不透明かどうかを示します。

7

setTransform(Matrix transform)

このメソッドは、このテクスチャビューに関連付ける変換を設定します。

8

unlockCanvasAndPost(Canvas canvas)

このメソッドは、表面のピクセルの編集を終了します。

以下の例は、TextureViewクラスの使用法を示しています。 テクスチャビュー内でカメラを表示し、角度、向きなどを変更できる基本的なアプリケーションを作成します。

この例を試すには、カメラが存在する実際のデバイスでこれを実行する必要があります。

Steps Description
1 You will use android studio IDE to create an Android application and name it as TextureView under a package com.example.textureview.
2 Modify src/MainActivity.java file to add Activity code.
3 Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
5 Run the application and choose a running android device and install the application on it and verify the results.
*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スタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 アプリケーションを開始する前に、Androidスタジオは次のウィンドウを表示して、Androidアプリケーションを実行するオプションを選択します。

Anroid TextureViewチュートリアル

オプションとしてモバイルデバイスを選択し、次の画面を表示するモバイルデバイスを確認します。 この画面には、アルファプロパティが 0.5 に設定され、回転が 45. に設定されています。

Android TextureViewチュートリアル

この画面には、 1.5 に設定されたアルファプロパティと 45 に設定された回転があります。

Android TextureViewチュートリアル

この画面には、 1.0 に設定されたalphaプロパティと 90 に設定された回転があります。

Android TextureViewチュートリアル