Android 双缓冲技术
jopen
10年前
为了有效控制闪屏现象,可以使用双缓冲来加载资源显示。 /** * 双缓冲技术实例 * @author b * */ public class Surfaceview extends View implements Runnable{ //要绘制的图片 private Bitmap mBitmap; private Paint paint; //内存一块缓冲区 private Bitmap sBitmap; private Canvas canvas; public Surfaceview(Context context) { super(context); //加载图片到内存 mBitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.b122)).getBitmap(); //创建指定大小的缓冲区域 sBitmap = Bitmap.createBitmap(1000, 800, Bitmap.Config.ARGB_8888); canvas = new Canvas(); // canvas.setBitmap(sBitmap); paint = new Paint(); //把图片绘制在缓冲区域上 canvas.drawBitmap(mBitmap, 0, 0, paint); new Thread(this).start(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //将缓冲区域的图片显示出来 canvas.drawBitmap(sBitmap, 0, 0, paint); } @Override public void run() { while(!Thread.currentThread().isInterrupted()){ try { Thread.sleep(100); } catch (Exception e) { Thread.interrupted(); } postInvalidate(); } } }