Source: Miroslav |
p=193 "style=" Color:rgb (202,0,0); Text-decoration:none; font-size:14px; Font-family:georgia, ' Times New Roman ', ' bitstream Charter ', Times,serif; line-height:21px ">http://fangjie.sinaapp.com/?p=193 Reprint Please specify the source
Finally, the results show:
page_id=54 "href=" http://fangjie.sinaapp.com/?
Page_id=54 ">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 each Weibo item is a real user picture, and each Weibo may also have a picture. Assume that you load the picture at the same time as the loading list. This has a few shortcomings, the first very troublesome, interface stuck, the user experience is very bad, the second Android in the main thread can not have network operation, so it is very troublesome to implement itself. That's why we implemented a picture asynchronous load class. Inherited from Asynctask<string, Void, Bitmap>, overloaded methods.
Doinbackground is the real asynchronous operation. To do some time-consuming tasks, this is to download the image on the server, OnPostExecute is called after the end of Doinbackground. and pass in the return value of Doinbackground.
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 will often swipe up and down, and these images will be called repeatedly when loaded into the task. This is very wasteful. The ability to cache images, once a picture has been loaded, you need to show it just to show it out of memory. There is no need to load it again. There are actually two caches of caching. One is the memory cache. There is also an SD card cache, that is, to download images to the SD card. Here we just talk about the memory cache.
In conjunction with the above image loading asynchronously, the whole picture is displayed as follows: When the image needs to be displayed, go to the memory to see if there is a cache of such images. Some words are directly displayed, no words are loaded asynchronously, and then saved 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 the LruCache object need to be passed in * @param image loads the image into this {@code}imageview * @param lruCache The object that cached the picture */Publ IC 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 the Put method of LRUCache to increase the picture in the memory cache. To give this image a key to easily be removed from the cache next time private void Addbitmaptomemorycache (String key, Bitmap Bitmap) {if (Getbitmapfromme Morycache (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;//is just a 5-point one used to do the picture cache private Lrucache<string, bitmap> Mlrucache = new lrucache<string, bitmap& gt; (cacheSize) {protected int sizeOf (String key, Bitmap Bitmap) {//Replication SizeOf () method//Repla CED by GetByteCount () in API return Bitmap.getrowbytes () * bitmap.getheight ()/1024; Here is how many KB to count}}; /** * * @param the URL of the picture that URLSTR needs to load. As a string, this URL can be used as a key to the cached image @param image ImageView control */private void LoadBitmap (String urlstr, ImageView image,int W Idth,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;// Assuming that the image exists 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 run the asynchronous task Asycntask to load the image on the Web}}
At this point, the microblog list on the Weibo page is loaded and all finished.
There are very many small points are not mentioned, but there are source code, I believe we can understand. The entire project file:
The code is put in: Http://git.oschina.net/fangjie/Sina-Weibo effect display:
Page_id=54 ">http://fangjie.sinaapp.com/?page_id=54
Note : Because Sina Weibo development platform application is not approved, so not all of the Weibo account can be authorized success, need to test the words 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 project have time to write down again!
You are welcome to follow my personal website:http://fangjie.sinaapp.com/
Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.
Android Sina Weibo client (vii)--listview pictures asynchronous load, cache