如果你想显示一段在线视频或者任意的数据流比如视频或者OpenGL 场景,你可以用android中的TextureView做到。

TextureView的兄弟SurfaceView

应用程序的视频或者opengl内容往往是显示在一个特别的UI控件中:SurfaceView。SurfaceView的工作方式是创建一个置于应用窗口之后的新窗口。这种方式的效率非常高,因为SurfaceView窗口刷新的时候不需要重绘应用程序的窗口(android普通窗口的视图绘制机制是一层一层的,任何一个子元素或者是局部的刷新都会导致整个视图结构全部重绘一次,因此效率非常低下,不过满足普通应用界面的需求还是绰绰有余),但是SurfaceView也有一些非常不便的限制。

因为SurfaceView的内容不在应用窗口上,所以不能使用变换(平移、缩放、旋转等)。也难以放在ListView或者ScrollView中,不能使用UI控件的一些特性比如View.setAlpha()

为了解决这个问题 Android 4.0中引入了TextureView。

TextureView

与SurfaceView相比,TextureView并没有创建一个单独的Surface用来绘制,这使得它可以像一般的View一样执行一些变换操作,设置透明度等。另外,Textureview必须在硬件加速开启的窗口中。

TextureView的使用非常简单,你唯一要做的就是获取用于渲染内容的SurfaceTexture。具体做法是先创建TextureView对象,然后实现SurfaceTextureListener接口,代码如下:

  1. private TextureView myTexture;
  2. public class MainActivity extends Activity implements SurfaceTextureListener{
  3. protected void onCreate(Bundle savedInstanceState) {
  4. myTexture = new TextureView(this);
  5. myTexture.setSurfaceTextureListener(this);
  6. setContentView(myTexture);
  7. }
  8. }

Activity implementsSurfaceTextureListener接口因此activity中需要重写如下方法:

  1. @Override
  2. public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1, int arg2) {
  3. }
  4. @Override
  5. public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
  6. }
  7. @Override
  8. public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,int arg2) {
  9. }
  10. @Override
  11. public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
  12. }

TextureView可以使用setAlphasetRotation方法达到改变透明度和旋转的效果。

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

除了上面的方法之外,TextureView 还有如下方法:

序号 方法&描述
1 getSurfaceTexture()
This method returns the SurfaceTexture used by this view.
2 getBitmap(int width, int height)
This method returns Returns a Bitmap representation of the content of the associated surface texture.
3 getTransform(Matrix transform)
This method returns the transform associated with this texture view.
4 isOpaque()
This method indicates whether this View is opaque.
5 lockCanvas()
This method start editing the pixels in the surface
6 setOpaque(boolean opaque)
This method indicates whether the content of this TextureView is opaque.
7 setTransform(Matrix transform)
This method sets the transform to associate with this texture view.
8 unlockCanvasAndPost(Canvas canvas)
This method finish editing pixels in the surface.

例子

下面的例子演示了如何使用TextureView类,我们创建了一个可以在TextureView中预览Camera的demo,可以改变它的角度以及方向。当然程序需要运行在有摄像头的设备上。

下面是MainActivity.java中的代码:

  1. package com.example.textureview;
  2. import java.io.IOException;
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.graphics.SurfaceTexture;
  6. import android.hardware.Camera;
  7. import android.os.Bundle;
  8. import android.view.Gravity;
  9. import android.view.Menu;
  10. import android.view.TextureView;
  11. import android.view.TextureView.SurfaceTextureListener;
  12. import android.view.View;
  13. import android.widget.FrameLayout;
  14. public class MainActivity extends Activity implements SurfaceTextureListener {
  15. private TextureView myTexture;
  16. private Camera mCamera;
  17. @SuppressLint("NewApi")
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. myTexture = new TextureView(this);
  23. myTexture.setSurfaceTextureListener(this);
  24. setContentView(myTexture);
  25. }
  26. @Override
  27. public boolean onCreateOptionsMenu(Menu menu) {
  28. // Inflate the menu; this adds items to the action bar if it is present.
  29. getMenuInflater().inflate(R.menu.main, menu);
  30. return true;
  31. }
  32. @SuppressLint("NewApi")
  33. @Override
  34. public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
  35. int arg2) {
  36. mCamera = Camera.open();
  37. Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
  38. myTexture.setLayoutParams(new FrameLayout.LayoutParams(
  39. previewSize.width, previewSize.height, Gravity.CENTER));
  40. try {
  41. mCamera.setPreviewTexture(arg0);
  42. } catch (IOException t) {
  43. }
  44. mCamera.startPreview();
  45. myTexture.setAlpha(1.0f);
  46. myTexture.setRotation(90.0f);
  47. }
  48. @Override
  49. public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
  50. mCamera.stopPreview();
  51. mCamera.release();
  52. return true;
  53. }
  54. @Override
  55. public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
  56. int arg2) {
  57. // TODO Auto-generated method stub
  58. }
  59. @Override
  60. public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
  61. // TODO Auto-generated method stub
  62. }
  63. }

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10. <TextureView
  11. android:id="@+id/textureView1"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:layout_alignParentTop="true"
  15. android:layout_centerHorizontal="true" />
  16. </RelativeLayout>

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.textureview"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="17" />
  9. <uses-permission android:name="android.permission.CAMERA"/>
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/AppTheme" >
  15. <activity
  16. android:name="com.example.textureview.MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. </application>
  24. </manifest>

不同参数下的截图:

myTexture.setAlpha(0.5f);

myTexture.setRotation(45.0f);

myTexture.setAlpha(1.5f);

myTexture.setRotation(45.0f);

myTexture.setAlpha(1.0f);

myTexture.setRotation(90.0f);

Android TextureView简易教程的更多相关文章

  1. Android开发简易教程

    Android开发简易教程 Android 开发因为涉及到代码编辑.UI 布局.打包等工序,有一款好用的IDE非常重要.Google 最早提供了基于 Eclipse 的 ADT 作为开发工具,后来在2 ...

  2. Android实战简易教程-第三十九枪(第三方短信验证平台Mob和验证码自己主动填入功能结合实例)

    用户注冊或者找回password时通常会用到短信验证功能.这里我们使用第三方的短信平台进行验证实例. 我们用到第三方短信验证平台是Mob,地址为:http://mob.com/ 一.注冊用户.获取SD ...

  3. Android实战简易教程-第四十枪(窃听风云之短信监听)

    近期在做监听验证码短信自己主动填入的功能,无意间想到了一个短信监听的办法. 免责声明:短信监听本身是一种违法行为,这里仅仅是技术描写叙述.请大家学习技术就可以.(哈哈) 本实例是基于bmob提供的后台 ...

  4. Android实战简易教程-第十三枪(五大布局研究)

    我们知道Android系统应用程序通常是由多个Activity组成,而这些Activity以视图的形式展如今我们面前, 视图都是由一个一个的组件构成的. 组件就是我们常见的Button.TextEdi ...

  5. Android实战简易教程-第九枪(BitmapFactory.Options对资源图片进行缩放)

    我们知道,我们编写的应用程序都是有一定内存限制的.程序占用了过高的内存就easy出现OOM(OutOfMemory)异常.因此在展示高分辨率图片的时候,最好先将图片进行压缩,压缩后的图片大小应该和用来 ...

  6. Android实战简易教程-第三十四枪(基于ViewPager和FragmentPagerAdapter实现滑动通用Tab)

    上一段时间写过一篇文章<基于ViewPager实现微信页面切换效果> 里面实现了相似微信Tab的页面.可是这样的实现方法有个问题.就是以后全部的代码逻辑都必须在MainActivity中实 ...

  7. Android实战简易教程-第二十八枪(基于Bmob实现头像图片设置和网络上传功能!)

    上一篇我们介绍了怎样由uri转换成String ,本文就用到了上篇文章的方法.以下我们介绍一下怎样设置头像后将头像图片上传到云端的方法,本文基于Bmob提供的服务. 看一下代码:(布局文件和前两篇文章 ...

  8. Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)

    接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...

  9. Android实战简易教程-第十枪(画廊组件Gallery有用研究)

    Gallery组件用于拖拽浏览图片,以下我们就来看一下怎样实现. 一.实现Gallery 1.布局文件非常easy: <?xml version="1.0" encoding ...

随机推荐

  1. maven使用技巧

    转:MAVEN常用命令 Maven库: http://repo2.maven.org/maven2/ Maven依赖查询: http://mvnrepository.com/ Maven常用命令: 1 ...

  2. Redit集群搭建-Sentinel模式搭建

    Redit集群搭建 学习了: Windows:http://blog.csdn.net/mrxiagc/article/details/52799081 Linux:https://www.cnblo ...

  3. hdu5282 最长公共子序列的变形

    pid=5282">http://acm.hdu.edu.cn/showproblem.php?pid=5282 Problem Description Xuejiejie loves ...

  4. Lists and tuples

    zip is a built-in function that takes two or more sequence and ‘zips’ them into a list of tuples, wh ...

  5. Copying lists

    When you assign an object to a variable, Python copies the reference to the object. In this case a a ...

  6. linux中不同颜色的文件代表什么不同的类型

    linux 文件颜色的含义,蓝色代表目录,绿色代表可执行文件,红色表示压缩文件,浅蓝色表示链接文件,灰色表示其他文件,红色闪烁表示链接的文件有问题了,黄色表示设备文件.蓝色文件----------目. ...

  7. 使用Chrome浏览器,让我们远离(所有)广告

    你是否还在为浏览网页时各种广告霸屏而急躁不安?这里分享一个小技巧,如何自动屏蔽各大广告. 这里使用的浏览器是Chrome,直接在Chrome网上应用商店搜索下载安装AdBlock插件(不知道其它浏览器 ...

  8. RecordAccumulator 1

    介绍 前面讲过producer会将数据保存在RecordAccumulator中,并通过Sender发送数据.RecordAccumulator 就相当于一个队列保存着那些准备发送到server的数据 ...

  9. CentOS7下安装二进制MYSQL8

    早看到MySQL8发布, 性能相比MySQL7提升2倍,今天准备安装下试试看 1.先卸载当前系统中已安装的mariadb rpm -qa | grep mariadb rpm -e mysql*/ma ...

  10. CF949C Data Center Maintenance(建图+强联通分量)

    题意 有 n 个信息中心,第 i 个信息中心要在第 ti 个小时维护,维护期间信息不能被获得. 每个用户的数据都有两份备份,第 i 个用户的数据放在信息中心 c(i,1) 和 c(i,2). 现在要挑 ...