Android中的Glide加載圖片


注意:在Android Studio的項目的build.gradle中添加:

 

compile 'com.github.bumptech.glide:glide:3.6.1'

 

然后同步一下

 

目錄:

  1.     使用Glide結合列表的樣式進行圖片加載
  2.     如果使用的是RecyclerView,可以在Adapter的onBindViewHolder方法中使用
  3.     當加載網絡圖片時,由於加載過程中圖片未能及時顯示,此時可能需要設置等待時的圖片,通過placeHolder()方法
  4.     當加載圖片失敗時,通過error(Drawable drawable)方法設置加載失敗后的圖片顯示
  5.     圖片的縮放,centerCrop()和fitCenter()
  6.     顯示gif動畫
  7.     顯示本地視頻
  8.     緩存策略
  9.     優先級,設置圖片加載的順序
  10.     當不需要將加載的資源直接放入到ImageView中而是想獲取資源的Bitmap對象
  11.     集成網絡棧(okHttp,Volley)
 
1.使用Glide結合列表的樣式進行圖片加載:
        1)    如果使用的是ListView,可以直接在Adapter的getView方法中使用:                
  1. @Override
  2. public View getView(int position, View convertView, ViewGroup parent) {
  3. if (null == convertView) {
  4. //.....
  5. }
  6. Glide
  7. .with(context)
  8. .load(imageUrls[position])
  9. .into(holder.imageView);
  10. return convertView;
  11. }
 
        2)    如果使用的是RecyclerView,可以在Adapter的onBindViewHolder方法中使用:        
  1. @Override
  2. public void onBindViewHolder(RVViewHolder holder, int position) {
  3. Glide.with(MainActivity.this)
  4. .load(args[position])
  5. .into(holder.imageView);
  6. }
       
         3)    當加載網絡圖片時,由於加載過程中圖片未能及時顯示,此時可能需要設置等待時的圖片,通過placeHolder()方法: 
  1. Glide
  2. .with(context)
  3. .load(UsageExampleListViewAdapter.eatFoodyImages[0])
  4. .placeholder(R.mipmap.ic_launcher) // can also be a drawable
  5. .into(imageViewPlaceholder);
        
        4)    當加載圖片失敗時,通過error(Drawable drawable)方法設置加載失敗后的圖片顯示:
  1. Glide
  2. .with(context)
  3. .load("http://futurestud.io/non_existing_image.png")
  4. .error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
  5. .into(imageViewError);
 
        5)    圖片的縮放,centerCrop()和fitCenter():        
  1. //使用centerCrop是利用圖片圖填充ImageView設置的大小,如果ImageView的
  2. //Height是match_parent則圖片就會被拉伸填充
  3. Glide.with(MainActivity.this)
  4. .load(args[position])
  5. .centerCrop()
  6. .into(holder.imageView);
  1. //使用fitCenter即縮放圖像讓圖像都測量出來等於或小於 ImageView 的邊界范圍
  2. //該圖像將會完全顯示,但可能不會填滿整個 ImageView。
  3. Glide.with(MainActivity.this)
  4. .load(args[position])
  5. .fitCenter()
  6. .into(holder.imageView);
        6)    顯示gif動畫:
  1. Glide
  2. .with( context )
  3. .load( gifUrl )
  4. .asGif() //判斷加載的url資源是否為gif格式的資源
  5. .error( R.drawable.full_cake )
  6. .into( imageViewGif );
 
        7)    顯示本地視頻
  1. String filePath = "/storage/emulated/0/Pictures/example_video.mp4";
  2. Glide
  3. .with( context )
  4. .load( Uri.fromFile( new File( filePath ) ) )
  5. .into( imageViewGifAsBitmap );
        8)    緩存策略:
  1. Glide
  2. .with( context )
  3. .load( Images[0] )
  4. .skipMemoryCache( true ) //跳過內存緩存
  5. .into( imageViewInternet );
  1. Glide
  2. .with( context )
  3. .load( images[0] )
  4. .diskCacheStrategy( DiskCacheStrategy.NONE ) //跳過硬盤緩存
  5. .into( imageViewInternet );
  • DiskCacheStrategy.NONE 什么都不緩存
  • DiskCacheStrategy.SOURCE 僅僅只緩存原來的全分辨率的圖像
  • DiskCacheStrategy.RESULT 僅僅緩存最終的圖像,即降低分辨率后的(或者是轉換后的)
  • DiskCacheStrategy.ALL 緩存所有版本的圖像(默認行為
        9)    優先級,設置圖片加載的順序:
    
  • Priority.LOW
  • Priority.NORMAL
  • Priority.HIGH
  • Priority.IMMEDIATE   
  1. private void loadImageWithHighPriority() {
  2. Glide
  3. .with( context )
  4. .load( mages[0] )
  5. .priority( Priority.HIGH )
  6. .into( imageViewHero );
  7. }
  8. private void loadImagesWithLowPriority() {
  9. Glide
  10. .with( context )
  11. .load( images[1] )
  12. .priority( Priority.LOW )
  13. .into( imageViewLowPrioLeft );
  14. Glide
  15. .with( context )
  16. .load( images[2] )
  17. .priority( Priority.LOW )
  18. .into( imageViewLowPrioRight );
  19. }
 
        10)    當不需要將加載的資源直接放入到ImageView中而是想獲取資源的Bitmap對象:
  1. //括號中的300,600代表寬和高但是未有作用
  2. SimpleTarget target = new SimpleTarget<Bitmap>(300,600) {
  3. @Override
  4. public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
  5. holder.imageView.setImageBitmap(resource);
  6. }
  7. };
  8. Glide.with(MainActivity.this)
  9. .load(args[position])
  10. .asBitmap()
  11. .into(target);
        
        11)    集成網絡棧(okHttp,Volley):  
  1. dependencies {
  2. // your other dependencies
  3. // ...
  4. // Glide
  5. compile 'com.github.bumptech.glide:glide:3.6.1'
  6. // Glide's OkHttp Integration
  7. compile 'com.github.bumptech.glide:okhttp-integration:1.3.1@aar'
  8. compile 'com.squareup.okhttp:okhttp:2.5.0'
  9. }
  1. dependencies {
  2. // your other dependencies
  3. // ...
  4. // Glide
  5. compile 'com.github.bumptech.glide:glide:3.6.1'
  6. // Glide's Volley Integration
  7. compile 'com.github.bumptech.glide:volley-integration:1.3.1@aar'
  8. compile 'com.mcxiaoke.volley:library:1.0.8'
  9. }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM