1、Surface

Surface

extends Object
implements Parcelable

java.lang.Object
   ↳ Android.view.Surface

Class Overview


Handle 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成员变量

  1. //@\frameworks\base\core\java\android\view\Surface.java
  2. // The mSurfaceControl will only be present for Surfaces used by the window
  3. // server or system processes. When this class is parceled we defer to the
  4. // mSurfaceControl to do the parceling. Otherwise we parcel the
  5. // mNativeSurface.
  6. private int mSurfaceControl;
  7. private int mSaveCount;
  8. private Canvas mCanvas;
  9. private int mNativeSurface;
  10. private int mSurfaceGenerationId;
  11. private String mName;

1.3、 理解Canvas对象,可以把它当做画布,Canvas的方法大多数是设置画布的大小、形状、画布背景颜色等等,要想在画布上面画画,一般要与Paint对象结合使用,顾名思义,Paint就是画笔的风格,颜料的色彩之类的。

  1. // 创建画笔
  2. Paint paint = new Paint();
  3. paint.setColor(Color.RED);// 设置红色
  4. canvas.drawCircle(602010, paint);// 画一个圆

1.4、Surface本身的作用类似一个句柄,得到了这个句柄就可以得到其中的Canvas、原生缓冲器以及其它方面的内容。

1.5、Surface实现了Parcelable接口,(implements Parcelable),也就是说Surface对象可以把显示内容的数据写入到 Parcel 中,并且能够从Parcel读回数据。

Parcelable

android.os.Parcelable
Known Indirect Subclasses

Class Overview


Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a static field calledCREATOR, which is an object implementing theParcelable.Creator interface.

简单翻译:
       实现这个接口的对象可以写入数据到Parcel,同时也可以把数据读出来。

2、SurfaceView

SurfaceView

extends View

java.lang.Object
   ↳ android.view.View
     ↳ android.view.SurfaceView
Known Direct Subclasses

Class Overview


Provides 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 callinggetHolder().

The Surface will be created for you while the SurfaceView's window is visible; you should implementsurfaceCreated(SurfaceHolder) andsurfaceDestroyed(SurfaceHolder) to discover when the Surface is created and destroyed as the window is shown and hidden.

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:

  • All SurfaceView and SurfaceHolder.Callback methods will be called from the thread running the SurfaceView's window (typically the main thread of the application). They thus need to correctly synchronize with any state that is also touched by the drawing thread.
  • You must ensure that the drawing thread only touches the underlying Surface while it is valid -- betweenSurfaceHolder.Callback.surfaceCreated() andSurfaceHolder.Callback.surfaceDestroyed().

简单翻译:

SurfaceView提供了一个专门用于绘制的surface,这个surface内嵌于。你可以控制这个Surface的格式和尺寸。Surfaceview控制这个Surface在屏幕的正确绘制位置。

surface是Z-ordered的(也就是说在xyz坐标系中,按照Z坐标排序的,Z值大的表面覆盖在Z值小的表面的上方),这表明它总在自己所在窗口的后面。surfaceview在显示窗口处为Surface提供了一个可见区域,通过这个区域,才能看到Surface里面的内容。可以放置一些覆盖图层(overlays)在Surface上面,如Button、Textview之类的。但是,需要注意的是,如果Surface上面有全透明的控件,那么随着Surface的每一次变化,这些全透明的控件就会重新渲染,这样的话,就影响性能与显示的效果。

你可以通过SurfaceHolder这个接口去访问Surface,而执行getHolder()方法可以得到SurfaceHolder接口。

当SurfaceView的窗口可见时,Surface就会被创建,当SurfaceView窗口隐藏时,Surface就会被销毁。当然了,你也可以通过复写surfaceCreated(SurfaceHolder) 和 surfaceDestroyed(SurfaceHolder)  这两个方法来验证一下Surface何时被创建与何时被销毁。

SurfaceView提供了一个运行在渲染线程的surface,若你要更新屏幕,你需要了解以下线程知识。

  • 所有SurfaceView 和 SurfaceHolder.Callback的方法都应该在主线程(UI线程)里面调用,应该要确保渲染进程所访问变量的同步性。
  • 你必须确保只有当Surface有效的时候,(也就是当Surface的生命周期在SurfaceHolder.Callback.surfaceCreated() 和SurfaceHolder.Callback.surfaceDestroyed()之间)才能让渲染进程访问。

2.1、SurfaceView与Surface的联系

简单来说,SurfaceView与Surface的联系就是,Surface是管理显示内容的数据(implementsParcelable),包括存储于数据的交换。而SurfaceView就是把这些数据显示出来到屏幕上面。

两者联系如图所示:

linux

3、SurfaceHolder

SurfaceHolder

Android.view.SurfaceHolder

Class Overview


Abstract 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 theSurfaceView class.

When using this interface from a thread other than the one running its SurfaceView, you will want to carefully read the methodslockCanvas() andCallback.surfaceCreated().

简单翻译:

SurfaceHolder是控制surface的一个抽象接口,你可以通过SurfaceHolder来控制surface的尺寸和格式,或者修改surface的像素,监视surface的变化等等,SurfaceHolder是SurfaceView的典型接口。

与直接控制SurfaceView来修改surface不同,使用SurfaceHolder来修改surface时,需要注意lockCanvas() 和Callback.surfaceCreated().这两个方法。

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

android.view.SurfaceHolder.Callback
Known Indirect Subclasses

Class Overview


A client may implement this interface to receive information about changes to the surface. When used with aSurfaceView, the Surface being held is only available between calls tosurfaceCreated(SurfaceHolder) andsurfaceDestroyed(SurfaceHolder). The Callback is set withSurfaceHolder.addCallback method.

简单翻译:

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.

//销毁时被调用,一般在这个方法里将渲染的线程停止。

附上上述所说几种的联系方法

  1. SurfaceHolder = SurfaceView.getHolder();
  2. Surface = SurfaceHolder.getSurface();
  3. Canvas =SurfaceHolder.LockCanvas(Rect dirty)
  4. Canvas   =Surface.lockCanvas(Rect dirty)

5、Demo小程序

  1. 共有两个class效果图如下,具体看代码和注释。

    进入程序,执行

    1. public void surfaceCreated(SurfaceHolder holder)

    然后执行

    1. public void surfaceChanged(SurfaceHolder holder, int format, int width,  int height)

    退出程序调用surfaceDestroyed,释放资源。

    1. public void surfaceDestroyed(SurfaceHolder holder)

    /****************************************************************/

    效果图:

    @MySurfaceView.java

    1. /*
    2. * author: conowen
    3. * e-mail: conowen@hotmail.com
    4. * date  :  2012.8.4
    5. */
    6. package com.conowen.SurfaceViewDemo;
    7. import Android.app.Activity;
    8. import android.os.Bundle;
    9. public class SurfaceViewDemoActivity extends Activity {
    10. /** Called when the activity is first created. */
    11. @Override
    12. public void onCreate(Bundle savedInstanceState) {
    13. super.onCreate(savedInstanceState);
    14. // setContentView(R.layout.main);
    15. setContentView(new MySurfaceView(this));
    16. }
    17. }

    @SurfaceViewDemoActivity.java

    1. /*
    2. * author: conowen
    3. * e-mail: conowen@hotmail.com
    4. * date  :  2012.8.4
    5. */
    6. package com.conowen.SurfaceViewDemo;
    7. import android.content.Context;
    8. import android.graphics.Canvas;
    9. import android.graphics.Color;
    10. import android.graphics.Paint;
    11. import android.graphics.Rect;
    12. import android.util.Log;
    13. import android.view.SurfaceHolder;
    14. import android.view.SurfaceView;
    15. public class MySurfaceView extends SurfaceView implements
    16. SurfaceHolder.Callback {
    17. private String TAG = "conowen";
    18. private SurfaceHolder sfh;
    19. private boolean ThreadFlag;
    20. private int counter;
    21. private Canvas canvas;
    22. private Thread mThread = new Thread(new Runnable() {
    23. @Override
    24. public void run() {
    25. // TODO Auto-generated method stub
    26. while (ThreadFlag) {
    27. // 锁定画布,得到Canvas对象
    28. canvas = sfh.lockCanvas();
    29. // 设定Canvas对象的背景颜色
    30. canvas.drawColor(Color.GREEN);
    31. // 创建画笔
    32. Paint p = new Paint();
    33. // 设置画笔颜色
    34. p.setColor(Color.RED);
    35. // 设置文字大小
    36. p.setTextSize(40);
    37. // 创建一个Rect对象rect
    38. // public Rect (int left, int top, int right, int bottom)
    39. Rect rect = new Rect(10050400350);
    40. // 在canvas上绘制rect
    41. canvas.drawRect(rect, p);
    42. // 在canvas上显示时间
    43. // public void drawText (String text, float x, float y, Paint
    44. // paint)
    45. canvas.drawText("时间 = " + (counter++) + " 秒"500200, p);
    46. if (canvas != null) {
    47. // 解除锁定,并提交修改内容,更新屏幕
    48. sfh.unlockCanvasAndPost(canvas);
    49. }
    50. try {
    51. Thread.sleep(1000);
    52. catch (InterruptedException e) {
    53. // TODO Auto-generated catch block
    54. e.printStackTrace();
    55. }
    56. }
    57. }
    58. });
    59. public MySurfaceView(Context context) {
    60. super(context);
    61. // TODO Auto-generated constructor stub
    62. // 通过SurfaceView获得SurfaceHolder对象
    63. sfh = this.getHolder();
    64. // 为SurfaceHolder添加回调结构SurfaceHolder.Callback
    65. sfh.addCallback(this);
    66. }
    67. @Override
    68. public void surfaceChanged(SurfaceHolder holder, int format, int width,
    69. int height) {
    70. // TODO Auto-generated method stub
    71. Log.i(TAG, "surfaceChanged");
    72. }
    73. @Override
    74. public void surfaceCreated(SurfaceHolder holder) {
    75. // TODO Auto-generated method stub
    76. Log.i(TAG, "surfaceCreated");
    77. counter = 0;
    78. ThreadFlag = true;
    79. mThread.start();
    80. }
    81. @Override
    82. public void surfaceDestroyed(SurfaceHolder holder) {
    83. // TODO Auto-generated method stub
    84. Log.i(TAG, "surfaceDestroyed");
    85. ThreadFlag = false;
    86. }
    87. }

转载于:https://www.cnblogs.com/senior-engineer/p/4997325.html

Android图形系统之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的联系相关推荐

  1. Android中的Surface, SurfaceHolder, SurfaceHolder.Callback, SurfaceView

    传入一个surface,然后让openGL在surface上画图 window->view hierachy(DecorView是tree的root)->ViewRoot->Surf ...

  2. Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系

    转载请包含网址:http://blog.csdn.net/pathuang68/article/details/7351317 一.Surface Surface就是"表面"的意思 ...

  3. Surface SurfaceView SurfaceHolder

    Surface 查询Android SKD得到解释(Handle onto a raw buffer that is being managed by the screen compositor.) ...

  4. surfacecontrol.java_简单说说JAVA层中Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的关系...

    1.Surface Surface extends Object implements Parcelable java.lang.Object android.view.Surface Class O ...

  5. Android 图形之 Surface 和 SurfaceHolder

    Surface 和 SurfaceHolder Surface 对象使应用能够渲染要在屏幕上显示的图像.通过 SurfaceHolder 接口,应用可以编辑和控制 Surface. Surface S ...

  6. Android中的Android中的Surface和SurfaceView

    一.什么是Surface 简单的说Surface对应了一块屏幕缓冲区,每个window对应一个Surface,任何View都要画在Surface上.传统的view共享一块屏幕缓冲区,所有的绘制必须在U ...

  7. android中的surfaceSurface、SurfaceHolder及SurfaceHolder.Callback

    一.Surface     Surface在SDK的文档中的描述是这样的:Handle onto a raw buffer that is being managed by the screen co ...

  8. SurfaceView 使用及SurfaceHolder.Callback() 回调方法分析

    Demo,点击按钮,在子线程内绘制颜色切换的矩形 activity_main.xml <?xml version="1.0" encoding="utf-8&quo ...

  9. 一篇文章看明白 Android 图形系统 Surface 与 SurfaceFlinger 之间的关系

    Android - SurfaceFlinger 图形系统 相关系列 一篇文章看明白 Android 系统启动时都干了什么 一篇文章了解相见恨晚的 Android Binder 进程间通讯机制 一篇文 ...

  10. android中的surface

    在android中,对view及其子类,都是画在surface上的.每个window对应一个surface,各surface对象通过surfaceflinger合成到framebuffer,每个sur ...

最新文章

  1. 静态分析C语言生成函数调用关系的利器——cflow
  2. Android Spinner值不显示,选择列表正常
  3. .NET 源代码库指南
  4. 【Hash应用问题】例3.1 统计同成绩学生人数
  5. 自己搭建手游服务器端数据修改,自己架设服务器玩手游
  6. 据说200G网盘资料
  7. 小爱同学app安卓版_小爱同学app2.0.1 安卓版 下载 - 51下载网
  8. 关于电解电容ESR的问题
  9. react引入antd报错找不到antd/dist/antd.css Module not found: Error: Can‘t resolve ‘antd/dist/antd.css‘ in
  10. qt GraphicsScene添加背景
  11. 企业快速构建可落地的IT服务管理体系的五大关键点
  12. c语言共阴极数码管数字6,共阴数码管 显示数字 C语言程序
  13. 生物信息学缘起——linux篇(六)for和while循环
  14. TIA博途V17中ProDiag功能的使用方法示例(三)文本列表
  15. 敏涵控股集团董事长刘敏:感恩奉献 一路向前
  16. Android 新手引导添加View的方法
  17. 奥的斯机电物联网中心和现代化改造展厅在杭州启幕
  18. 二十六、ISIS技术总结
  19. 好用的个人电子邮箱有哪些,电子邮箱怎么注册申请?
  20. 【Android导航 君子生非异也 善假于物也】优秀博客

热门文章

  1. 手拉手教你实现一门编程语言 Enkel, 系列 15
  2. 《软件工程(第4版?修订版)》—第2章2.7节本章对单个开发人员的意义
  3. 《Android游戏编程入门经典》——14.4节问与答
  4. 第二课:电场与偶极子
  5. 处理SQLServer死锁
  6. SimpleDateFormat多线程问题
  7. BZOJ1911[APIO2010] 特别行动队
  8. 数组模拟栈解决括号匹配
  9. npm install -g @vue/cli时 -4048 npm ERR! Error: EPERM: operation not permitted, lstat报错的几种解决方案
  10. c语言怎样得到函数内参数的值_C语言零基础入门—函数01