How to optimize the paint () function

Source: Internet
Author: User

We often encounter such problems. The program that runs well on the simulator is very slow on the actual mobile phone, or even fails to run. This is mostly due to improper use of the re-painting mechanism, the paint () function in j2's plays the role of painting the screen. It is often called frequently when animation is required, and on some low-end mobile phones or mobile devices, for the paint () function () the frequent call of a function may cause problems such as long time or even abnormal operation. Therefore, optimization of the paint () function is particularly important. Here, we propose several methods and precautions for optimizing the paint () function. for programming, you can refer:
1. Do not use time-consuming operations in the paint () function, such:
Try {
Image image_Splash = Image. createImage ("/back.png ");
G. drawImage (image_Splash, 0, 0, Graphics. TOP | Graphics. LEFT );
}
Catch (Exception ex ){
}
The image creation method is time-consuming because I/O operations are required, so we should avoid it as much as possible. The creation operation is only performed once during initialization.
2. use pictures with smaller pixels. Generally, books are used, and images with less colors are displayed for less time. Therefore, try to avoid using pictures with more than 256 colors, excessively slow images often require a variety of pixels to meet your needs. Therefore, using cartoon images or converting them to 256 or 16 colors will reduce the drawing time.
3. partial Refresh: You must call the paint () method for each repaint (), instead of re-painting the entire Screen for every repaint, therefore, sometimes you need to re-draw some screens. There are two ways to achieve this. The first method is to use Clip, which is the drawing area of a rectangle. When re-painting, you can only repaint the content within this area, regardless of other parts, so you can save operations and set the drawing area using functions such as setClip.
In addition, you can use repaint (x, y, width, height) to re-draw a specified region. This function actually sets a specific re-painting Clip (), however, it is more convenient to use.
4. Reduce the number of repaint () times and try to reproduce the painting only when it works.
5. Add some of the content of the paint () function to the outside, so that the re-painting reduces the calculation amount and operation.
6. Using Dual-Cache Technology, some devices support dual-Cache Technology to determine whether the device supports dual-Cache. You can use the isDoubleBuffered () method of the Canvas class. To achieve dual-cache, you can use the Image-class variable Image technology. For example, use:
Image = Image. createImage (width, height );
Graphics g = image. getGraphics ();

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.