When dealing with some complex interfaces, you often need to use view and SurfaceView to process the drawing by yourself. For example, you can use SurfaceView to paste two images and control their left and right translation. The basic code is as follows:
Java code
Canvas c = null;
Try {
C = mSurfaceHolder. lockCanvas (null );
If (c! = Null ){
C. setDrawFilter (mFilter );
C. drawRect (0, 0, c. getWidth (), c. getHeight (), mBGPaint); // draw the background
C. drawBitmap (bm1, 0, 0, null );
C. drawBitmap (bm2, bm1.getWidth (), 0, null); // the second picture is next to the first one.
}
} Finally {
If (c! = Null ){
MSurfaceHolder. unlockCanvasAndPost (c );
}
}
When you control the movement between the left and right sides, you will find that the screen is very flickering and your eyes will be very tired. After the study, we found that this is because the two images are pasted on the screen one by one. If the refresh frequency is high, the screen will flash very much.
The solution is actually very simple. I think of the Method of Developing and solving the problem of drawing flickering under the j2s architecture. First, I need to draw a picture first on a large memory bitmap and then paste it to the screen. Android is actually the same. The solution is as follows:
Java code
Final Bitmap memBm = Bitmap. createBitmap (screenWidth, screenHeight, Bitmap. Config. RGB_565 );
Final Canvas c = new Canvas (memBm );
C. setDrawFilter (mFilter );
C. drawRect (0, 0, c. getWidth (), c. getHeight (), mBGPaint); // draw the background
C. drawBitmap (bm1, 0, 0, null );
C. drawBitmap (bm2, bm1.getWidth (), 0, null); // the second picture is next to the first one.
Canvas render = null;
Try {
Render = mSurfaceHolder. lockCanvas ();
If (render! = Null ){
Render. drawBitmap (memBm, 0, 0, null );
}
} Finally {
If (render! = Null)
MSurfaceHolder. unlockCanvasAndPost (render );
}
MemBm. recycle (); // remember to recycle the memory bitmap
After this processing, the view refresh will become smoother, and the eyes will become much more comfortable.
From the wanghao_happy Column