Optimization of Flash Platform Technology (10) Processing pixels

Source: Internet
Author: User

Use setvector () to plot pixels.
When you draw a pixel, you can use the corresponding method of bitmapdata to perform some simple optimization. One way to quickly draw pixels is to use the setvector () method introduced in Flash Player 10:

// Image dimensions
VaR wdth: Int = 200;
VaR hght: Int = 200;
VaR Total: Int = wdth * hght;
// Pixel colors Vector
VaR pixels: vector. <uint> = new vector. <uint> (total, true );
For (var I: Int = 0; I <total; I ++)
{
// Store the color of each pixel
Pixels [I] = math. Random () * 0 xffffff;
}
// Create a non-transparent bitmapdata object
VaR myimage: bitmapdata = new bitmapdata (wdth, hght, false );
VaR imagecontainer: bitmap = new Bitmap (myimage );
// Paint the pixels
Myimage. setvector (myimage. rect, pixels );
Addchild (imagecontainer );
If you are using a slow method, such as setpixel () or setpixel32 (), use the lock () and unlock () Methods to speed up the operation. In the following code, the lock () and unlock () methods are used to improve performance:
VaR Buffer: bitmapdata = new bitmapdata (200,200, true, 0 xffffffff );
VaR bitmapcontainer: bitmap = new Bitmap (buffer );
VaR positionx: int;
VaR positiony: int;
// Lock update
Buffer. Lock ();
VaR starting: Number = gettimer ();
For (var I: Int = 0; I <2000000; I ++)
{
// Random positions
Positionx = math. Random () * 200;
Positiony = math. Random () * 200;
// 40% transparent pixels
Buffer. setpixel32 (positionx, positiony, 0x66990000 );
}
// Unlock update
Buffer. Unlock ();
Addchild (bitmapcontainer );
Trace (gettimer ()-starting );
// Output: 670
The lock () method of the bitmapdata class can lock the image and prevent the object that references the image from being updated when the bitmapdata object is changed. For example, if a bitmap object references a bitmapdata object, you can lock the bitmapdata object and change it before unlocking. Before the bitmapdata object is unlocked,
The bitmap object is not changed. To improve performance, use this method and the unlock () method before and after multiple calls to the setpixel () or setpixel32 () method. Call lock () and unlock () to prevent unnecessary updates on the screen.
Note: If you are processing pixels (dual buffering) in a bitmap (rather than a display list), this technology sometimes does not improve performance. If the bitmap object does not reference the bitmap buffer, the use of lock () and unlock () will not improve the performance. Flash Player detects the unreferenced buffer and the bitmap is not displayed on the screen. Pixel traversal methods (such as getpixel (), getpixel32 (), setpixel (), and setpixel32 () may be slow, especially on mobile devices. If possible, use the method to retrieve all pixels in one call. To read pixels, use the getvector () method, which is faster than the getpixels () method. In addition, remember to use vector object-dependent APIs as much as possible because they may run faster.

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.