How to draw an optimized view on the android UI

Source: Internet
Author: User
For more information about how to optimize the activity startup speed and view rendering speed, see the documentation in this SDK. Android-sdk-windows-1.5_r1/docs/resources/articles/window-bg-speed.html.

After reading this, you will know that Android: windowbackground is too important and affects the rendering efficiency.

Here is another point, not the windowbackground.

In order to improve the rendering speed of various aspects such as scrolling, android can create a cache for each view and use view. builddrawingcache to create a cache for its own view,

This so-called cache is actually a bitmap object. This bitmap object can only have multiple formats, such

Bitmap. config. argb_8888;

Bitmap. config. argb_4444;

Bitmap. config. argb_8888;

Bitmap. config. argb_8888;

Bitmap. config. rgb_565;

The default format is bitmap. config. argb_8888.

The Alpha value. Therefore, you do not need to calculate the Alpha synthesis when creating the image. Secondly, rgb_565 can directly use the optimized memcopy function, which is much more efficient.

Therefore, when using builddrawingcache to create a cache, you can use the rgb_565 format. But how should we develop this format? Builddrawingcache has two versions: builddrawingcache (Boolean) and builddrawingcache (). There is no parameter to set the RGB format. See the source code first:

Public void builddrawingcache (Boolean autoscale ){

If (mprivateflags & drawing_cache_valid) = 0 | (autoscale?

(Mdrawingcache = NULL | mdrawingcache. Get () = NULL ):

(Munscaleddrawingcache = NULL | munscaleddrawingcache. Get () = NULL ))){

If (viewdebug. trace_hierarchy ){

Viewdebug. Trace (this, viewdebug. hierarchytracetype. build_cache );

}

If (config. debug & viewdebug. profiledrawing ){

EventLog. writeevent (60002, hashcode ());

}

Int width = mright-mleft;

Int Height = mbottom-MTop;

Final attachinfo = mattachinfo;

Final Boolean scalingrequired = attachinfo! = NULL & attachinfo. mscalingrequired;

If (autoscale & scalingrequired ){

Width = (INT) (width * attachinfo. mapplicationscale) + 0.5f );

Height = (INT) (height * attachinfo. mapplicationscale) + 0.5f );

}

Final int drawingcachebackgroundcolor = mdrawingcachebackgroundcolor;

Final Boolean opaque = drawingcachebackgroundcolor! = 0 |

(Mbgdrawable! = NULL & mbgdrawable. getopacity () = pixelformat. opaque );

If (width <= 0 | height <= 0 |

(Width * height * (opaque? 2: 4)> // projected bitmap size in bytes

Viewconfiguration. Get (mcontext). getscaledmaximumdrawingcachesize ())){

Destroydrawingcache ();

Return;

}

Boolean clear = true;

Bitmap bitmap = autoscale? (Mdrawingcache = NULL? Null: mdrawingcache. Get ()):

(Munscaleddrawingcache = NULL? Null: munscaleddrawingcache. Get ());

If (Bitmap = NULL | bitmap. getwidth ()! = Width | bitmap. getheight ()! = Height ){

Bitmap. config quality;

If (! Opaque ){

Switch (mviewflags & drawing_cache_quality_mask ){

Case drawing_cache_quality_auto:

Quality = bitmap. config. argb_8888;

Break;

Case drawing_cache_quality_low:

Quality = bitmap. config. argb_4444;

Break;

Case drawing_cache_quality_high:

Quality = bitmap. config. argb_8888;

Break;

Default:

Quality = bitmap. config. argb_8888;

Break;

}

} Else {

Quality = bitmap. config. rgb_565;

}

// Try to cleanup memory

If (Bitmap! = NULL) bitmap. Recycle ();

Try {

Bitmap = bitmap. createbitmap (width, height, quality );

Bitmap. setdensity (getresources (). getdisplaymetrics (). densitydpi );

If (autoscale ){

Mdrawingcache = new softreference <bitmap> (Bitmap );

} Else {

Munscaleddrawingcache = new softreference <bitmap> (Bitmap );

}

} Catch (outofmemoryerror e ){

// If there is not enough memory to create the bitmap cache, just

// Ignore the issue as bitmap caches are not required to draw

// View hierarchy

If (autoscale ){

Mdrawingcache = NULL;

} Else {

Munscaleddrawingcache = NULL;

}

Return;

}

Clear = drawingcachebackgroundcolor! = 0;

}

Canvas canvas;

If (attachinfo! = NULL ){

Canvas = attachinfo. mcanvas;

If (canvas = NULL ){

Canvas = new canvas ();

}

Canvas. setbitmap (Bitmap );

// Temporarily clobber the cached canvas in case one of our children

// Is also using a drawing cache. Without this, the children wowould

// Steal the canvas by attaching their own bitmap to it and bad, bad

// Thing wocould happen (invisible views, incluupted drawings, etc .)

Attachinfo. mcanvas = NULL;

} Else {

// This case shocould hopefully never or seldom happen

Canvas = new canvas (Bitmap );

}

If (clear ){

Bitmap. erasecolor (drawingcachebackgroundcolor );

}

Computescroll ();

Final int restorecount = canvas. Save ();

If (autoscale & scalingrequired ){

Final float scale = attachinfo. mapplicationscale;

Canvas. Scale (scale, scale );

}

Canvas. Translate (-mscrollx,-mscrolly );

Mprivateflags | = drawn;

// Fast path for layouts with no backgrounds

If (mprivateflags & skip_draw) = skip_draw ){

If (viewdebug. trace_hierarchy ){

Viewdebug. Trace (this, viewdebug. hierarchytracetype. Draw );

}

Mprivateflags & = ~ Dirty_mask;

Dispatchdraw (canvas );

} Else {

Draw (canvas );

}

Canvas. restoretocount (restorecount );

If (attachinfo! = NULL ){

// Restore the cached canvas for our siblings

Attachinfo. mcanvas = canvas;

}

Mprivateflags | = drawing_cache_valid;

}

}

After reading it, I understand that there are at least two factors related to drawingcachebackgroundcolor and mbgdrawable.

After setdrawingcachebackgroundcolor (0xffff0000) is set to a non-default color, the created cache is rgb565. You can verify it using the following methods:

Final bitmap cache = mcontent. getdrawingcache ();

If (Cache! = NULL ){

Config CFG = cache. getconfig ();

Log. D (TAG, "----------------------- cache. getconfig () =" + CFG );

This article is transferred fromWww.35java.com

Related Article

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.