GeorgeYang'Blog

my technology blog


本人认为用Glide做安卓的图片加载库最适合不过了,其他都没那么好用。

自定义bitmapTransform加载完成后显示圆形头像

首先布局是这样的:

 <ImageView
         android:layout_width="wrap_content"
         android:id="@+id/img"
         android:layout_height="wrap_content"/>

然后写自定义处理类:

 import android.app.Activity;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
 import android.graphics.Paint;
 import android.graphics.PorterDuff;
 import android.graphics.PorterDuffXfermode;
 import android.graphics.Rect;
 import android.graphics.RectF;
 import android.os.Bundle;

 import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
 import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;

 /**
  * Created by george.yang on 2016-5-5.
  */
 public class UserBitmapTransformation extends BitmapTransformation {
     //为了考虑性能
     private static UserBitmapTransformation instance;
     public static UserBitmapTransformation getInstance(Context context) {
         if (instance==null) {
             instance =  new UserBitmapTransformation(context);
         }
         return instance;
     }

     public UserBitmapTransformation(Context context) {
         super(context);
     }

     public UserBitmapTransformation(BitmapPool bitmapPool) {
         super(bitmapPool);
     }

     @Override
     protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
         return toRoundBitmap(toTransform);
     }

     @Override
     public String getId() {
         return "ddd";
     }

     /**
      * 转换图片成圆形
      * @param bitmap 传入Bitmap对象
      * @return
      */
     public Bitmap toRoundBitmap(Bitmap bitmap) {
         int width = bitmap.getWidth();
         int height = bitmap.getHeight();
         float roundPx;
         float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
         if (width <= height) {
             roundPx = width / 2;
             top = 0;
             bottom = width;
             left = 0;
             right = width;
             height = width;
             dst_left = 0;
             dst_top = 0;
             dst_right = width;
             dst_bottom = width;
         } else {
             roundPx = height / 2;
             float clip = (width - height) / 2;
             left = clip;
             right = width - clip;
             top = 0;
             bottom = height;
             width = height;
             dst_left = 0;
             dst_top = 0;
             dst_right = height;
             dst_bottom = height;
         }

         Bitmap output = Bitmap.createBitmap(width,
                 height, Bitmap.Config.ARGB_8888);
         Canvas canvas = new Canvas(output);

         final int color = 0xff424242;
         final Paint paint = new Paint();
         final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);
         final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);
         final RectF rectF = new RectF(dst);

         paint.setAntiAlias(true);

         canvas.drawARGB(0, 0, 0, 0);
         paint.setColor(color);
         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
         canvas.drawBitmap(bitmap, src, dst, paint);
         return output;
     }
 }

自定义placeholder的Drawable模仿stype文字头像

之前写过一篇文章,生成skype文字头像,是基于Fresco,由于项目二版完全摒弃Fresco,所以代码要迁移,上面BitmapTransformation执行的前提是图片加载成功,如果失败,就会显示placeholder设置的图片,所以要自定义一个图片加载失败时显示文字头像的drawable,代码如下:

 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.BitmapShader;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.ColorFilter;
 import android.graphics.Paint;
 import android.graphics.Rect;
 import android.graphics.RectF;
 import android.graphics.Shader;
 import android.graphics.Typeface;
 import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.text.TextUtils;

 import java.util.WeakHashMap;

 /**
  * Created by george.yang on 2016-5-5.
  */
 public class UserHeadDrawable extends Drawable {
     private static WeakHashMap<String,UserHeadDrawable> weakHashMap;
     //为了考虑性能
     public static UserHeadDrawable getInstance(String name) {
         if (weakHashMap==null) {
             weakHashMap =  new WeakHashMap<>();
         }
         UserHeadDrawable drawable = weakHashMap.get(name);
         if (drawable==null) {
             drawable = new UserHeadDrawable(name);
             weakHashMap.put(name,drawable);
         }
         return drawable;
     }

     private String userName;

     public UserHeadDrawable (String name) {
         if (!TextUtils.isEmpty(name)) {
             userName = name.replace(" ","");
         } else {
             userName = " ";
         }

         mPaint = new Paint();
         mPaint.setAntiAlias(true);
         mRectF = new RectF(0, 0, 100, 100);
     }

     private int getColor(char chr) {
         String coloIndexs;
         if (chr>='A' && chr <='Z') {
             coloIndexs = Integer.toString(chr-'A',3);
         } else {
             coloIndexs= Integer.toString(27,3);
         }
         if (coloIndexs.length()==1) {
             coloIndexs = "00"+coloIndexs;
         } else if (coloIndexs.length()==2) {
             coloIndexs = "0"+coloIndexs;
         }
         StringBuilder sb = new StringBuilder("#FF");
         for (char ch:coloIndexs.toCharArray()) {
             int colorCut = 0;
             //255/3=85 (0-85,86-170,170-255)
             if (ch=='1') {
                 colorCut = 70;
             } else if (ch == '2') {
                 colorCut = 150;
             } else {
                 colorCut = 230;
             }
             String colorCut16 = Integer.toHexString(colorCut).toUpperCase();
             sb.append(colorCut16);
         }
 //        Logutil.showlog("color:" + sb.toString());
         int bgColor = Color.GRAY;
         try {
             bgColor = Color.parseColor(sb.toString());
         } catch (Exception e) {

         }
         return bgColor;
     }

     @Override
     public void draw(Canvas canvas) {
         if (TextUtils.isEmpty(userName)) {
             userName = " ";
         }
             char chr = userName.toUpperCase().charAt(0);

             Rect cav = getBounds();

             int bgColor = getColor(chr);
             canvas.drawColor(Color.TRANSPARENT);

             mPaint.setColor(bgColor);
             canvas.drawCircle(mRectF.centerX(),mRectF.centerY(),mRectF.width()/2,mPaint);

             mPaint.setColor(Color.BLACK);
             mPaint.setTextSize(cav.width()/3);


             Rect bounds = new Rect();
             mPaint.getTextBounds(chr+"", 0, (chr+"").length(), bounds);

             canvas.drawText(chr +"",mRectF.centerX() - bounds.width()/2,mRectF.centerY()+bounds.height()/2,mPaint);

     }

     @Override
     public int getIntrinsicHeight() {
         return (int) mRectF.height();
     }

     @Override
     public int getIntrinsicWidth() {
         return (int) mRectF.width();
     }

     private Paint mPaint;
     private RectF mRectF;
     @Override
     public void setBounds(int left, int top, int right, int bottom) {
         super.setBounds(left, top, right, bottom);
         mRectF = new RectF(left, top, right, bottom);
     }

     @Override
     public void setAlpha(int alpha) {
         mPaint.setAlpha(alpha);
     }

     @Override
     public void setColorFilter(ColorFilter colorFilter) {
         mPaint.setColorFilter(colorFilter);
     }

     @Override
     public int getOpacity() {
         return 0;
     }
 }

最后使用方法

 Glide.with(mActivity)
                 .load(data.avatar)
                 .placeholder(UserHeadDrawable.getInstance(data.firstName))
                 .bitmapTransform(UserBitmapTransformation.getInstance(mActivity))
                 .into(imageView);

Glide更多使用用法