本文主要是介绍SurfaceView的代码模板,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
🍨SurfaceView的代码模板
🎏说在前面的:屋外是无尽的寒风,屋里是可亲的人。寒风想吹灭的你的光亮,你的任务是照亮这间小小的屋子。加油吧
1.SurfaceView 与 View的区别
-
使用View的痛点:
- View通过刷新来重绘视图,Android系统通过发出了VSYNC的信号来进行屏幕的绘制,刷新的时间间隔为16ms,如果在此时间内没有完成刷新操作,用户体验就会卡顿;
-
View主要适用于主动更新的情况,而SurfaceView主要适用于被动更新,例如频繁的刷新;
-
View在主线程中对画面进行刷新,而SURfaceView通常通过一个子线程来进行页面的刷新;
-
View在绘制的时候会使用双缓存机制,而SurfaceView在底层实现机制中已经实现了双缓存机制;
📝总结成一句话就是:如果你的自定义view需要频繁的刷新,或者刷新是数据量比较大,可以考虑使用SurfaceView;
2.SurfaceView的使用:
(1)创建SurfaceView
public class SurfaceView_eg extends SurfaceView implements SurfaceHolder.Callback,Runnable
(2)初始化SurfaceView
//syrfaceHolderprivate SurfaceHolder mHolder;//用于绘图的Canvasprivate Canvas mCanvas;//子线程标志位private boolean mIsDrawing;private int x;private int y;//初始化private void initView() {mHolder = getHolder();mHolder.addCallback(this);setFocusable(true);setFocusableInTouchMode(true);this.setKeepScreenOn(true);}
(3)使用SurfaceView(全部代码)
package com.example.android_wavebackground;import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;import androidx.annotation.NonNull;public class SurfaceView_eg extends SurfaceView implements SurfaceHolder.Callback,Runnable {//syrfaceHolderprivate SurfaceHolder mHolder;//用于绘图的Canvasprivate Canvas mCanvas;//子线程标志位private boolean mIsDrawing;private int x;private int y;public SurfaceView_eg(Context context) {super(context);initView();}//初始化private void initView() {mHolder = getHolder();mHolder.addCallback(this);setFocusable(true);setFocusableInTouchMode(true);this.setKeepScreenOn(true);}public SurfaceView_eg(Context context, AttributeSet attrs) {super(context, attrs);initView();}public SurfaceView_eg(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView();}@Overridepublic void surfaceCreated(@NonNull SurfaceHolder holder) {mIsDrawing = true;new Thread(this).start();}@Overridepublic void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {}@Overridepublic void surfaceDestroyed(@NonNull SurfaceHolder holder) {mIsDrawing = false;}@Overridepublic void run() {while (mIsDrawing) {draw();}}/*mHolder.unlockCanvasAndPost(mCanvas);放在finally中的目的是保证每次都能将内容提交*/private void draw(){try {mCanvas = mHolder.lockCanvas();}catch (Exception e){}finally {if (mCanvas != null) {mHolder.unlockCanvasAndPost(mCanvas);}}}
}
3.零碎小知识点
(1)SurfaceView的创建、改变、销毁
@Overridepublic void surfaceCreated(@NonNull SurfaceHolder holder) {}@Overridepublic void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {}@Overridepublic void surfaceDestroyed(@NonNull SurfaceHolder holder) {}
上面的三个方法分别对应SurfaceView的创建、改变、销毁的过程.
(2)绘制的几个知识点
绘制的时候,充分利用SurfaceView的三个回调方法,在SurfaceCreated()中开启一个子线程进行绘制,而子线程使用一个while()循环来不停进行绘制;
通过lockCanvas()方法获得的Canvas对象进行绘制,并通过unlockCancasAndPost()方法来对画布的内容进行提交.
这篇关于SurfaceView的代码模板的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!