Android图形系统之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的联系
1、Surface
Surfaceextends Object
Class OverviewHandle onto a raw buffer that is being managed by the screen compositor. 简单翻译: Surface是原始图像缓冲区(raw buffer)的一个句柄,而原始图像缓冲区是由屏幕图像合成器(screen compositor)管理的。 |
1.1、 就如在C语言编程一样,通过一个文件的句柄,就可以操作文件,获取文件的内容。 同样的,通过Surface就可以获取raw buffer其中的内容。原生缓冲区(raw buffer)存储着当前窗口的像素数据。
1.2、事实上,当得到一个Surface对象时,同时会得到一个Canvas(画布)对象。这一点可以通过查看\frameworks\base\core\Java\Android\view\Surface.java文件可知道Surface类定义了一个Canvas成员变量
- //@\frameworks\base\core\java\android\view\Surface.java
- // The mSurfaceControl will only be present for Surfaces used by the window
- // server or system processes. When this class is parceled we defer to the
- // mSurfaceControl to do the parceling. Otherwise we parcel the
- // mNativeSurface.
- private int mSurfaceControl;
- private int mSaveCount;
- private Canvas mCanvas;
- private int mNativeSurface;
- private int mSurfaceGenerationId;
- private String mName;
1.3、 理解Canvas对象,可以把它当做画布,Canvas的方法大多数是设置画布的大小、形状、画布背景颜色等等,要想在画布上面画画,一般要与Paint对象结合使用,顾名思义,Paint就是画笔的风格,颜料的色彩之类的。
- // 创建画笔
- Paint paint = new Paint();
- paint.setColor(Color.RED);// 设置红色
- canvas.drawCircle(60, 20, 10, paint);// 画一个圆
1.4、Surface本身的作用类似一个句柄,得到了这个句柄就可以得到其中的Canvas、原生缓冲器以及其它方面的内容。
1.5、Surface实现了Parcelable接口,(implements Parcelable),也就是说Surface对象可以把显示内容的数据写入到 Parcel 中,并且能够从Parcel读回数据。
Parcelable
Class OverviewInterface for classes whose instances can be written to and restored from a 简单翻译: |
2、SurfaceView
SurfaceViewextends View
Class OverviewProvides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes. Access to the underlying surface is provided via the SurfaceHolder interface, which can be retrieved by calling The Surface will be created for you while the SurfaceView's window is visible; you should implement One of the purposes of this class is to provide a surface in which a secondary thread can render into the screen. If you are going to use it this way, you need to be aware of some threading semantics:
简单翻译: SurfaceView提供了一个专门用于绘制的surface,这个surface内嵌于。你可以控制这个Surface的格式和尺寸。Surfaceview控制这个Surface在屏幕的正确绘制位置。
|
2.1、SurfaceView与Surface的联系
简单来说,SurfaceView与Surface的联系就是,Surface是管理显示内容的数据(implementsParcelable),包括存储于数据的交换。而SurfaceView就是把这些数据显示出来到屏幕上面。
两者联系如图所示:
3、SurfaceHolder
SurfaceHolder
Class OverviewAbstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface. This interface is typically available through the When using this interface from a thread other than the one running its 简单翻译: SurfaceHolder是控制surface的一个抽象接口,你可以通过SurfaceHolder来控制surface的尺寸和格式,或者修改surface的像素,监视surface的变化等等,SurfaceHolder是SurfaceView的典型接口。 |
SurfaceHolder控制surface的流程所使用的几个方法。
3.1、abstract void addCallback(SurfaceHolder.Callback callback) Add a Callback interface for this holder.// 给SurfaceHolder一个回调对象。 3.2、abstract Canvas lockCanvas(Rect dirty) Just like lockCanvas() but allows specification of a dirty rectangle. // 锁定画布中的某一个区域,返回的画布对象Canvas(当更新的内容只有一个区域时,同时要追求高效,可以只更 新一部分的区域,而不必更新全部画布区域) 3.3、abstract Canvas lockCanvas() Start editing the pixels in the surface.// 锁定画布,返回的画布对象Canvas 3.4、abstract void removeCallback(SurfaceHolder.Callback callback) Removes a previously added Callback interface from this holder.//移除回调对象 3.5、abstract void unlockCanvasAndPost(Canvas canvas) Finish editing pixels in the surface.// 结束锁定画图,并提交改变。 |
4、SurfaceHolder.Callback
SurfaceHolder.Callback
Class OverviewA client may implement this interface to receive information about changes to the surface. When used with a 简单翻译: SurfaceHolder.Callback是监听surface改变的一个接口 |
4.1、public abstract voidsurfaceChanged(SurfaceHolder holder, int format, int width, int height)
holder | The SurfaceHolder whose surface has changed. |
---|---|
format | The new PixelFormat of the surface. |
width | The new width of the surface. |
height | The new height of the surfa |
//surface发生改变时被调用
4.2、public abstract voidsurfaceCreated(SurfaceHolder holder)
Parameters
holder | The SurfaceHolder whose surface is being created |
---|
//在surface创建时被调用,一般在这个方法里面开启渲染屏幕的线程。
4.3、public abstract voidsurfaceDestroyed(SurfaceHolder holder)
Parameters
holder | The SurfaceHolder whose surface is being destroyed. |
---|
//销毁时被调用,一般在这个方法里将渲染的线程停止。
附上上述所说几种的联系方法
- SurfaceHolder = SurfaceView.getHolder();
- Surface = SurfaceHolder.getSurface();
- Canvas =SurfaceHolder.LockCanvas(Rect dirty)
- Canvas =Surface.lockCanvas(Rect dirty)
5、Demo小程序
共有两个class效果图如下,具体看代码和注释。
进入程序,执行
- public void surfaceCreated(SurfaceHolder holder)
然后执行
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
退出程序调用surfaceDestroyed,释放资源。
- public void surfaceDestroyed(SurfaceHolder holder)
/****************************************************************/
效果图:
@MySurfaceView.java
- /*
- * author: conowen
- * e-mail: conowen@hotmail.com
- * date : 2012.8.4
- */
- package com.conowen.SurfaceViewDemo;
- import android.app.Activity;
- import android.os.Bundle;
- public class SurfaceViewDemoActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // setContentView(R.layout.main);
- setContentView(new MySurfaceView(this));
- }
- }
@SurfaceViewDemoActivity.java
- /*
- * author: conowen
- * e-mail: conowen@hotmail.com
- * date : 2012.8.4
- */
- package com.conowen.SurfaceViewDemo;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- public class MySurfaceView extends SurfaceView implements
- SurfaceHolder.Callback {
- private String TAG = "conowen";
- private SurfaceHolder sfh;
- private boolean ThreadFlag;
- private int counter;
- private Canvas canvas;
- private Thread mThread = new Thread(new Runnable() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while (ThreadFlag) {
- // 锁定画布,得到Canvas对象
- canvas = sfh.lockCanvas();
- // 设定Canvas对象的背景颜色
- canvas.drawColor(Color.GREEN);
- // 创建画笔
- Paint p = new Paint();
- // 设置画笔颜色
- p.setColor(Color.RED);
- // 设置文字大小
- p.setTextSize(40);
- // 创建一个Rect对象rect
- // public Rect (int left, int top, int right, int bottom)
- Rect rect = new Rect(100, 50, 400, 350);
- // 在canvas上绘制rect
- canvas.drawRect(rect, p);
- // 在canvas上显示时间
- // public void drawText (String text, float x, float y, Paint
- // paint)
- canvas.drawText("时间 = " + (counter++) + " 秒", 500, 200, p);
- if (canvas != null) {
- // 解除锁定,并提交修改内容,更新屏幕
- sfh.unlockCanvasAndPost(canvas);
- }
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- });
- public MySurfaceView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- // 通过SurfaceView获得SurfaceHolder对象
- sfh = this.getHolder();
- // 为SurfaceHolder添加回调结构SurfaceHolder.Callback
- sfh.addCallback(this);
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
- Log.i(TAG, "surfaceChanged");
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- Log.i(TAG, "surfaceCreated");
- counter = 0;
- ThreadFlag = true;
- mThread.start();
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- Log.i(TAG, "surfaceDestroyed");
- ThreadFlag = false;
- }
- }

Android图形系统之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的联系的更多相关文章
- Android图形系统之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback开发实例
原文:Android图形系统之Surface.SurfaceView.SurfaceHolder及SurfaceHolder.Callback之间的联系 Surface是原始图像缓冲区(raw buf ...
- Android中的Surface, SurfaceHolder, SurfaceHolder.Callback, SurfaceView
传入一个surface,然后让openGL在surface上画图 window->view hierachy(DecorView是tree的root)->ViewRoot->Surf ...
- Android 展示控件之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系
一.Surface Surface在SDK的文档中的描述是这样的:Handle onto a raw buffer that is being managed by the screen compos ...
- Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系
转载请包含网址:http://blog.csdn.net/pathuang68/article/details/7351317 一.Surface Surface就是“表面”的意思.在SDK的文档中, ...
- Android中的Surface和SurfaceView
一.什么是Surface 简单的说Surface对应了一块屏幕缓冲区,每个window对应一个Surface,任何View都要画在Surface的Canvas上(后面有原因解释).传统的view共享一 ...
- Android中surface,surfaceview,sufaceholder以及surface客户端的关系
这里以照相机camera功能的实现来解释surface,surfaceview,sufaceholder以及surface客户端(本例子中指的是camera)的关系,surface及其client(客 ...
- Android 学习笔记之SurfaceView的使用+如何实现视频播放...
学习内容: 1.掌握Surface的使用... 2.Android中如何实现视频播放... 1.SurfaceView类的使用 在Android中,一般播放音频时我们可以去使用Android提供的 ...
- android 使用两个surfaceview 在摄像机画面上绘图
转载自http://blog.csdn.net/jesse__zhong/article/details/24934083 使用双surface,将第一个设置为透明背景,在摄像机上绘制图像,纠结搞了一 ...
- Android之View和SurfaceView
Android之View和SurfaceView Android游戏当中主要的除了控制类外就是显示类View.SurfaceView是从View基类中派生出来的显示类.android游戏开发中常用的三 ...
随机推荐
- [原创]java WEB学习笔记77:Hibernate学习之路---Hibernate 版本 helloword 与 解析,.环境搭建,hibernate.cfg.xml文件及参数说明,持久化类,对象-关系映射文件.hbm.xml,Hibernate API (Configuration 类,SessionFactory 接口,Session 接口,Transaction(事务))
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 关于内存 GetMemory( ) 笔试分析
1. #include<stdio.h>#include<string.h>void GetMemory(char *p){ p=(char *)malloc(100); }i ...
- paper 82:边缘检测的各种微分算子比较(Sobel,Robert,Prewitt,Laplacian,Canny)
不同图像灰度不同,边界处一般会有明显的边缘,利用此特征可以分割图像.需要说明的是:边缘和物体间的边界并不等同,边缘指的是图像中像素的值有突变的地方,而物体间的边界指的是现实场景中的存在于物体之间的边界 ...
- DFT设计绪论
DFT设计的主要目的是为了将defect-free的芯片交给客户. 产品质量,通常使用Parts Per million(PPM)来衡量. 但是随着IC从SSI到VLSI的发展,在test上花销的时间 ...
- PAT乙级 1008. 数组元素循环右移问题 (20)
1008. 数组元素循环右移问题 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 一个数组A中存有N(N>0)个整数,在不允 ...
- 6. 星际争霸之php设计模式--建造器模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- 一个fork()系统调用的问题
转载:http://coolshell.cn/articles/7965.html 题目:请问下面的程序一共输出多少个“-”? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- Openstack的error僵尸实例的解决办法
在我们对集群环境进行各种调整的情况下,很容易产生一些僵尸实例. 僵尸实例主要是没有该主机,但是在dashboard上,数据库中存在,解决办法网络上有的人给出了繁杂的修改数据库的方法,其实按照下面的命令 ...
- 编写更少bug的程序的六条准则
如何编写更少bug的程序? 尽可能避免常见的程序错误. 沟通设计先行 + 编写可复用代码 + 做得更多 + 做的更少 + 创造“编程心流”+ 严格的程序测试 ...
- redis监控状态
Redis介绍 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表.哈希.集合和有序集合5种.支持在服务器端计算集合 ...