Android Batch Image loading classic series--afinal framework implementation of image asynchronous cache loading

Source: Internet
Author: User
<span id="Label3"></p> <table border="0" style="border:0px solid #000000; width:100%; height:38px; color:#333333; line-height:25px; font-family:Georgia,‘Times New Roman‘,Times,sans-serif; font-size:14px; border-collapse:collapse; background-color:#1570a6"> <tbody> <tr> <td style="border-style:solid; border-color:silver; border-collapse:collapse; padding:3px"><strong><span style="color:#ffffff; font-size:14pt; font-family:‘Microsoft YaHei‘">first, The problem description</span></strong></td> </tr> </tbody> </table><p><p><span style="font-family:Microsoft YaHei; font-size:14px">In the previous series, we used the volley and xutil frameworks to implement cache loading for images, and then we'll cover the use of the afinal Framework.</span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">Afinal is an Android HTTP framework, SQLite orm, and IOC Framework. </span> <span style="font-family:Microsoft YaHei; font-size:14px">make it easier to use, Afinal's purpose is concise, Fast. After the way of the contract configuration, try to do everything in one line of code, code intrusion is small, in the three are Recommended. </span> <span style="font-family:Microsoft YaHei; font-size:14px">Here we mainly use the HTTP framework of the FINALHTTP and Finalbitmap components to implement network image loading</span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">Case introduction--</span> to <span style="font-family:Microsoft YaHei; font-size:14px">achieve a picture news tour:</span></p></p><p><p></p></p> <table border="0" style="border:0px solid #000000; width:100%; height:38px; color:#333333; line-height:25px; font-family:Georgia,‘Times New Roman‘,Times,sans-serif; font-size:14px; border-collapse:collapse; background-color:#1570a6"> <tbody> <tr> <td style="border-style:solid; border-color:silver; border-collapse:collapse; padding:3px"><strong><span style="color:#ffffff; font-size:14pt; font-family:‘Microsoft YaHei‘">Ii. main components of the case</span></strong></td> </tr> </tbody> </table><p><p><span style="font-family:Microsoft YaHei; font-size:14px"> <strong><span style="color:#3366ff">1, finalhttp use method</span></strong> :  </span> <span style="font-family:Microsoft YaHei; font-size:14px">finalhttp fh=new finalhttp ();</span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">  <strong>(1) Send post mode request</strong></span></p></p><pre><pre><pre name="code" class="java"> 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); } called Super.onstart () when @Override public void OnStart () {//starts an asynchronous request ; } @Override public void onsuccess (String t) {//request successful invocation and accept return result });</pre></pre></pre><p><p><strong><span style="font-family:Microsoft YaHei; font-size:14px">(2) Send Get method request</span></strong></p></p><pre><pre><pre name="code" class="java">Fh.get (url, callBack), Use same post mode</pre></pre></pre><p><p><strong><span style="font-family:Microsoft YaHei; font-size:14px">(3) Download File</span></strong></p></p><p><p>  <span style="font-family:Microsoft YaHei; font-size:14px">The method parameters represent the URL of the downloaded file, the file save target, the Ajaxcallback callback method, respectively</span></p></p><pre><pre><pre name="code" class="java">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 ()); } );</pre></pre></pre><p><p><span style="color:#3366ff"><strong><span style="font-family:Microsoft YaHei; font-size:14px">2. Pass parameters to the service side</span></strong></span></p></p><pre><pre><pre name="code" class="java">Ajaxparams params=new ajaxparams ();//set request parameter Params.put ("category", "today");</pre></pre></pre><p><p><span style="font-family:Microsoft YaHei; font-size:14px">Call Fh.get (url,params,ajaxcallback) or Fh.post (url,params,ajaxcallback) method to pass data</span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">  <strong>JSP Service side</strong></span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">by Request.getparameter ("category"); Get text parameters</span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">You can also upload files</span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">Params.put ("profile_picture", new File ("/mnt/sdcard/head.jpg")) or</span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">Params.put ("profile_picture", inputstream);</span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">  <strong>Service side</strong></span></p></p><p><p><span style="font-family:Microsoft YaHei; font-size:14px">Uploads can be implemented using the Commfileupload component</span></p></p><p><p><span style="color:#3366ff"><strong><span style="font-family:Microsoft YaHei; font-size:14px">3. Finalbitmap cache and load network images asynchronously</span></strong></span></p></p><pre><pre><pre name="code" class="java"> 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); configuration, do not set fb.configloadingimage (r.drawable.default_big);//set the picture to display when the image is loading fb.configloadfailimage ( R.drawable.error_big)//set picture to display when image loading fails</pre></pre></pre><p><p><span style="font-family:Microsoft YaHei; font-size:14px">The configuration methods are Also:</span></p></p><pre><pre><pre name="code" class="java">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, such as display animation in the process of display, etc. </pre></pre></pre><p><p><span style="font-family:Microsoft YaHei; font-size:14px">Last Call display () to finish loading the picture:</span></p></p><pre><pre><span style="color:#008000"></span><pre name="code" class="java"><span style= "white-space:pre" ></span>//the first parameter represents the UI that displays the picture, the second parameter is the picture network address</pre><pre name="code" class="java"><span style= "white-space:pre" ></span>fb.display (view,url);//load picture, first load from cache, memory no longer loaded from the network </pre></pre></pre> <table border="0" style="border:0px solid #000000; width:100%; height:38px; color:#333333; line-height:25px; font-family:Georgia,‘Times New Roman‘,Times,sans-serif; font-size:14px; border-collapse:collapse; background-color:#1570a6"> <tbody> <tr> <td style="border-style:solid; border-color:silver; border-collapse:collapse; padding:3px"><strong><span style="color:#ffffff; font-size:14pt; font-family:‘Microsoft YaHei‘">three, The case complete code</span></strong></td> </tr> </tbody> </table><p style="margin-left:30px"><p style="margin-left:30px"><strong><span style="color:#3366ff; font-family:Microsoft YaHei; font-size:14px">1. Sunnewsapplication Components</span></strong></p></p><pre><pre><pre name="code" class="java">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; }}</pre></pre></pre><p style="margin-left:30px"><p style="margin-left:30px"><span style="color:#3366ff"><strong><span style="font-family:Microsoft YaHei; font-size:14px">2. Write the adapter</span></strong></span></p></p><pre><pre name="code" class="java">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", "getItem ()"); 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 view switch (item.getstyle ()) {case 0:convertview=layoutinflater.from (mactivity). Inflate (r.layout.news_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 picture, first load from cache, memory no longer load switch from network (item.getstyle ()) { Case 1:imageloader.display (holder.ivimg2, item.getimgurl () [1]);//load picture, first load from cache, memory no longer loaded from network Imageloader.display (holder.ivimg3, item.getimgurl () [2]);//loading pictures, first loaded from the cache, memory no longer 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; }}</pre></pre><p style="margin-left:30px"><p style="margin-left:30px"> <strong> <span style="color: #3366ff; font-family:microsoft yahei; font-size:14px"> 3, Write maintactivity </span> </strong> </p></p><pre><pre name="code" class="java">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 pleaseParameter 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 Successful call and accept return Result//TODO Auto-generate D method Stub Gson gson=new Gson (); List List=gson.fromjson (t, New typetoken<arraylist<newsitem>> () {}.gettype ()); Newslist.addall (list); Adapter.notifydatasetchanged (); } }); }</pre></pre><p><p></p></p><p><p><span style="font-family:‘Microsoft YaHei‘; font-size:15px">To learn more about the small partners, you can click to <strong><span style="font-size:18px">view the source code</span></strong> , run the test yourself.</span></p></p><p><p><span style="font-family:‘Microsoft YaHei‘; font-size:15px">Inquiries or technical exchanges, Please join the official QQ group: (452379712)<br></span></p></p><p><p><span style="font-family:‘Microsoft YaHei‘; font-size:15px"><br></span></p></p>Jerry Education<br>Source:<span style="color:#000000"><span style="color:#000000"><span style="color:#000000">http://blog.csdn.net/jerehedu/</span></span></span><br><span style="font-family:‘Microsoft YaHei‘; color:#000000"><span style="font-family:‘Microsoft YaHei‘; color:#000000">This article is the copyright of <span style="color:#000000">Yantai Jerry Education Technology co., Ltd.</span> and CSDN common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. </span></span><p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Batch Image loading classic series--afinal framework implementation of image asynchronous cache loading</p></p></span>

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.