Original from: Miroslav | http ://fangjie.sinaapp.com/?p=193   reprint please specify the source
Final Effect Demo: http://fangjie.sinaapp.com/?page_id=54
The project code has been put into Github:https://github.com/jayfang1993/sinaweibo
I. Picture of a ListView asynchronous loading
We all know that there are user avatars for every Weibo item, and each tweet may also have pictures. If loading the list at the same time load the picture, so there are several shortcomings, the first very troublesome, the interface stuck, the user experience is not good, the second Android in the main thread can not have network operation, it is very troublesome to implement itself. That's why we implemented a picture asynchronous load class. Inherit from Asynctask<string, Void, Bitmap>, overloaded with methods. Doinbackground is the real asynchronous operation, do some time-consuming tasks, here is to download the image on the server, OnPostExecute is called at the end of the Doinbackground, and passed in Doinbackground return value.
Public Asyncimageloader (ImageView image, Lrucache<string, bitmap> lrucache,int width,int height) { super (); C2/>this.image = image; This.lrucache = LruCache; This.width=width; this.height=width; } @Override protected Bitmap doinbackground (String ... params) { Bitmap Bitmap = null; Bitmap = Getuserinfo.getbitmap (Params[0]); if (width!=0&height!=0) bitmap=getuserinfo.scaleimg (bitmap, width, height); Addbitmaptomemorycache (Params[0], bitmap); return bitmap; } @Override protected void OnPostExecute (Bitmap Bitmap) { image.setimagebitmap (Bitmap); }
Then encapsulate an interface LoadBitmap in Weiboadapter and load the picture.
Two. ListView Picture Cache
The ListView often slides up and down, and these picture loading tasks are called repeatedly, which can be wasteful. Can be cached for the picture, when a picture has been loaded and needs to be displayed again only need to be taken out of memory to show it, do not need to load. The cache actually has two kinds of caches, one is the memory cache, the other is the SD card cache, that is, the download image to the SD card. Here we only talk about memory caches.
Combined with the above image asynchronous loading, the whole picture shows the process is: when the need to display the picture, first go to the memory to see if there is a cache of this image, and some words directly displayed, no words, go to asynchronous loading, and then save to the memory cache, and then display.
full Asyncimageloader.java code
Package Com.fangjie.weibo.util;import Android.graphics.bitmap;import Android.os.asynctask;import Android.support.v4.util.lrucache;import Android.widget.imageview;public class Asyncimageloader extends AsyncTask <string, Void, bitmap> {private ImageView image; Private lrucache<string, bitmap> LruCache; private int width; private int height; /** * Construction method, the ImageView control and LruCache objects need to be passed in * @param image to load the image to this {@code}imageview * @param lruCache The object of the cached picture */Public Asyncimageloader (ImageView image, Lrucache<string, bitmap> lrucache,int width,int height) { Super (); This.image = image; This.lrucache = LruCache; This.width=width; This.height=width; } @Override protected Bitmap doinbackground (String ... params) {Bitmap Bitmap = null; Bitmap = Getuserinfo.getbitmap (Params[0]); if (width!=0&height!=0) bitmap=getuserinfo.scaleimg (bitmap, width, height); Addbitmaptomemorycache (Params[0], bitmap); return bitmap; } @Override protected void OnPostExecute (Bitmap Bitmap) {image.setimagebitmap (BITMAP); }//Call LRUCache's Put method to add the image to the memory cache, to give the picture a key to facilitate the next time out from the cache private void Addbitmaptomemorycache (String key, BITM AP Bitmap) {if (Getbitmapfrommemorycache (key) = = null) {Lrucache.put (key, bitmap); }}//Call LRUCache's Get method to go from memory cache to picture public Bitmap Getbitmapfrommemorycache (String key) {return L Rucache.get (key); } }
The calling interface in Weiboadapter, LoadBitmap
Private final int maxmemory = (int) runtime.getruntime (). MaxMemory ();//Gets the maximum memory allocated by the current application private final int cacheSize = maxmemory/5;//only a 5-point one to do the picture cache private Lrucache<string, bitmap> Mlrucache = new lrucache<string, bitmap> ( cacheSize) {protected int sizeof (String key, Bitmap Bitmap) {//Replication SizeOf () method//replaced by GetByteCount () in API return Bitmap.getrowbytes () * bitmap.getheight ()/1024; Here is how many KB to count}}; /** * * @param urlstr The URL of the image to be loaded in string form, you can use this URL as the key to the cached image @param image ImageView control */private void LoadBitmap ( String urlstr, ImageView image,int width,int height) {System.out.println (URLSTR); Asyncimageloader Asyncloader = new Asyncimageloader (image, Mlrucache,width,height);//What an asynchronous picture loads an object bitmap bitmap = Asyncloader.getbitmapfrommemorycache (URLSTR);//First get picture if (bitmap! = null) {Image.setimagebitmap (bitmap) from memory cache;// If this picture is present in the cache, it is set directly to ImageView} else {Image.setimageresource (r.drawable.user_head);//Otherwise set the default picture Asyncloader.execute (URLSTR);//Then perform the asynchronous task Asycntask to load the picture online}}
At this point, the Weibo page of the microblog list is loaded and completed. There are a lot of small points are not mentioned, but there are source code, I believe we can read. The entire project document:
The code is put in: Http://git.oschina.net/fangjie/Sina-Weibo Effect show: http://fangjie.sinaapp.com/?page_id=54
Note : Due to the application of Sina Weibo development platform application is not approved, so not all the Weibo account can be authorized success, need to test, you can find me, You can also change the source code (COM.FANGJIE.WEIBO.UTIL.AUTHUTIL) to the appkey,appsecret you are applying for.
Weibo client projects have time to write down!
&NBSP; welcome attention to my personal site: http://fangjie.sinaapp.com/&NBSP;