"Turn" Android Drawingcache

Source: Internet
Author: User

Transferred from: http://magiclen.org/android-drawingcache/

Publication date: August 27, 2014 | Magic Len

When you launch Android, in a passionate situation, you'll use the Getdrawingcache method of view to get a view of what's currently shown (Drawingcache), although it's a somewhat convenient way, but there are many flaws in this approach, It's not only very inefficient, but the internal reality and the results of the return are quite different with the Android API version. The most serious point is that Getdrawingcache often invites you to eat null. In this article, you'll explore why Getdrawingcache will return null, and how to solve this problem.

The relationship between setdrawingcacheenabled, Builddrawingcache and Getdrawingcache

On the Android SDK, all the view has setdrawingcacheenabled, Builddrawingcache and Getdrawingcache three methods, which look at the undesired of similar methods in the end what kind of emotion correcting ge? Let's continue watching.

Most of the view, if you do not use the Setdrawingcacheenabled method to activate the view of the Drawingcache function, then the preset is not used. To use Drawingcache, when using the Getdrawingcache method, you will first call the Builddrawingcache method to establish the Drawingcache, and then return the results, without activating Drawingcache, When using the Getdrawingcache method, the result of the last use of the Builddrawingcache method is returned, if there were no previous builddrawingcache to establish Drawingcache, Then Getdrawingcache will return null, of course, even if there is no drawingcache, it can be used in advance to establish Drawingcache Builddrawingcache, Avoid getdrawingcache return null.

The latest drawingcache of the view can be obtained in a few two ways:

... view.setdrawingcacheenabled (true);.. Bitmap Drawingcache = View.getdrawingcache ();
... view.builddrawingcache (); Bitmap Drawingcache = View.getdrawingcache ();

After the Drawingcache, do not call the Builddrawingcache method, the following method should be avoided, resulting in the creation of the Drawingcache twice:

... view.setdrawingcacheenabled (true); View.builddrawingcache (); Bitmap Drawingcache = View.getdrawingcache ();

At the same time that Drawingcache was established using the Builddrawingcache method, the Android SDK preset will recycle out the last Drawingcache. So you don't have to be smart before using the Builddrawingcache method, or before using the Getdrawingcache method in Drawingcache, to give the previous drawingcache to the hand recycle, If you do this, you will be recycle the rumtimeexception of the repetitive. So the following notation should also be avoided:

... if (view.getdrawingcache () = null) {View.getdrawingcache (). recycle ();;} View.builddrawingcache (); Bitmap Drawingcache = View.getdrawingcache ();
Why is Getdrawingcache performance bad?

At the beginning of the article, it was mentioned that the effectiveness of getdrawingcache is very poor, why? As mentioned above, once the Drawingcache is initiated, each call to Getdrawingcache will automatically re-call the Builddrawingcache method to create a new drawingcache, but in most cases The state of a view is not arbitrarily altered, and if used in an event of the OnDraw, it will getdrawingcache the performance very low. Again, the higher the Android API level, the higher the quality of the Drawingcache, and in most cases, the slowest argb_ to use the most memory, and the fastest. 8888, the Setdrawingcachequality method provided by the past view has no physical effect, regardless of the quality of the set, will still use argb_8888.

Why Getdrawingcache often return null?

If you encounter a Getdrawingcache method that returns NULL, make sure that the view's Drawingcache is not available, and if not, then make sure there is no call Builddrawingcache method. If the above is correct, and then see if the use of Getdrawingcache is wrong, the view must be through the measure and layout of the process before it can be drawn. In the example below, Drawingcache can be obtained even if the view is not directly added to the activity or fragment Rootview:

... ViewGroup viewgroup = new Framelayout (GetContext ()); ImageView image = new ImageView (GetContext ()); Image.setimageresource (R.drawable.ic_launcher); Viewgroup.addview (image); Viewgroup.measure ( Measurespec.makemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec (0, MeasureSpec.UNSPECIFIED)) ; viewgroup.layout (0, 0, viewgroup.getmeasuredwidth (), Viewgroup.getmeasuredheight ()); Viewgroup.builddrawingcache ( ); Bitmap Bitmap = Viewgroup.getdrawingcache ();

If it is true that the view has been measure and layout and there is a call to Builddrawingcache (either by itself or by hand), but the getdrawingcache still returns NULL, That's because the drawingcache is too big to be drawn, and it's over the drawingcachesize of the Android system, so the system doesn't give the painting! When this happens, you can only abandon the use of drawingcache, and in fact, this situation is somewhat Chang.

The Drawingcache size limit for Android system presets is different on different devices, and may even be several times worse, if you want to see the numbers, you can use the following methods in the Android SDK to get Drawingcachesize:

Viewconfiguration.get (context). Getscaledmaximumdrawingcachesize ();
An alternative to using Getdrawingcache

The article saw here that everyone should be able to understand that Getdrawingcache is very difficult to use, in that case, then completely abandoned the android built-in Drawingcache machine! In fact, it is not too difficult to actually make the same kind of function, the general concept is to build a bitmap on its own, and use a canvas to draw on this bitmap, as long as you adjust the drawing method of the view and import your own canvas as a reference, The results will appear on the bitmap. The program can be written as follows:

 123456789101112131415161718192021 
public Bitmap getmagicdrawingcache (view view) {Bitmap Bitmap = (Bitmap) view.gettag (Cachebitmapkey); Boolean dirty = (Boolean) View.gettag (cachebitmapdirtykey); int viewwidth = View.getwidth (); int viewheight = View.getheight (); if (bitmap = = NULL | | bitmap.getwidth ()! = Viewwidth | | bitmap.getheight ()! = Viewheight) {if (Bitmap! = Null &&!bitmap.isrecycled ()) {bitmap.recycle ();} Bitmap = Bitmap.createbitmap (Viewwidth, Viewheight, bitmap_quality), View.settag (Cachebitmapkey, bitmap);d Irty = true;} if (dirty = = True | |!quick_cache) {bitmap.erasecolor (color_background); Canvas canvas = new canvas (bitmap); View.draw (canvas); View.settag (Cachebitmapdirtykey, false);}
  return bitmap;} 

Where Cachebitmapkey and Cachebitmapdirtykey are the same integer values, the tag ID of the view is specified. The location of the Cachebitmapkey will be stored using this method to establish the location of the Drawingcache;cachebitmapdirtykey to store this view Drawingcache whether it has been dirty (dirty) The draw method to call view is re-drawn. The bitmap used by Drawingcache is only re-established when no bitmap objects or bitmap objects are of the size and size of the view, and the previous bitmap will be bitmap before a new recycle is established. The new Bitmap object will be credited to the view's tag again. If the Quick_cache is set to False, the Drawingcache is not dirty, but it is only necessary to do so when the view is constantly changing. Bitmap_quality can be set as Bitmap.Config.RGB_565 or bitmap.config.argb_8888,bitmap.config.argb_4444 already with Android The higher the API level, the more slowly it is disabled, and in fact the rgb_565 is not transparent, but the performance will be much better than the argb_8888.

If you want to add a sentence that is not in the activity or fragment Rootview, you can write the following program:

12345678910111213141516171819202122232425
public Bitmap getmagicdrawingcache (view view) {Bitmap Bitmap = (Bitmap) view.gettag (Cachebitmapkey); Boolean dirty = (Boolean) View.gettag (Cachebitmapdirtykey), if (View.getwidth () + view.getheight () = = 0) {view.measure ( Measurespec.makemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec (0, MeasureSpec.UNSPECIFIED)) ; view.layout (0, 0, view.getmeasuredwidth (), View.getmeasuredheight ());}  int viewwidth = View.getwidth (); int viewheight = View.getheight (); if (bitmap = = NULL | | bitmap.getwidth ()! = Viewwidth | | Bitmap.getheight ()! = Viewheight) {if (bitmap! = null &&!bitmap.isrecycled ()) {bitmap.recycle ();} Bitmap = Bitmap.createbitmap (Viewwidth, Viewheight, bitmap_quality), View.settag (Cachebitmapkey, bitmap);d Irty = true;} if (dirty = = True | |!quick_cache) {bitmap.erasecolor (color_background); Canvas canvas = new canvas (bitmap); View.draw (canvas); View.settag (Cachebitmapdirtykey, false);} return bitmap;} 
Drawingcache actually uses

The Drawingcache is very versatile and can be used to create a view that is not built into the Android SDK, such as the following time-axis view.

"Turn" Android Drawingcache

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.