When developing a project, it takes a lot of time to obtain images from the Internet and then generate bitmap. The card will be very difficult during the sliding process. This seriously affects the user experience.
The articles on the listview page are searched online. Now let's take a look at how to implement asynchronous loading.
Public class asyncimageloader {
// Image Cache
Private Map <string, softreference <bitmap> imagecache = new hashmap <string, softreference <bitmap> ();
Public bitmap loaddrawable (final string imageurl, final imagecallback callback ){
// If the uploaded image has already generated an image object
If (imagecache. containskey (imageurl )){
Softreference <bitmap> softreference = imagecache. Get (imageurl );
If (softreference. Get ()! = NULL ){
Return softreference. Get ();
}
}
// Generate a handler object to receive messages
Final handler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
Callback. imageloaded (Bitmap) msg. OBJ, imageurl );
}
};
// Load data in a new thread
New thread (){
Public void run (){
Bitmap bitmap = getbitmap (imageurl );;
// Drawable = loadimagefromurl (imageurl );
Imagecache. Put (imageurl, new softreference <bitmap> (Bitmap ));
Handler. sendmessage (handler. obtainmessage (0, bitmap ));
};
}. Start ();
Return NULL;
}
// Callback function
Public interface imagecallback {
Public void imageloaded (Bitmap imagedrawable, string imageurl );
}
// Return an image through URL
Public bitmap getbitmap (string URL ){
Bitmap bitmap = viewweb. vieweb. gethttpbitmap (URL );
Bitmap newbit = bitmapfactory. decoderesource (viewweb. vieweb. getresources (), R. drawable. test );
If (Bitmap! = NULL ){
Newbit = viewweb. vieweb. getscaleimg (bitmap, 80, 80 );
}
Return newbit;
}
}
Write in listviewadapter as follows:
Class listviewadapter extends baseadapter {
// Number of data displayed by a listview
Int COUNT = 10;/* starting amount */
// Asynchronous image loading thread
Private asyncimageloader imageloader = new asyncimageloader ();
@ Override
Public int getcount (){
Return count;
}
@ Override
Public object getitem (int pos ){
Return Pos;
}
@ Override
Public long getitemid (int pos ){
Return Pos;
}
// Current Cache
Private Map <integer, View> viewmap = new hashmap <integer, View> ();
@ Override
Public View getview (INT POs, view V, viewgroup group ){
// Cache
View rowview = viewmap. Get (POS );
// If NO content exists
If (rowview = NULL ){
Tablelayout table = new tablelayout (viewweb. This );
Tablerow layout = new tablerow (viewweb. This );
Layout. setorientation (linearlayout. Horizontal );
Try {
Page = allpagedata [POS];
} Catch (exception e ){
Toast. maketext (getapplicationcontext (), "ended", Toast. length_short). Show ();
}
If (Pos> allpagedata. Length-1 ){
Pos = allpagedata. Length-1;
}
// Overall layout on the right
View Right = layoutinflater. Inflate (R. layout. Right, null );
Right. setpadding (20, 10, 10, 10 );
// Final imageview = (imageview) Right. findviewbyid (R. Id. synjd );
// Single-thread display for Image Display
Final imageview = new imageview (viewweb. This );
Imageview. setbackgroundresource (R. drawable. Wait );
Imageloader. loaddrawable (page. getimg (), new imagecallback (){
Public void imageloaded (Bitmap imagedrawable, string imageurl ){
Imageview. setbackgroundresource (0 );
Imageview. setimagebitmap (imagedrawable );
Imageview. setpadding (10, 10, 10, 10 );
}
});
// Obtain the title
Textview Title = (textview) Right. findviewbyid (R. Id. firsttitle );
Title. settext (page. gettitle ());
// Obtain the environment
Textview HJ = (textview) Right. findviewbyid (R. Id. huangjingid );
Hj. settext (page. gethj ());
// Service
Textview fw = (textview) Right. findviewbyid (R. Id. fuwuid );
FW. settext (page. getfw ());
// Taste
Textview kW = (textview) Right. findviewbyid (R. Id. kouweiid );
KW. settext (page. getkw ());
// Price
Textview JG = (textview) Right. findviewbyid (R. Id. jiageid );
JG. settext (page. getjg ());
// Overall score
Textview zhdf = (textview) Right. findviewbyid (R. Id. zhdfid );
String df = page. getzhdf ();
Zhdf. settext (DF );
Double score = double. parsedouble (DF );
// Determine the number of complete stars
Int fullstar = (INT) score/1;
// Determine whether half of the stars exist
Boolean hasharf = false;
If (score % 1! = 0 ){
Hasharf = true;
}
// For 7 imageviews
Int [] imageindex = {R. id. image0, R. id. image1, R. id. image2, R. id. image3, R. id. image4, R. id. image5, R. id. image6, R. id. image7 };
// The eight imageviews of the star
Imageview imageviews [] = new imageview [imageindex. Length];
For (INT I = 0; I <imageviews. length; I ++ ){
Imageviews [I] = (imageview) Right. findviewbyid (imageindex [I]);
If (I <= fullStar-1 ){
Imageviews [I]. setbackgroundresource (R. drawable. Star );
} Else if (hasharf & I = fullstar ){
Imageviews [I]. setbackgroundresource (R. drawable. harfstar );
} Else {
Imageviews [I]. setbackgroundresource (R. drawable. nostar );
}
}
// Draw a line
Layout. addview (imageview, rowp );
Layout. addview (right, rowp );
// Draw the entire table
Table. addview (layout, tablep );
// Final string url = harfurl + "id =" + Page. getsid () + "& mobile = OK & Output = xml ";
// Pass the current position to the past
Final string Index = string. valueof (POS );
Layout. setonclicklistener (New onclicklistener (){
@ Override
Public void onclick (view v ){
// Upload the detailed information page
Intent intent = new intent (viewweb. This, viewwebdetail. Class );
Intent. putextra ("Index", index );
Startactivity (intent );
}
});
// Put both the location and view in the cache
Viewmap. Put (Pos, table );
Return table;
} Else {
// If yes, it will directly return
Return rowview;
}
}
}
Now I understand how to do the east and west plus cache, as well as the importance of asynchronous implementation to the program.