2016-04-15 46 views
2

我有圖像文件(PNG/JPG格式)。當加載到列表視圖,其中一些我需要與另一個透明圖像疊加。我使用類似如下的方法執行此操作:Android滑動加載圖片文件應用覆蓋並設置爲imageview

public Bitmap applyOverlay(Context context, Bitmap sourceImage, int overlayDrawableResourceId){ 
     Bitmap bitmap = null; 
     try{ 
      int width = sourceImage.getWidth(); 
      int height = sourceImage.getHeight(); 
      Resources r = context.getResources(); 
      Drawable imageAsDrawable = new BitmapDrawable(r, sourceImage); 
      Drawable[] layers = new Drawable[2]; 
      layers[0] = imageAsDrawable; 
      layers[1] = new BitmapDrawable(r, BitmapUtils.decodeSampledBitmapFromResource(r, overlayDrawableResourceId, width, height)); 
      LayerDrawable layerDrawable = new LayerDrawable(layers);    
      bitmap = BitmapUtils.drawableToBitmap(layerDrawable); 
     }catch (Exception ex){} 
     return bitmap; 
    } 

其中BitmapUtils是一個自定義類,它實現了位圖智能方法。

import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.graphics.drawable.LayerDrawable; 
import android.media.ThumbnailUtils; 
import android.widget.ImageView; 
import java.io.ByteArrayOutputStream; 
import java.io.File; 

public class BitmapUtils { 

    public static Bitmap applyOverlay(Context context, Bitmap sourceImage, int overlayDrawableResourceId){ 
     Bitmap bitmap = null; 
     try{ 
      int width = sourceImage.getWidth(); 
      int height = sourceImage.getHeight(); 
      Resources r = context.getResources(); 
      Drawable imageAsDrawable = new BitmapDrawable(r, sourceImage); 
      Drawable[] layers = new Drawable[2]; 
      layers[0] = imageAsDrawable; 
      layers[1] = new BitmapDrawable(r, BitmapUtils.decodeSampledBitmapFromResource(r, overlayDrawableResourceId, width, height)); 
      LayerDrawable layerDrawable = new LayerDrawable(layers); 
      bitmap = BitmapUtils.drawableToBitmap(layerDrawable); 
     }catch (Exception ex){} 
     return bitmap; 
    } 

    public static Bitmap drawableToBitmap(Drawable drawable) { 
     Bitmap bitmap = null; 

     if (drawable instanceof BitmapDrawable) { 
      BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; 
      if(bitmapDrawable.getBitmap() != null) { 
       return bitmapDrawable.getBitmap(); 
      } 
     } 

     if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { 
      bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel 
     } else { 
      bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); 
     } 

     Canvas canvas = new Canvas(bitmap); 
     drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 
     drawable.draw(canvas); 
     return bitmap; 
    } 

    static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight){ 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 

     // Compute inSampleSize 
     options.inSampleSize = computeInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
    } 
} 



使用的Android(UIL)通用圖像裝載機庫中,這種效應是通過編寫自定義ImageDecoder並將其施加到ImageLoaderConfiguration實現。

問題是,我該如何使用Glide來做到這一點?
謝謝。

回答

2

爲我工作的事情:
我用了以下的答案:https://stackoverflow.com/a/33709650/3106975

public void display(final Context context, String mediaPath, final ImageView imageView, final boolean doApplyOverlay){ 
     try{     
       Glide.with(context) 
         .load(mediaPath) 
         .asBitmap() 
         .placeholder(R.drawable.placeholder_default) 
         .error(R.drawable.error_default) 
         .diskCacheStrategy(DiskCacheStrategy.ALL) 
         .into(new SimpleTarget<Bitmap>() { 
          @Override 
          public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { 
           if (doApplyOverlay) { 
            resource = BitmapUtils.applyOverlay(context, resource, R.drawable.overlay); 
           } 
           imageView.setImageBitmap(resource); 
          } 
         });  
     }catch (Exception ex){} 
    } 



因此,加載一個位圖,並顯示給用戶之前與它進行交互,你需要實現自定義目標並在那裏進行交互。由Glide提供的SimpleTarget類實現了Target接口並允許這樣的事情。有關更多信息,您可以訪問github上的Glide的CustomTargets wiki頁面:https://github.com/bumptech/glide/wiki/Custom-targets

+0

在哪裏可以找到BitmapUtils類? – AndroidRuntimeException

+0

@AgustinSivoplás這不是一個庫類,我專門爲這個項目創建了基本的位圖智能方法。 –

+0

如果將來有人需要BitmapUtils.java類,我已經創建了一個公共主題。 https://gist.github.com/agustinsivoplas/44f01672d698394dc28edda2c7a05cb6 – AndroidRuntimeException

相關問題