双缓冲技术解决方案之一:保存所有要绘制内容,全屏重绘
package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceView; public class AllRefreshTextView extends SurfaceView { private Paint mPaint; public AllRefreshTextView(Context context) { super(context); init(); } public AllRefreshTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public AllRefreshTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setTextSize(30); getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder holder) { drawText(holder); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }); } private void drawText(final SurfaceHolder holder) { new Thread(new Runnable() { @Override public void run() { Canvas canvas = holder.lockCanvas(); //一次性绘制完成 for (int i = 0; i < 10; i++) { if (canvas != null) { canvas.drawText(i + "", i * 30, 50, mPaint); } } holder.unlockCanvasAndPost(canvas); } }).start(); } }
效果:
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!