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

//@\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

android.os.Parcelable

Known

Indirect Subclasses

AbsSavedState,AbsoluteSizeSpan,AccessibilityEvent,

AccessibilityNodeInfo,

AccessibilityServiceInfo,

Account,

AccountAuthenticatorResponse,

ActivityInfo,

ActivityManager.MemoryInfo,

ActivityManager.ProcessErrorStateInfo,

ActivityManager.RecentTaskInfo,

ActivityManager.RunningAppProcessInfo,

ActivityManager.RunningServiceInfo,

ActivityManager.RunningTaskInfo, and

144 others.

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

GLSurfaceView,RSSurfaceView,VideoView

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的(关于Z-ordered,可以参看下一篇博文),这表明它总在自己所在窗口的后面。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就是把这些数据显示出来到屏幕上面。

两者联系如图所示:

52128627_1.png

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

GLSurfaceView,NativeActivity,RSSurfaceView,SurfaceHolder.Callback2

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.

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

5、Demo小程序

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

SurfaceHolder = SurfaceView.getHolder();

Surface = SurfaceHolder.getSurface();

Canvas =SurfaceHolder.LockCanvas(Rect dirty)

Canvas   =Surface.lockCanvas(Rect dirty)

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

  1. 【Android FFMPEG 开发】FFMPEG ANativeWindow 原生绘制 ( Java 层获取 Surface | 传递画布到本地 | 创建 ANativeWindow )

    文章目录 I . FFMPEG ANativeWindow 原生绘制 II . FFMPEG 原生绘制流程 III . Java 层获取 Surface 画布 IV . 传递 Surface 画布到 ...

  2. android native java_在Android Native层中创建Java虚拟机实例

    前言 Android应用中JNI代码,是作为本地方法运行的.而大部分情况下,这些JNI方法均需要传递Dalvik虚拟机实例作为第一个参数.例如,你需要用虚拟机实例来创建jstring和其他的Java对 ...

  3. 口袋妖怪 java_简单的Java口袋妖怪扑灭模拟器

    我已经写了一个类创建和战斗口袋妖怪,但我不知道如何调用测试类中的战斗方法,以测试我写的类.简单的Java口袋妖怪扑灭模拟器 我的任务是编写和测试模拟两个口袋妖怪之间的战斗模拟.每个口袋妖怪都有一个健康 ...

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

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

  5. Node.js中exports、module.exports、require之间的关系

    Node中的js文件 Node中的每个JS文件都是一个单独的模块,模块中包含的核心变量:exports.module.exports.require nodejs中module文档 // 插入图片 E ...

  6. 3.UML中的类图及类图之间的关系

    统一建模语言简介 统一建模语言(Unified Modeling Language,UML)是用来设计软件蓝图的可视化建模语言,1997 年被国际对象管理组织(OMG)采纳为面向对象的建模语言的国际标 ...

  7. 程序中try、throw、catch三者之间的关系

    c++程序中,采用一种专门的结构化处理逻辑的异常处理机制. 1.try语句 try语句块的作用是启动异常处理机制,检测try语句块中程序语句执行时可能出现的异常. try语句块总是与catch一同出现 ...

  8. spring中的控制反转和依赖注入之间的关系

    Spring中的控制反转:把new这一个过程交给了spring容器去处理. 控制反转就是将new对象这一个过程交给外部去做(即Spring)而不是自己去创建. 图中的1"控制正转" ...

  9. java catch中抛出异常_简单了解Java编程中抛出异常的方法

    任何Java代码都可以抛出异常,如:自己编写的代码.来自Java开发环境包中代码,或者Java运行时系统.无论是谁,都可以通过Java的throw语句抛出异常.从方法中抛出的任何异常都必须使用thro ...

最新文章

  1. 20幅漫画告诉你未来世界是怎样的
  2. python标准异常
  3. 玩转Mixly – 9、Arduino AVR编程 之 函数
  4. POJ-2942:吃糖果
  5. C语言循环为1404的循环,考试,求大神帮忙,C语言,小弟感激不尽
  6. 脚本语言+文档对象模型基于浏览器客户端的编程
  7. 青、取之于蓝,而青于蓝。
  8. Tether已在以太坊扩容方案Hermez Network上发行USDT
  9. 【react】 flux 的数据修改流程,类似与vuex那样,但是又有区别
  10. 锐捷认证客户端常见问题解决及简介
  11. 德鲁克:终生难忘的7堂课
  12. SpringBoot系列(4):构建一个多模块SpringBoot项目
  13. Can‘t update has no tracked branch
  14. JDBC(Java数据库连接)
  15. Win10 Office2016 激H失败错误代码0x80080005
  16. [爱情智慧]爱作的女人,最后都不怎么好!学会述情才能婚姻幸福!
  17. 程序员怎么接单赚外快,去这6个平台就可以了!
  18. python复数计算符号_Python:基本运算、基本函数(包括复数)、Math模块、NumPy模块...
  19. Window 10未连接到互联网
  20. 邢台学院计算机老师待遇2020,老师待遇不好?2020年的三个教师改革,将会让老师迎来事业第二春...

热门文章

  1. 20221121 秩相同,值域不一定相同
  2. GPS快速定位之----AGPS、EPO
  3. win10连接共享打印机_连接不了局域网共享打印机
  4. IP问问区县级别IP地址库---使用帮助(下篇)
  5. 移动互联网 3G时代的“新宠”
  6. Android 10以上,保存视频通知到相册
  7. html5的colgroup,HTML5 教程之HTML colgroup 标签
  8. 转:教你用CHARIOT测量带宽、网速
  9. 1型错误和2型错误_W.1 仿真错误定位:gjob exits with status * ?
  10. 炉石辛达苟斯贫民打法