I. Descriptive narrative of the problem |
In the previous series, we used the volley and xutil frameworks to implement the cache loading of images, and then we'll cover the use of the afinal framework.
Afinal is an Android HTTP framework, SQLite ORM, and IOC framework. make it more easy to use, Afinal's purpose is concise, high-speed. After the way of contract configuration, try one line of code complete all things, code intrusion is small, in the three is recommended. Here we mainly use the HTTP framework of the FINALHTTP and Finalbitmap components to implement network image loading
Case Introduction-- To achieve a picture news tour:
Ii. main components of the case |
1. Finalhttp Usage : finalhttp fh=new finalhttp ();
(1) Send post mode request
Fh.post (URL, new ajaxcallback<string> () { @Override public void OnFailure (Throwable t, String strMsg) {// Request failed call Super.onfailure (T, STRMSG); } @Override public void onloading (Long count, long current) {//The request process does not have a second callback super.onloading (count, current); } @Override public void OnStart () {//starts an asynchronous request when Super.onstart () is called; } @Override public void onsuccess (String t) {//Request successful invocation and accept return result });
(2) Send Get method request
Fh.get (URL, callBack), using the same post method
(3) Download file
The method parameters represent the URL of the downloaded file, the file save target, the Ajaxcallback callback method, respectively.
Fh.download (URL, "/mnt/sdcard/21-sun.apk", new Ajaxcallback<file> () { @Override public void Onloading ( Long count, long current) { //TODO auto-generated method stub tvprocess.settext ("Download Progress" + (Current/count)); } @Override public void onsuccess (File f) {//requests a successful call and accepts the return result Tvfilename.settext (f==null?"": F.getabsolutefile (). toString ()); } );
2. Pass the number of references to the service side
Ajaxparams params=new ajaxparams ();//Set Request reference Params.put ("category", "Today");
Call Fh.get (Url,params,ajaxcallback) or Fh.post (Url,params,ajaxcallback) method to pass data
JSP Service side
by Request.getparameter ("category"), obtaining text references
You can also upload files
Params.put ("Profile_picture", New File ("/mnt/sdcard/head.jpg")) or
Params.put ("Profile_picture", InputStream);
Service side
Uploads can be implemented using the Commfileupload component
3. Finalbitmap cache and asynchronously load network pictures
Create Finalbitmap, and set the location of the file cache, the percentage of memory cache (e.g. 1/8 of system memory) Finalbitmap fb=finalbitmap.create (this, Diskcachepath, memorycachesizepercent); To configure. Do not set fb.configloadingimage (R.DRAWABLE.DEFAULT_BIG);//Set the picture to display when the picture is loading fb.configloadfailimage ( R.DRAWABLE.ERROR_BIG)//Set picture to display when picture loading fails
The configuration methods are also:
Configbitmapmaxheight (int bitmapheight)//Configure the maximum height of the default picture configbitmapmaxwidth (int bitmapwidth)//Configure the maximum width of the default picture Configdisplayer (Displayer displayer)//Set up the display, for example, to display the animation during the display, etc.
Last Call display () to complete the loading of the picture:
<span style= "White-space:pre" ></span>//the first parameter represents the UI for displaying the picture. The second parameter is the picture network address
<span style= "White-space:pre" ></span>fb.display (View,url);//load the picture. First loaded from the cache, the memory is not loaded from the network
Three, the case complete code |
1. Sunnewsapplication Components
public class Sunnewsapplication extends application { private finalbitmap fb; @Override public void OnCreate () { fb=finalbitmap.create (this); Fb.configloadingimage (R.DRAWABLE.DEFAULT_BIG)/// Set picture to display when the picture is loading } public Finalbitmap Getfinalbitmap () { return fb; }}
2. Write the adapter
public class Morestylenewslistviewadapter extends Baseadapter {private Activity mactivity; Private list<newsitem> newslist; Private Finalbitmap Imageloader; Public Morestylenewslistviewadapter (Activity mactivity,list<newsitem> newslist) {this.mactivity=mactivity; This.newslist=newslist; Imageloader= ((sunnewsapplication) mactivity.getapplication ()). Getfinalbitmap (); } private final int type_count=2; /** * Return data item display type Data * 0 1 2 */@Override public int getitemviewtype (int position) {//TOD O auto-generated method stub return newslist!=null?Newslist.get (position). GetStyle ():-1; }/** * Returns the number of types */@Override public int getviewtypecount () {//TODO auto-generated method stub return type_count; } @Override public int getcount () {//TODO auto-generated Method Stub log.d ("Jereh", "GetCount ()") ; return Newslist.size (); } @Override public Object getItem (int position) {//TODO auto-generated Method Stub log.d ("Jereh", "ge TItem () "); return Newslist.get (position); } @Override public long getitemid (int position) {//TODO auto-generated Method Stub log.d ("Jereh", "ge Titemid () "); return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated Me Thod stub viewholder holder=null; NewsItem Item=newslist.get (position); if (convertview==null) {holder=new viewholder (); Convert Layout.xml to viewSwitch (Item.getstyle ()) {case 0:convertview=layoutinflater.from (mactivity). Inflate (r.layout.new S_ITEM1, NULL); holder.ivimg1= (ImageView) Convertview.findviewbyid (r.id.ivnewsimg); Break Case 1:convertview=layoutinflater.from (mactivity). Inflate (r.layout.news_item2, NULL); holder.ivimg1= (ImageView) Convertview.findviewbyid (R.ID.IVIMG1); Holder.ivimg2= (ImageView) Convertview.findviewbyid (R.ID.IVIMG2); holder.ivimg3= (ImageView) Convertview.findviewbyid (R.ID.IVIMG3); Break } holder.tvtilte= (TextView) Convertview.findviewbyid (r.id.tvtitle); Convertview.settag (holder);//Record a logo}else{holder= (viewholder) Convertview.gettag (); }//Bind data to UI elements Holder.tvTilte.setText (Item.gettitle ()); Imageloader.display (HOLDER.IVIMG1, Item.getimgurl () [0]);//load the picture. First loaded from the cache, the memory is no longer loaded from the network switch (itEm.getstyle ()) {case 1:imageloader.display (Holder.ivimg2, Item.getimgurl () [1])//Load picture, first load from cache, inside The Imageloader.display (HOLDER.IVIMG3, Item.getimgurl () [2]) is loaded from the network and loaded into the cache, and the memory is not loaded from the network Break } log.d ("Jereh", "GetView ()"); return convertview; } private class viewholder{private TextView tvtilte; Private ImageView IVIMG1; Private ImageView IvImg2; Private ImageView IVIMG3; }}
3 , writing maintactivity
public class Mainactivity extends Activity {private Radiogroup rgchannel; Private list<newsitem> newslist=new arraylist<newsitem> (); Private Morestylenewslistviewadapter adapter; Private ListView Newslistview; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_home); Initview (); RequestData (); } private void Initview () {rgchannel= (Radiogroup) Super.findviewbyid (R.id.rgchannel); Rgchannel.check (R.id.rbtoday); newslistview= (ListView) Super.findviewbyid (r.id.lvnews); Adapter=new Morestylenewslistviewadapter (this,newslist); Newslistview.setadapter (adapter); /** * Asynchronous request for network data */private void RequestData () {String url= "http://192.168.0.10 7:8080/21-sun/newslistservlet "; Finalhttp fh=new finalhttp (); Ajaxparams params=new ajaxparams ();//Setup pleaseNumber of Params.put ("category", "Today"); Fh.post (URL, params,new ajaxcallback<string> () {@Override public void onfailure (Throwable t, S Tring STRMSG) {//Request failed call//TODO auto-generated Method Stub log.d ("Jereh", STRMSG); } @Override public void onsuccess (String t) {//request successfully called. and accept the return result//TODO auto-generated method stub Gson gson=new Gson (); List List=gson.fromjson (T, new typetoken<arraylist<newsitem>> () {}.gettype ()); Newslist.addall (list); Adapter.notifydatasetchanged (); } }); }
Small partners who want to know a lot of other content can click to view the source code and perform the test in person.
Inquiries or technical exchanges, please add the official QQ Group: (452379712)
Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article belongs to Yantai Jerry Education Technology Co., Ltd. and CSDN co-owned, welcome reprint. However, this statement must be retained without the author's permission. And in the article page obvious location to give the original text connection. Otherwise, the right to pursue legal liability is retained.
Android batch images loaded into the classic series--afinal framework to implement asynchronous cache loading of pictures