Android dual-buffer plotting to avoid screen flickering

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.