Android Afinal framework for open source projects

The project is shown in the figure:

This article refers to the network!

Afinal is an open source Android orm and ioc application development framework, which is characterized by small size and flexibility, and less code intrusion. In android application development, through Afinal's ioc framework, such as ui binding, event binding, and automatic binding through annotations. Through Afinal's orm framework, without any configuration information, one line of code can add, delete, modify, and query the sqlite database of android. At the same time, Afinal embeds simple and easy-to-use tools such as finalHttp, which can easily intercede with http. Afinal's mission is to be concise and fast. The convention is greater than the way of configuration. Try to do everything in one line of code.

 

The convenience brought by the various modules of the Afinal framework

1. FinalDB module: the orm framework in android, one line of code can add, delete, modify and check. Support one-to-many, many-to-one and other queries.
2. FinalActivity module: the ioc framework in android, UI binding and event binding can be performed in a fully annotated way. No need to findViewById and setClickListener etc.
3. FinalHttp module: encapsulates http data requests through httpclient and supports ajax loading.
4. FinalBitmap module: When loading bitmap through FinalBitmap, imageview does not need to consider the phenomenon of oom and picture dislocation when the android container slides quickly during the bitmap loading process. FinalBitmap can configure the number of thread loading threads, cache size, cache path, loading display animation, etc. The memory management of FinalBitmap uses the lru algorithm and does not use weak references (Google no longer recommends using weak references after android2.3. After android2.3, soft references and weak references are forcibly recovered. For details, see the official android documentation), better manage bitmap memory . FinalBitmap can customize the downloader to extend other protocols to display network pictures, such as ftp. At the same time, you can customize the bitmap display, play animations, etc. when the imageview displays the picture (the default is the gradient animation display).

 

The process of bringing the Afinal framework into your project

1. Download the Afinal jar package. It is recommended to download from GIT (https://github.com/yangfuhai/afinal), which not only provides Afinal jar package, but also Afinal source code and detailed API.
2. Add the downloaded jar package to the Android project. I believe everyone will, but you may encounter that when the Afinal package is added, the Activity that inherits FinalActivity will report an error like java.lang.classNotFound. In this case, you'd better copy the Afinal package directly to the lib folder of the Android project. in. At this time, if you look at the Dependencies package of Android, if there is an Afinal package, it should not be wrong to run the APK again.
3. A project that depends on the Afinal package also needs to add the permissions it needs: we add the following permissions to the AndroidManifest.xml file:

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

 

The specific use of each module of the Afinal framework

How to use FinalDB :

  1. FinalDbdb=FinalDb.create(this);
  2. Useruser=newUser();
  3. user.setEmail("[email protected]");
  4. user.setId(1);
  5. user.setName("michaelyang");
  6. db.save(user);

 

How to use FinalActivity :

  1. publicclassAfinalDemoActivityextendsFinalActivity{
  2. //No need to call findViewById and setOnclickListener etc.
  3. @ViewInject(id=R.id.button,click="btnClick")Buttonbutton;
  4. @ViewInject(id=R.id.textView)TextViewtextView;
  5. publicvoidonCreate(BundlesavedInstanceState){
  6. super .onCreate (savedInstanceState);
  7. setContentView(R.layout.main);
  8. }
  9. public void btnClick(Viewv){
  10. textView.setText("textsetformbutton");
  11. }
  12. }

 

How to use FinalHttp :

 

  1. FinalHttpfh = new FinalHttp ();
  2. fh.get("http://www.yangfuhai.com",newAjaxCallBack(){
  3. @Override
  4. public void onLoading( long count, long current){ //Automatically called back every 1 second
  5. textView.setText(current+"/"+count);
  6. }
  7. @Override
  8. publicvoidonSuccess(Stringt){
  9. textView.setText(t==null?"null":t);
  10. }
  11. @Override
  12. public void onStart(){
  13. //Callback when starting http request
  14. }
  15. @Override
  16. publicvoidonFailure(Throwablet,StringstrMsg){
  17. // Callback when loading fails
  18. }
  19. });


Upload files or submit data:

 

  1. AjaxParamsparams=newAjaxParams();
  2. params.put("username","michaelyang");
  3. params.put("password","123456");
  4. params.put("email","[email protected]");
  5. params.put("profile_picture",newFile("/mnt/sdcard/pic.jpg"));//上传文件
  6. params.put( "profile_picture2" ,inputStream); //Upload data stream
  7. params.put( "profile_picture3" , new ByteArrayInputStream(bytes)); //Submit byte stream
  8. FinalHttpfh = new FinalHttp ();
  9. fh.post("http://www.yangfuhai.com",params,newAjaxCallBack(){
  10. @Override
  11. publicvoidonLoading(longcount,longcurrent){
  12. textView.setText(current+"/"+count);
  13. }
  14. @Override
  15. publicvoidonSuccess(Stringt){
  16. textView.setText(t==null?"null":t);
  17. }
  18. });


Download files using FinalHttp:

 

  1. FinalHttpfh = new FinalHttp ();
  2. fh.download( "http://www.xxx.com/download path/xxx.apk" , "/mnt/sdcard/testapk.apk" , new AjaxCallBack(){
  3. @Override
  4. publicvoidonLoading(longcount,longcurrent){
  5. textView.setText( "Download progress: " +current+ "/" +count);
  6. }
  7. @Override
  8. public void onSuccess(Net){
  9. textView.setText(t==null?"null":t.getAbsoluteFile().toString());
  10. }
  11. });

 

How to use FinalBitmap (fb.display(imageView,url) is just one line of code to load a network image):

 

  1. private GridViewgridView;
  2. privateFinalBitmapfb;
  3. @Override
  4. protectedvoidonCreate(BundlesavedInstanceState){
  5. super .onCreate (savedInstanceState);
  6. setContentView(R.layout.images);
  7. gridView=(GridView)findViewById(R.id.gridView);
  8. gridView.setAdapter(mAdapter);
  9. fb= new FinalBitmap( this ).init(); //Must call init to initialize the FinalBitmap module
  10. fb.configLoadingImage(R.drawable.downloading);
  11. //You can configure more than a dozen other items here, or you can do it without configuration. After configuration, you must call the init() function to take effect.
  12. //fb.configBitmapLoadThreadSize(intsize)
  13. //fb.configBitmapMaxHeight(bitmapHeight)
  14. }
  1. ///////////////////////////adaptergetView////////////////////////////////////////////
  2. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  3. ImageViewiv;
  4. if(convertView==null){
  5. convertView=View.inflate(BitmapCacheActivity.this,R.layout.image_item,null);
  6. iv=(ImageView)convertView.findViewById(R.id.imageView);
  7. iv.setScaleType(ScaleType.CENTER_CROP);
  8. convertView.setTag (iv);
  9. }else{
  10. iv=(ImageView)convertView.getTag();
  11. }
  12. //bitmap is loaded with this line of code, display has other overloads, see the source code for details
  13. fb.display(iv,Images.imageUrls[position]);
  14. returnconvertView;
  15. }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326530915&siteId=291194637