Android Bitmap processing Bitmaps Off the UI Thread (slag translation)

Source: Internet
Author: User

Two: UI thread plus load image

1. Using Asynctask
The Asynctask class provides a simple background processing method and returns the results to the UI thread.
It is proposed in the thread to use weakreference<?> to cache UI controls that display bitmap, such as: ImageView. WeakReference ensures that the resource collector can recycle UI control resources, such as when the view resource is reclaimed by the ListView, even when the thread is operating.
Ex

 ImageView ImageView = new  layoutparams (100 //  initialize WeakReference  weakreference<imageview> reference = new  weakreference<imageview>  //  get WeakReference Store content  if  (Reference! = null  && reference.get ()! =   = Reference.get (); 

2. Handling concurrent Operations (multi-line preempted)
Some common components, such as the ListView and GridView, advocate another mainstream practice, when components are combined with Asynctask to display content, in order to effectively utilize memory, the component reclaims the Child View resource (beyond the display area's child view) after the user scrolls the component.
If each sub-view triggers a asynctask, there is no guarantee that when the asynctas of the child view is complete, the child view is reused by the other child view if it has not been reclaimed by the resource collector. And there is no guarantee that the asynchronous threads will start in the same order as they were completed.
Then do this:
You can also use WeakReference to create an bitmapdrawable inheriting class (assuming asyncdrawable) to store references to Asynctask. Before you execute Asynctask, you can create asyncdrawable and bind to ImageView.
A checksum is required before the previous step (if the current ImageView is a reused resource and the loaded image resource is consistent, there is no need to turn on the thread to load the same image resource), through the image resource's flags (such as ID, URI) and ImageView (such as drawable), determine in the checksum whether to cancel the Asynctask (call the Cancel () method).
Finally, in Asynctask, you need to look at a judgment, when the thread iscancelled (), ends the OnPostExecute operation and returns the result, forcing the end to reuse the thread in ImageView.

Android Bitmap processing Bitmaps Off the UI Thread (slag translation)

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.