1. 程式人生 > >Android-query框架的基本使用(一)

Android-query框架的基本使用(一)

使用Android-query框架開發,能夠進行快速開發,比傳統的Android開發要節省很多程式碼。

第一節:

        // 必須實現AQuery這個類
	AQuery aq = new AQuery(view);
	// 按順序分析:取得xml對應控制元件id,設定圖片,設定可以顯示,點選事件(方法someMethod必須是public修飾)    

	aq.id(R.id.icon).image(R.drawable.icon).visible().clicked(this, "someMethod");  
	// 設定文字內容
        aq.id(R.id.name).text(content.getPname());
        aq.id(R.id.time).text(FormatUtility.relativeTime(System.currentTimeMillis(), content.getCreate())).visible();


	aq.id(R.id.desc).text(content.getDesc()).visible();  


 AQuery也支援Fragment:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        
        View view = inflater.inflate(getContainerView(), container, false);             
                
        aq = new AQuery(getActivity(), view);
        return view;
  }   

第二節: 使用AQuery非同步載入圖片

2.1 從網上讀取圖片

aq.id(R.id.image1).image(“圖片URL”); 

2.2 快取控制:  圖片過大的話,避免記憶快取

boolean memCache =false;

     boolean fileCache = true;

     aq.id(R.id.image1).image("http://www.vikispot.com/z/images/vikispot/android-w.png", memCache, fileCache);
2.3 當下載太多圖片的時候需要降低圖片取樣率,第四個引數為了保證圖片質量,一般範圍時200-399
aq.id(R.id.image1).image(imageUrl, true, true, 200, 0);
2.4 如果下載圖片失敗,處理的方法:1. 設定一個預定的圖片  2. 使imageview不可見或者是goneaq.id(R.id.image1).image(imageUrl, true, true, 0, R.drawable.default_image);
     aq.id(R.id.image1).image(imageUrl, true, true, 0, AQuery.INVISIBLE);
     aq.id(R.id.image1).image(imageUrl, true, true, 0, AQuery.GONE);

2.5 圖片預載入// 從之前的url取得小圖片
     String thumbnail = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_s.jpg";   
     Bitmap preset = aq.getCachedImage(thumbnail);
    // 載入大圖片前先顯示小圖片
     String imageUrl = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg";            
    aq.id(R.id.image).image(imageUrl, false, false, 0, 0, preset, AQuery.FADE_IN);
2.6 在載入圖片的時候顯示進度條,progress裡面傳入idString imageUrl = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg";            
     aq.id(R.id.image).progress(R.id.progress).image(imageUrl, false, false);
2.7 圖片圓角顯示,不支援大圖片ImageOptions options = new ImageOptions();
     options.round = 15;
     aq.id(R.id.image).image(url, options);
2.8 圖片長寬比例    // 保留原圖片比例
    aq.id(R.id.image).image(imageUrl, true, true, 0, 0, null, AQuery.FADE_IN, AQuery.RATIO_PRESERVE);
    // 自定義圖片比例
    //1:1, a square 
    aq.id(R.id.image2).image(imageUrl, true, true, 0, 0, null, 0, 1.0f / 1.0f);             
    aq.id(R.id.image3).image(imageUrl, true, true, 0, 0, null, 0, 1.5f / 1.0f);     
    //16:9, a video thumbnail
    aq.id(R.id.image4).image(imageUrl, true, true, 0, 0, null, 0, 9.0f / 16.0f);    
    aq.id(R.id.image5).image(imageUrl, true, true, 0, 0, null, 0, 3.0f / 4.0f);
2.9 圖片描點,如果圖片過高,描點可用來描述圖片的哪一部分用於顯示
Anchor values:1.0 : Display top of the image
0 : Display the center of the image
-1.0 : Display bottom of the image
AQuery.ANCHOR_DYNAMIC : Display image with a top bias for photos.
=======================================================ImageOptions options =newImageOptions();options.ratio =1;options.anchor =1.0;aq.id(R.id.image1).image(imageUrl, options);
2.10 自定義圖片載入後的處理aq.id(R.id.image1).image(imageUrl,true,true,0,0,newBitmapAjaxCallback(){});
2.11 非同步從檔案載入圖片,建議使用降低取樣率避免oomFile file =newFile(path);//load image from file, down sample to target width of 300 pixels  
aq.id(R.id.avatar).image(file,300);
   //load image from file with callback
aq.id(R.id.avatar).image(file,false,300,newBitmapAjaxCallback(){
@Override
publicvoid callback(String url,ImageView iv,Bitmap bm,AjaxStatus status){
iv.setImageBitmap(bm);
}
});
2.12 如果之前image("url")已經成功,之後的都可以直接使用而不需要重新訪問網路,也就是說之後可以離線訪問此影象資源
2.13 檔案中獲取緩衝圖片
     File file = aq.getCachedFile(url);
2.14 除了imageview,webview也可以用來放圖片
     aq.id(R.id.web).progress(R.id.progress).webImage(url);
2.15 延遲圖片載入,幫助你是否載入正在快速滾動的listview,詳情參考文件使用
2.16 圖片不使用快取
     aq.id(R.id.image).image(url, false, false);
2.17 快取配置,快取一般是儲存在內部檔案系統,但也可以儲存在SDCard裡面File ext = Environment.getExternalStorageDirectory();
      File cacheDir = new File(ext, "myapp"); 
      AQUtility.setCacheDir(cacheDir);
2.18 共享圖片,為了與其他程式共享圖片,你需要把檔案放在SDCard,makeSharedFile方法建立快取地址的一個副本File file = aq.makeSharedFile(url, "android.png");
         if(file != null){
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("image/jpeg");
                intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                startActivityForResult(Intent.createChooser(intent, "Share via:"), SEND_REQUEST);
        }
2.19 配置,最好把配置寫在application的onCreate方法,詳細參考文件
2.20 程式退出時候需要把快取清除
      if(isTaskRoot()){
  AQUtility.cleanCacheAsync(this);
}
或者: if(isTaskRoot()){
//clean the file cache with advance option
        long triggerSize = 3000000; //大於3M時候開始清除
        long targetSize = 2000000;      //直到少於2M
        AQUtility.cleanCacheAsync(this, triggerSize, targetSize);
        }
2.21 低記憶體處理publicclassMainApplicationextendsApplication{
@Override
         public void onLowMemory(){  
        //clear all memory cached images when system is in low memory
        //note that you can configure the max image cache count, see CONFIGURATION
         BitmapAjaxCallback.clearCache();
}
}