博客原址:http://write.blog.csdn.net/postedit
源碼地址:https://github.com/yangfuhai/afinal
Afinal簡介
- Afinal 是一個android的sqlite orm 和 ioc 框架。同時封裝了android中的http框架,使其更加簡單易用;
- 使用finalBitmap,無需考慮bitmap在android中加載的時候oom的問題和快速滑動的時候圖片加載位置錯位等問題。
- Afinal的宗旨是簡潔,快速。約定大於配置的方式。儘量一行代碼完成所有事情。
目前Afinal主要有四大模塊:
-
FinalDB模塊:android中的orm框架,一行代碼就可以進行增刪改查。支持一對多,多對一等查詢。
-
FinalActivity模塊:android中的ioc框架,完全註解方式就可以進行UI綁定和事件綁定。無需findViewById和setClickListener等。
-
FinalHttp模塊:通過httpclient進行封裝http數據請求,支持ajax方式加載。
-
FinalBitmap模塊:通過FinalBitmap,imageview加載bitmap的時候無需考慮bitmap加載過程中出現的oom和android容器快速滑動時候出現的圖片錯位等現象。FinalBitmap可以配置線程加載線程數量,緩存大小,緩存路徑,加載顯示動畫等。FinalBitmap的內存管理使用lru算法,沒有使用弱引用(android2.3以後google已經不建議使用弱引用,android2.3後強行回收軟引用和弱引用,詳情查看android官方文檔),更好的管理bitmap內存。FinalBitmap可以自定義下載器,用來擴展其他協議顯示網絡圖片,比如ftp等。同時可以自定義bitmap顯示器,在imageview顯示圖片的時候播放動畫等(默認是漸變動畫顯示)。
使用afinal快速開發框架需要有以下權限:
1 |
< uses-permission android:name = "android.permission.INTERNET" /> |
2 |
< uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" /> |
- 第一個是訪問網絡
- 第二個是訪問sdcard
- 訪問網絡是請求網絡圖片的時候需要或者是http數據請求時候需要,訪問sdcard是圖片緩存的需要。
FinalDB使用方法
關於finalDb的更多介紹,請點擊這裏
1 |
FinalDb
db = FinalDb.create( this ); |
2 |
User
user = new User(); |
4 |
user.setName( "michael
yang" ); |
FinalActivity使用方法:
- 完全註解方式就可以進行UI綁定和事件綁定
- 無需findViewById和setClickListener等
01 |
public class AfinalDemoActivity extends FinalActivity
{ |
04 |
@ViewInject (id=R.id.button,click= "btnClick" )
Button button; |
05 |
@ViewInject (id=R.id.textView)
TextView textView; |
07 |
public void onCreate(Bundle
savedInstanceState) { |
08 |
super .onCreate(savedInstanceState); |
09 |
setContentView(R.layout.main); |
12 |
public void btnClick(View
v){ |
13 |
textView.setText( "text
set form button" ); |
FinalHttp使用方法:
普通get方法
01 |
FinalHttp
fh = new FinalHttp(); |
02 |
fh.get( "http://www.yangfuhai.com" , new AjaxCallBack(){ |
05 |
public void onLoading( long count, long current)
{ |
06 |
textView.setText(current+ "/" +count); |
10 |
public void onSuccess(String
t) { |
11 |
textView.setText(t== null ? "null" :t); |
15 |
public void onStart()
{ |
20 |
public void onFailure(Throwable
t, String strMsg) { |
使用FinalHttp上傳文件 或者 提交數據 到服務器(post方法)
文件上傳到服務器,服務器如何接收,請查看這裏
01 |
AjaxParams
params = new AjaxParams(); |
02 |
params.put( "username" , "michael
yang" ); |
03 |
params.put( "password" , "123456" ); |
05 |
params.put( "profile_picture" , new File( "/mnt/sdcard/pic.jpg" )); |
06 |
params.put( "profile_picture2" ,
inputStream); |
07 |
params.put( "profile_picture3" , new ByteArrayInputStream(bytes)); |
09 |
FinalHttp
fh = new FinalHttp(); |
10 |
fh.post( "http://www.yangfuhai.com" ,
params, new AjaxCallBack(){ |
12 |
public void onLoading( long count, long current)
{ |
13 |
textView.setText(current+ "/" +count); |
17 |
public void onSuccess(String
t) { |
18 |
textView.setText(t== null ? "null" :t); |
使用FinalHttp下載文件:
01 |
FinalHttp
fh = new FinalHttp(); |
03 |
HttpHandler
handler = fh.download( "http://www.xxx.com/下載路徑/xxx.apk" ,
//這裏是下載的路徑 |
05 |
"/mnt/sdcard/testapk.apk" , |
08 |
public void onLoading( long count, long current)
{ |
09 |
textView.setText( "下載進度:" +current+ "/" +count); |
13 |
public void onSuccess(File
t) { |
14 |
textView.setText(t== null ? "null" :t.getAbsoluteFile().toString()); |
FinalBitmap 使用方法
加載網絡圖片就一行代碼 fb.display(imageView,url) ,更多的display重載請看幫助文檔
01 |
private GridView
gridView; |
02 |
private FinalBitmap
fb; |
04 |
protected void onCreate(Bundle
savedInstanceState) { |
05 |
super .onCreate(savedInstanceState); |
06 |
setContentView(R.layout.images); |
08 |
gridView
= (GridView) findViewById(R.id.gridView); |
09 |
gridView.setAdapter(mAdapter); |
11 |
fb
= FinalBitmap.create( this ); |
12 |
fb.configLoadingImage(R.drawable.downloading); |
21 |
public View
getView( int position,
View convertView, ViewGroup parent) { |
23 |
if (convertView
== null ){ |
24 |
convertView
= View.inflate(BitmapCacheActivity. this ,R.layout.image_item, null ); |
25 |
iv
= (ImageView) convertView.findViewById(R.id.imageView); |
26 |
iv.setScaleType(ScaleType.CENTER_CROP); |
27 |
convertView.setTag(iv); |
29 |
iv
= (ImageView) convertView.getTag(); |
32 |
fb.display(iv,Images.imageUrls[position]); |