The Afinal quick development framework is very convenient to use. The following describes how to use Afinal to load network images and download files:
First look:
Note: Before using Afinal, add the Afinal jar, which can be downloaded here: http://download.csdn.net/detail/baiyuliang2013/7313587
This includes the Afinal source code and jar. You only need to add the jar package for use. In the learning stage, you can also directly add the source code to your project to learn more about Afinal.
Check the Code:
Activit_main.xml:
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". mainActivity "> <Button android: id =" @ + id/btn "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "Download"/> <TextView android: id = "@ + id/text" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_toRightOf = "@ + id/btn" android: text = "Progress:"/> <ImageView android: id = "@ + id/img" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: layout_centerHorizontal = "true" android: src = "@ drawable/ic_launcher"/> </RelativeLayout>
MainActivity. java:
Package com. example. afinaltest; import java. io. file; import net. tsz. afinal. *; import net. tsz. afinal. annotation. view. viewInject; import net. tsz. afinal. http. ajaxCallBack; import android. annotation. suppressLint; import android. OS. bundle; import android. OS. environment; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. imageView; import android. w Idget. textView; import android. widget. toast; public class MainActivity extends FinalActivity {@ ViewInject (id = R. id. img) ImageView img; TextView textView; Button btn; FinalBitmap finalBitMap = null; FinalHttp fh; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView = (TextView) findViewById (R. id. text); btn = (Button) find ViewById (R. id. btn); btn. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {String apkPath = Environment. getExternalStorageDirectory (). getAbsolutePath () + "/qq.apk"; File f = new File (apkPath); if (f. exists () {f. delete () ;}fh = new FinalHttp (); fh. download ("http://gdown.baidu.com/data/wisegame/4ae6d2d7378e6cdf/QQ_122.apk", apkPath, new AjaxCallBack <File> () {@ Overridepublic Void onStart () {super. onStart (); Toast. makeText (getApplicationContext (), "Start download", Toast. LENGTH_SHORT ). show () ;}@ SuppressLint ("DefaultLocale") @ Overridepublic void onLoading (long count, long current) {super. onLoading (count, current); int progress = 0; if (current! = Count & current! = 0) {progress = (int) (current/(float) count * 100);} else {progress = 100;} textView. setText ("progress:" + progress + "%") ;}@ Overridepublic void onSuccess (File t) {super. onSuccess (t); Toast. makeText (getApplicationContext (), "download completed", Toast. LENGTH_SHORT ). show (); textView. setText (t = null? "Null": t. getAbsoluteFile (). toString () ;}@ Overridepublic void onFailure (Throwable t, int errorNo, String strMsg) {super. onFailure (t, errorNo, strMsg); Toast. makeText (getApplicationContext (), "Download failed", Toast. LENGTH_SHORT ). show () ;}}) ;}}); finalBitMap = FinalBitmap. create (MainActivity. this); finalBitMap. display (img, "http://meme.zenfs.com/u/a33312d2e9eaa443321f4ec716fe795a23c27c89.jpeg ");}}
As you can see, MainActivity inherits FianlActivity. Therefore, when initializing the control, you do not need findViewById. You can directly use the annotation method such as @ ViewInject (id = R. id. img) ImageView img; @ ViewInject (id = R. id. btn, click = "onclick") Button btn; for a click event, you only need to write a method corresponding to click = "onclick", for example, public void onclick.
The FinallyBitmap source code analysis shows that the create method must be called during initialization, and then display (imgView, url) must be called. Network images can be loaded, and images displayed before loading can be set, you can see from the source code that FinalHttp is required to download the File and call the download method. In this example, download (String, String, AjaxCallBack <File>) is used, the first parameter is the file path to be downloaded, the second parameter is the local file storage path, and the third parameter is a callback function, which is called every second, you can easily view the file download progress and file download status, such as success or failure. You need to rewrite onStart, onLoading, onSuccess, onFailure, and other methods.
Finally, do not forget to add the network access permission and the read and write permissions for the memory card in AndroidManifest. xml:
<Uses-permission android: name = "android. permission. INTERNET"/>
<Uses-permission android: name = "android. permission. READ_EXTERNAL_STORAGE"/>
<Uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE"/>