如果要显示实时视频流或任何内容流(如视频或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 方法调整其alpha属性.它的语法在下面和下面给出;
myTexture.setAlpha(1.0f); myTexture.setRotation(90.0f);
除了这些方法之外,TextureView类还有其他方法可用.它们列在下面和下面;
Sr.No | 方法&描述 |
---|---|
1 | getSurfaceTexture () 此方法返回此视图使用的SurfaceTexture. |
2 | getBitmap(int width,int height) 此方法返回返回相关曲面纹理内容的位图表示. |
3 | getTransform(Matrix transform) 此方法返回与此纹理视图关联的变换. |
4 | isOpaque() 此方法指示此视图是否不透明. |
5 | lockCanvas() 此方法开始编辑曲面中的像素 |
6 | setOpaque(boolean opaque) 此方法表示r这个TextureView的内容是不透明的. |
7 | setTransform(Matrix transform) 此方法将变换设置为与此纹理视图关联. |
8 | unlockCanvasAndPost(Canvas canvas) 此方法完成编辑曲面中的像素. |
下面的示例演示了TextureView类的用法.它包含一个基本应用程序,允许您在纹理视图中查看相机并更改其角度,方向等.
要试验此示例,您需要在实际的设备上运行它相机存在.
步骤 | 描述 |
---|---|
1 | 您将使用android studio IDE创建Android应用程序在com.example.textureview包下将其命名为TextureView. |
2 | 修改src/MainActivity.java文件以添加活动代码. |
3 | 修改布局XML文件res/layout/activity_main.xml如果需要,添加任何GUI组件. |
5 | 运行应用程序并选择正在运行的Android设备并在其上安装应用程序并验证结果. |
以下是 src/c的内容om.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>
[object Object]
选择您的移动设备作为选项,然后检查您的移动设备,该设备将显示以下屏幕.此屏幕的alpha属性设置为 0.5 ,旋转设置为 45.
此屏幕的alpha属性设置为 1.5 ,旋转设置为 45 .
此屏幕的alpha属性设置为 1.0 和旋转设置为 90 .