Android Project development process (ii)--AFINAL usage Brief introduction

Source: Internet
Author: User

This blog post on the "Android Project development process (a)-Create the project," The main introduction of the project used in a very important framework-afinal, because this series of blog focus is the project development process, so here first introduce the several features used in this project:

Afinal Introduction

    • Afinal is an Android-SQLite ORM and IOC framework. It also encapsulates the HTTP framework in Android, making it easier to use;
    • With Finalbitmap, there is no need to consider the problem of Oom when the bitmap is loaded in Android and when the image loading position is misplaced when fast sliding.
    • The purpose of afinal is to be concise and fast. The contract is larger than the configured way. Try to do everything in one line of code.

Four modules of afinal

    • FINALDB module: The ORM Framework in Android, a line of code can be used for additional deletions and checks. Support one-to-many, many-to-one queries.

    • Finalactivity module: The IOC framework in Android, which is fully annotated to enable UI binding and event binding. No need for Findviewbyid and Setclicklistener.

    • Finalhttp module: Encapsulates HTTP data requests through httpclient, and supports AJAX-mode loading.

    • Finalbitmap module: When loading the bitmap through Finalbitmap,imageview, there is no need to consider the phenomenon of image dislocation when the Oom and the Android container quickly slide during the bitmap loading process. Finalbitmap can configure the number of thread preempted threads, cache size, cache path, load display animation, etc. Finalbitmap's memory management uses the LRU algorithm, Not using weak references (after android2.3, Google has not recommended the use of weak references, android2.3 after the forced recovery of soft and weak references, details of the official Android document), better management bitmap memory. Finalbitmap can customize the downloader to extend other protocols to display network pictures, such as FTP. You can also customize the bitmap display, play animations when ImageView display pictures, and so on (default is the gradient animation display).

The following permissions are required to use the Afinal development framework

<uses-permission android:name= "Android.permission.INTERNET"/><uses-permission android:name= " Android.permission.WRITE_EXTERNAL_STORAGE "/>

The above is the afinal on GitHub overall introduction, the following describes the project needs to several functions, because this project is very small, just use a little bit of afinal in the function:

1, annotations can be UI binding and event binding, without the use of Findviewbyid and Setclicklistener methods, do not forget to let the activity inherit finalactivity.

1  Public classTestactivityextendsfinalactivity {2@ViewInject (Id=r.id.bn_click, click= "Testclick") Button Bnclick;3@ViewInject (id=r.id.tv_show) TextView tvShow;4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         //TODO auto-generated Method Stub7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.test_main);9     }Ten      Public voidTestclick (View v) { OneTvshow.settext ("You clicked OK"); A     } -}

Operation Result:               

After clicking OK

2, the use of Finalhttp, the next is to open the hanging, or with just the activity, a little change let us do the network request

Get method: A line of code is done, directly using the Get method to submit the request address, and then in the relevant callback method for the result operation. The above code gives three callback methods

1  Public classTestactivityextendsfinalactivity {2 finalhttp fh;3String url = "Http://xiaohua.hao.360.cn/m/itxt?page=1";4@ViewInject (Id=r.id.bn_click, click= "Testclick") Button Bnclick;5@ViewInject (id=r.id.tv_show) TextView tvShow;6 @Override7     protected voidonCreate (Bundle savedinstancestate) {8         //TODO auto-generated Method Stub9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.test_main); OneFH =Newfinalhttp (); A     } -      Public voidTestclick (View v) { -Fh.get (URL,NewAjaxcallback<string>(){ the @Override -              Public voidOnStart () { -                 //TODO auto-generated Method Stub -                 Super. OnStart (); +Tvshow.settext ("Requesting ..."); -             } +              A @Override at              Public voidOnloading (LongCountLongCurrent ) { -                 //TODO auto-generated Method Stub -                 Super. Onloading (count, current); -                 //This method is executed during the request response and is automatically recalled every 1 seconds -Tvshow.settext (current + "/" +count); -             } in              - @Override to              Public voidonsuccess (String t) { +                 //TODO auto-generated Method Stub -                 Super. onsuccess (t); the Tvshow.settext (t); *             } $             Panax Notoginseng @Override -              Public voidOnFailure (Throwable t,intErrorno, String strMsg) { the                 //TODO auto-generated Method Stub +                 Super. OnFailure (t, Errorno, STRMSG); ATvshow.settext ("Network Exception"); the             } +         }); -     } $}

Post method:

1Ajaxparams params =Newajaxparams ();2Params.put ("username", "testname");3Params.put ("Password", "123456");4Params.put ("Profile_picture",NewFile ("/mnt/sdcard/test.jpg"));//Uploading Files5Params.put ("Profile_picture2", InputStream);//Uploading data streams6Params.put ("Profile_picture3",NewBytearrayinputstream (bytes));//Commit byte stream7 8Fh.post ("http://www.test.com", params,NewAjaxcallback () {...});

①onstart () callback this method at the beginning of the request.

②onsuccess () callback this method when the request succeeds, where "T" is the return result.

③onfailure () callback this method when the request fails.

④onloading () This method is recalled every 1 seconds during the request response.

The results of the implementation are as follows:

  

3, the use of Finalbitmap. Similarly, loading a network picture is a single line of code Fb.display (Imageview,url).

1  Public void loadingimg (View v) {2         Finalbitmap fb = finalbitmap.create (this); 3         Fb.display (ivimg, "http://pic4.nipic.com/20091120/805653_183746006558_2.jpg"); 4 }

Where the Loadingimg method is an annotation-bound event on a button, Ivimg is a ImageView control. When you click the button, a network image is loaded and the results are as follows:

  

Haha, is not very simple, at the same time also very to force Ah!

This project is currently designed to afinal some usage on these few, if you want to know more can be consulted: http://www.oschina.net/p/afinal

In the project opened also involves another very strong tool--jackson, temporarily not introduced, and so on in the project need to be introduced.

With these two tools, our development work has greatly simplified a lot, and performance is no better than the way you write Web requests purely manually. Of course, in the actual development we can not be such a violent direct use of the Get and post methods, it is best to do a package, so that both save the workload, reduce duplication of code, but also to ensure that the requirements of the normative, in the subsequent blog post will be expanded detailed introduction of the project.

Android Project development process (ii)--AFINAL usage Brief introduction

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.