网站建设 朝阳区,一元购网站建设方案书,国外服务器品牌前十大排名,wordpress图片地址Android应用开发学习笔记——目录索引 参考Android官网#xff1a;https://developer.android.com/reference/android/view/SurfaceView 一、SurfaceView简介 
SurfaceView派生自View#xff0c;提供嵌入视图层次结构内部的专用绘图表面#xff0c;SurfaceView可以在主线程之…Android应用开发学习笔记——目录索引 参考Android官网https://developer.android.com/reference/android/view/SurfaceView 一、SurfaceView简介 
SurfaceView派生自View提供嵌入视图层次结构内部的专用绘图表面SurfaceView可以在主线程之外的线程中向屏幕绘图这样可以避免画图任务繁重的时候造成主线程阻塞从而提高了程序的反应速度。 
通过 SurfaceHolder 接口提供对Surface的访问调用getHolder()方法获取SurfaceHolder。 
实现SurfaceHolder.Callback回调接口回调是通过 SurfaceHolder.addCallback方法设置。 
SurfaceHolder.Callback中定义了三个接口方法 
SurfaceHolder.Callback#surfaceCreated  // 当首次创建surface后立即调用。 SurfaceHolder.Callback#surfaceChanged  //当surface进行任何更改立即调用此方法。 SurfaceHolder.Callback#surfaceDestroyed  //当surface即将被破坏之前调用的。 
//当surface对象创建后该方法就会被立即调用。 
public voidsurfaceCreated(SurfaceHolder holder)   {  }  //当surface发生任何结构性的变化时格式或者大小该方法就会被立即调用。
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, intarg3) {  }  //当surface对象在将要销毁前该方法会被立即调用。   
public voidsurfaceDestroyed(SurfaceHolder holder)  {  }   
什么是Surface简单的说Surface对应了一块屏幕缓冲区每个window对应一个Surface任何View都要画在Surface的Canvas画布上。传统的view共享一块屏幕缓冲区所有的绘制必须在UI线程中进行。 在SDK的文档中对Surface的描述是这样的“Handle onto a raw buffer that is being managed by the screen compositor”翻译成中文就是“由屏幕显示内容合成器(screen compositor)所管理的原始缓冲区的句柄”这句话包括下面两个意思 1、通过Surface就可以获得原生缓冲器以及其中的内容因为Surface是句柄。就像在C语言中可以通过一个文件的句柄就可以获得文件的内容一样。 2、原始缓冲区a raw buffer是用于保存当前窗口的像素数据的。引伸地可以认为Android中的Surface就是一个用来画图形graphics或图像image的涂鸦场所。由此可以推知一个Surface对象中应该包含有一个Canvas画布对象。 因此在前面提及的两个意思的基础上可以再加上一条 3、Surface中有一个Canvas成员专门用于画图的。 由以上的概括我们总结如下Surface中的Canvas成员是专门用于供程序员画图的场所就像黑板一样其中的原始缓冲区是用来保存数据的地方Surface本身的作用类似一个句柄得到了这个句柄就可以得到其中的Canvas、原始缓冲区以及其它方面的内容。 Surface是用来管理数据的句柄在这里“数据”指的就是画板的内容。。 SurfaceView 的 Public 方法 voidapplyTransactionToFrame(SurfaceControl.Transaction transaction) 添加一个事务该事务将与显示 SurfaceView 的下一帧同步应用。 voiddraw(Canvas canvas) 手动将此视图及其所有子视图渲染到给定的画布。 booleangatherTransparentRegion(Region region) 当视图层次结构包含一个或多个 SurfaceView 时ViewRoot 使用它来执行优化。 SurfaceHoldergetHolder() 返回 SurfaceHolder提供对此 SurfaceView 底层表面的访问和控制。 IBindergetHostToken() 用于构造 的令牌SurfaceControlViewHost。 intgetImportantForAccessibility() 获取用于确定此 View 对于可访问性是否重要的模式。 SurfaceControlgetSurfaceControl() 返回一个 SurfaceControl可用于将 Surface 设为此 SurfaceView 的父级。 booleanhasOverlappingRendering() 返回此视图是否有重叠的内容。 voidsetAlpha(float alpha) 将视图的不透明度设置为 0 到 1 之间的值其中 0 表示视图完全透明1 表示视图完全不透明。 voidsetChildSurfacePackage(SurfaceControlViewHost.SurfacePackage p) 显示嵌入此 SurfaceView 中的视图层次结构SurfaceControlViewHost.SurfacePackage 。 voidsetClipBounds(Rect clipBounds) 在此视图上设置一个矩形区域绘制视图时将剪裁到该区域。 voidsetSecure(boolean isSecure) 控制是否应将表面视图的内容视为安全以防止其出现在屏幕截图中或在非安全显示器上查看。 voidsetSurfaceLifecycle(int lifecycleStrategy) 控制此 SurfaceView 拥有的 Surface 的生命周期。 voidsetVisibility(int visibility) 设置此视图的可见性状态。 voidsetZOrderMediaOverlay(boolean isMediaOverlay) 控制表面视图的表面是否放置在窗口中另一个常规表面视图的顶部但仍在窗口本身的后面。 voidsetZOrderOnTop(boolean onTop) 控制表面视图的表面是否放置在其窗口的顶部。  SurfaceView 的 Protected 方法 voiddispatchDraw(Canvas canvas) 由draw调用来绘制子视图。 voidonAttachedToWindow() 当视图附加到窗口时调用此方法。 voidonDetachedFromWindow() 当视图与窗口分离时调用此方法。 voidonFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) 当该视图的焦点状态发生变化时由视图系统调用。 voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)  测量视图及其内容以确定测量的宽度和测量的高度。 booleanonSetAlpha(int alpha) 如果存在涉及 alpha 的 Transform则调用。 voidonWindowVisibilityChanged(int visibility) 当包含的窗口的可见性发生更改在GONE、INVISIBLE和 之间VISIBLE时调用。  
二、SurfaceView和View的不同之处 
Android提供了View来进行绘图处理在大部分情况下View都能满足绘图需求。大家都知道View是通过刷新来重绘视图Android系统通过发出VSYNC信号来进行屏幕的重绘刷新的间隔时间为16ms。如果在16ms内View完成了你所需要执行的所有操作那么用户在视觉上就不会产生卡顿的感觉反之如果操作的逻辑过多时处理时间超过了一帧的时间周期就会掉帧从而使得用户感觉到卡顿。特别的需要频繁刷新的界面上如游戏60FPS以上、高帧率视频等就会不断阻塞主线程从而导致界面卡顿。基于此Android提供SurfaceView来解决这种情况。 
ViewSurfaceView共享Surface独立Surface在主线程中进行画面更新通常通过一个子线程来进行画面更新 
SurfaceView和View一大不同View通过invalidate方法通知系统来主动刷新界面的但View的刷新是依赖于系统的VSYSC信号的而且因为UI线程中的其他一些操作会导致掉帧卡顿。而对于SurfaceView而言SurfaceView是在子线程中绘制图形根据这一特性即可控制其显示帧率通过简单地设置休眠时间即可并且由于在子线程中一般不会引起UI卡顿。 
Thread.sleep(50);即可以控制1s内刷新20次 
三、SurfaceView的基本操作 
1. 测试程序一下面通过画圆来介绍SurfaceView的基本操作 
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SurfaceHolder.Callback {private static final String TAG  SurfaceViewTest;private Button mButton;private SurfaceView mSurfaceView;private SurfaceHolder mSurfaceHolder;private Paint mPaint  new Paint();private int mCircleRadius  10;private boolean isRunning  false;private boolean isStart  false;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mSurfaceView  (SurfaceView) findViewById(R.id.surfaceView);mSurfaceHolder  mSurfaceView.getHolder();mSurfaceHolder.addCallback(this);mButton  (Button) findViewById(R.id.button);mButton.setOnClickListener(this);mButton.setText(启动);}Overridepublic void onClick(View v) {if (v.getId()  R.id.button) {isStart  !isStart;if (isStart) {mButton.setText(停止);start();} else {mButton.setText(启动);stop();}}}Overridepublic void surfaceCreated(NonNull SurfaceHolder surfaceHolder) {Log.d(TAG, surfaceCreated...);mPaint.setAntiAlias(true); // 设置画笔为无锯齿mPaint.setColor(Color.RED); // 设置画笔的颜色mPaint.setStrokeWidth(10); // 设置画笔的线宽mPaint.setStyle(Paint.Style.FILL); // 设置画笔的类型。STROK表示空心FILL表示实心mPaint.setTextSize(30);}Overridepublic void surfaceChanged(NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {Log.d(TAG, surfaceChanged...);}Overridepublic void surfaceDestroyed(NonNull SurfaceHolder surfaceHolder) {Log.d(TAG, surfaceDestroyed...);}// 开始绘制public void start() {isRunning  true;new Thread() {Overridepublic void run() {while (isRunning) {drawCircle();try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}}}}.start();}// 停止绘制public void stop() {isRunning  false;}// 绘制图形private void drawCircle() {long now  System.currentTimeMillis();synchronized (mSurfaceHolder) {Canvas canvas  mSurfaceHolder.lockCanvas();if (canvas ! null) {// 设置画布为灰色背景色canvas.drawARGB(255, 55, 55, 55);// 画圆canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, mCircleRadius, mPaint);if (mCircleRadius  canvas.getWidth() / 2) {mCircleRadius;} else {mCircleRadius  10;}mSurfaceHolder.unlockCanvasAndPost(canvas);}}}
} 
layout XML 
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parenttools:context.MainActivitySurfaceViewandroid:idid/surfaceViewandroid:layout_width350dpandroid:layout_height350dpandroid:layout_marginTop8dpapp:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent /Buttonandroid:idid/buttonandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_marginTop16dpandroid:text启动app:layout_constraintEnd_toEndOfparentapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toBottomOfid/surfaceView /
/androidx.constraintlayout.widget.ConstraintLayout 
2. 测试程序二下面通过touch划线来介绍SurfaceView的基本操作 
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {private final static String TAG  lzl-test;private SurfaceView mSurfaceView;//SurfaceHolderprivate SurfaceHolder mHolder;//用于绘图的Canvasprivate Canvas mCanvas;//子线程标志位private boolean mIsDrawing;//画笔private Paint mPaint;//路径private Path mPath;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.d(TAG, onCreate...);setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); //设置屏幕不随手机旋转setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //设置屏幕直向显示mSurfaceView  (SurfaceView) findViewById(R.id.surfaceView);mHolder  mSurfaceView.getHolder();mHolder.addCallback(this);/* 清屏 */findViewById(R.id.button_clear).setOnClickListener(new View.OnClickListener() {Overridepublic void onClick(View v) {if (v.getId()  R.id.button_clear) {mPath.reset();}}});}Overridepublic void surfaceCreated(NonNull SurfaceHolder holder) {Log.d(TAG, surfaceCreated...);if (mHolder  null) {mHolder  mSurfaceView.getHolder();mHolder.addCallback(this);}mPath  new Path();//初始化画笔mPaint  new Paint();mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(6);mPaint.setAntiAlias(true);mPaint.setColor(Color.RED);mSurfaceView.setFocusable(true);mSurfaceView.setFocusableInTouchMode(true);mSurfaceView.setKeepScreenOn(true);mIsDrawing  true;new Thread(runnable).start();}Overridepublic void surfaceChanged(NonNull SurfaceHolder holder, int format, int width, int height) {Log.d(TAG, surfaceChanged...);}Overridepublic void surfaceDestroyed(NonNull SurfaceHolder holder) {Log.d(TAG, surfaceDestroyed...);mIsDrawing  false;mHolder  null;}private void draw() {if (mHolder ! null) {Canvas canvas  null;try{//用于绘图的Canvas, 锁定画布并返回画布对象canvas  mHolder.lockCanvas();//接下去就是在画布上进行一下drawcanvas.drawColor(Color.WHITE);canvas.drawPath(mPath,mPaint);} catch (Exception e){} finally {//当画布内容不为空时才post避免出现黑屏的情况。if(canvas !null  mHolder ! null)mHolder.unlockCanvasAndPost(canvas);}}}private  Runnable runnable  new Runnable() {Overridepublic void run() {long start System.currentTimeMillis();while(mIsDrawing){draw();long end  System.currentTimeMillis();if(end-start100){try{Thread.sleep(100-endstart);} catch (InterruptedException e) {e.printStackTrace();}}}}};Overridepublic boolean onTouchEvent(MotionEvent event) {int x(int) event.getX();int y (int) event.getY();switch (event.getAction()){case MotionEvent.ACTION_DOWN:Log.d(TAG, onTouchEvent: down);mPath.moveTo(x,y);break;case MotionEvent.ACTION_MOVE:Log.d(TAG, onTouchEvent: move);mPath.lineTo(x,y);break;case MotionEvent.ACTION_UP:Log.d(TAG, onTouchEvent: up);break;}return true;}
} 四、测试程序 
完整源码 
百度网盘链接:百度网盘 请输入提取码 提取码:test 
SurfaceViewTest目录 
运行效果 点此查看Android应用开发学习笔记的完整目录 文章转载自: http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.lkwyr.cn.gov.cn.lkwyr.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.plqqp.cn.gov.cn.plqqp.cn http://www.morning.whpsl.cn.gov.cn.whpsl.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.qsszq.cn.gov.cn.qsszq.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.cjqqj.cn.gov.cn.cjqqj.cn http://www.morning.dblgm.cn.gov.cn.dblgm.cn http://www.morning.qkwxp.cn.gov.cn.qkwxp.cn http://www.morning.tfznk.cn.gov.cn.tfznk.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.xckqs.cn.gov.cn.xckqs.cn http://www.morning.rqbr.cn.gov.cn.rqbr.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn http://www.morning.qtyfb.cn.gov.cn.qtyfb.cn http://www.morning.prmyx.cn.gov.cn.prmyx.cn http://www.morning.tfwr.cn.gov.cn.tfwr.cn http://www.morning.deanzhu.com.gov.cn.deanzhu.com http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.tbnn.cn.gov.cn.tbnn.cn http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn http://www.morning.qbdqc.cn.gov.cn.qbdqc.cn http://www.morning.rnmmh.cn.gov.cn.rnmmh.cn http://www.morning.gkmwk.cn.gov.cn.gkmwk.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.fbxdp.cn.gov.cn.fbxdp.cn http://www.morning.bwmm.cn.gov.cn.bwmm.cn http://www.morning.mxhgy.cn.gov.cn.mxhgy.cn http://www.morning.qtltg.cn.gov.cn.qtltg.cn http://www.morning.gpkjx.cn.gov.cn.gpkjx.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.nqcwz.cn.gov.cn.nqcwz.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.fycjx.cn.gov.cn.fycjx.cn http://www.morning.qfmns.cn.gov.cn.qfmns.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.ylyzk.cn.gov.cn.ylyzk.cn http://www.morning.rnqyy.cn.gov.cn.rnqyy.cn http://www.morning.fbmjw.cn.gov.cn.fbmjw.cn http://www.morning.fypgl.cn.gov.cn.fypgl.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.xfwnk.cn.gov.cn.xfwnk.cn http://www.morning.bsqbg.cn.gov.cn.bsqbg.cn http://www.morning.jbgzy.cn.gov.cn.jbgzy.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.rmdwp.cn.gov.cn.rmdwp.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.cjmmt.cn.gov.cn.cjmmt.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.horihe.com.gov.cn.horihe.com http://www.morning.bgzgq.cn.gov.cn.bgzgq.cn http://www.morning.ttaes.cn.gov.cn.ttaes.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn http://www.morning.xkyst.cn.gov.cn.xkyst.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.pqkgb.cn.gov.cn.pqkgb.cn http://www.morning.wkhfg.cn.gov.cn.wkhfg.cn http://www.morning.ftznb.cn.gov.cn.ftznb.cn http://www.morning.rkbly.cn.gov.cn.rkbly.cn http://www.morning.xsgxp.cn.gov.cn.xsgxp.cn http://www.morning.ykqbs.cn.gov.cn.ykqbs.cn http://www.morning.cnqdn.cn.gov.cn.cnqdn.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.pflry.cn.gov.cn.pflry.cn