首页 > 其他分享 >Android图形系统之Surface、Surfac…

Android图形系统之Surface、Surfac…

时间:2023-07-28 18:32:34浏览次数:41  
标签:图形系统 int Surfac Surface SurfaceView SurfaceHolder surface Android android


1、Surface


Surface

extends Object implements Parcelable



Class Overview


Handle onto a raw buffer that is being managed by the screen compositor.

简单翻译:

Surface是原始图像缓冲区(raw buffer)的一个句柄,而原始图像缓冲区是由屏幕图像合成器(screen compositor)管理的。



java.lang.Object

  ↳

android.view.Surface



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 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,同时也可以把数据读出来。


android.os.Parcelable



2、SurfaceView


SurfaceView

extends View

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()之间)才能让渲染进程访问。


java.lang.Object

  ↳

android.view.View


  ↳

android.view.SurfaceView


2.1、SurfaceView与Surface的联系

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

两者联系如图所示:





3、SurfaceHolder


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().这两个方法。

android.view.SurfaceHolder

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)

           


4、SurfaceHolder.Callback

SurfaceHolder.Callback


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改变的一个接口

android.view.SurfaceHolder.Callback

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)

效果图:

Android图形系统之Surface、Surfac…_java


@MySurfaceView.java




package com.conowen.SurfaceViewDemo;

import android.app.Activity;
import android.os.Bundle;

public class SurfaceViewDemoActivity extends Activity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);
        setContentView(new MySurfaceView(this));
        
    }
}



@SurfaceViewDemoActivity.java



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

标签:图形系统,int,Surfac,Surface,SurfaceView,SurfaceHolder,surface,Android,android
From: https://blog.51cto.com/u_548275/6886102

相关文章

  • android与PC,C#与Java 利用protob…
    protobuf是什么? Protocolbuffers是一种编码方法构造的一种有效而可扩展的格式的数据。谷歌使用其内部几乎RPC协议和文件格式的所有协议缓冲区。参考文档http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/overview.html  API的 参考文档 ......
  • Android中的Audio播放:竞争Audio…
    Android是多任务系统,Audio系统是竞争资源。Android2.2之前,没有内建的机制来解决多个程序竞争Audio的问题,2.2引入了称作AudioFocus的机制来管理对Audio资源的竞争的管理与协调。本文主要讲解AudioFocus的使用。按照AudioFocus的机制,在使用AudioStream之前,需要申请AudioFocus,在获得A......
  • android下面res目录使用
    res文件夹里面的多个文件夹的各自介绍(来自网上的Android开发指南中文版内容):  *res/raw和assets的相同点:目录Directory资源类型ResourceTypesres/anim/XML文件,它们被编译进逐帧动画(framebyframeanimation)或补间动画(tweenedanimation)对象res/drawable/.png、.9.png、.......
  • Android实现数据存储技术
    本文介绍Android中的5种数据存储方式。数据存储在开发中是使用最频繁的,在这里主要介绍Android平台中实现数据存储的5种方式,分别是:使用SharedPreferences存储数据; 文件存储数据;SQLite数据库存储数据;使用ContentProvider存储数据;网络存储数据;下面将为大家一一详细介绍。第一种: ......
  • Android处理图片OOM的若干方法小结
    众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视平台而定)。因此在开发应用时需要特别关注自身的内存使用量,而一般最耗内存量的资源,一般是图片、音频文件、视频文件等多媒体资源;由于Android系统对音频、视频等资源做了边解析便播放的处理,使用时......
  • Android之Widget  Frame…
        2AppWidget Framework2.1定义    (1)AppWidgetService是框架的的核心类,是系统service之一,它负责widgets的管理工作。加载,删除,定时事件等都需要AppWidgetService的处理。开机自启动的。     ......
  • android网络通信之HTTP协议教程实…
    在现在的开发和应用中,网络通讯是必不可少的。虽然还是比较怀念小时候,抱着一台95在那里玩单机游戏玩的天昏地暗的时光,但是,现在,就算一个幼儿园的小盆友如果问你要手机玩游戏,突然发现居然买不了冰激凌草莓果汁什么的去喂talkinggina,或者切出一个超爆的水果分数却传不到网上去炫......
  • Android AIDL——实现机制浅析
    1.基于前面写的aidl使用,这段时间准备研究ActivityManager框架,对aidl进行了更深入的研究,因为android框架大量使用了进程通信机制,所以,在研究androidframework前认真研究一下AIDL的实现机制十分有必要的3.aidl接口packagecom.cao.android.demos.binder.aidl;importcom.cao.an......
  • Android NFC简介
    目录:AndroidNFC1 简介1.1 什么是 NFC1.2Android 上的 NFC1.3Android 中的 NFC 通讯机制2 用法2.1 读取 tag2.2 发送消息2.2.1 发送过程2.2.2NDEF 消息格式1 简介1.1 什么是 NFC近场通讯 (NearFiledCommunication) 技术是一种近距离的高频无线......
  • Android-组件属性
    ImageView:app:tint=“”(tint:染色)作用:将非透明像素点染成指定颜色 EditText:android:selectAllOnFocus="true"作用:输入框获取焦点后,获取到所有文本内容 ......