Ligxe
4/7/2017 - 6:27 AM

Android:利用第三方库Glide加载图片后进行形状包装比如圆角和圆形的

Android:利用第三方库Glide加载图片后进行形状包装比如圆角和圆形的

package org.lgf.ostrich.library.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

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

/**
 * Create On: 2017/3/21."UTF-8"
 * Author: GuanFu Li
 * Desc:将图片转化为圆角,构造中第二个参数为定义半径
 * Transplant:添加Glide的jar或者在线注入依赖,
 * Related:class:null;
 * xml:null;
 */

public class GlideRoundTransform extends BitmapTransformation {

    private static float radius = 0f;

    public GlideRoundTransform(Context context) {
        this(context, 4);
    }

    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

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

    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }

    @Override
    public String getId() {
        return getClass().getName() + Math.round(radius);
    }
}
package org.lgf.ostrich.library.manager;

import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import org.lgf.ostrich.library.view.GlideCircleTransform;
import org.lgf.ostrich.library.view.GlideRoundTransform;

/**
 *
 * Create On: 2017/3/21."UTF-8"
 * Author:  GuanFu Li
 * Desc:针对Glide加载完成图片进行形状包装,可以显示圆形和圆角图片:移植需要官方glide的jar或者在线注入依赖。
 * Related:class:org.lgf.ostrich.library.view.GlideCircleTransform
 *                org.lgf.ostrich.library.view.GlideRoundTransform
 *            xml:null
 * Usage:
 *  GlideImgManager.glideLoader(this, url, R.mipmap.ic_launcher, R.mipmap.ic_launcher, mImgNormal);
 *  GlideImgManager.glideLoader(this, url, R.mipmap.ic_launcher, R.mipmap.ic_launcher, mImgCircle, 0);//圆形图片
 *  GlideImgManager.glideLoader(this, url, R.mipmap.ic_launcher, R.mipmap.ic_launcher, mImgRound, 10);//圆角图片,传入非零值为圆角半径。
 */

public class GlideImgManager {

    /**
     * load normal  for img
     *
     * @param url       图片地址
     * @param errorImg   加载失败时的占位图
     * @param emptyImg  未加载完成时的占位图
     * @param iv        目标显示控件:ImageView
     */
    public static void glideLoader(Context context, String url, int errorImg, int emptyImg, ImageView iv) {
        //原生 API
        Glide.with(context).load(url).placeholder(emptyImg).error(errorImg).into(iv);
    }
    /**
     * load normal  for  circle or round img
     *
     * @param url    网络图片地址
     * @param errorImg  加载失败时的占位图
     * @param emptyImg   加载未完成是的占位图
     * @param iv    Glide加载到的目标空间
     * @param radius   圆角半径,为零时显示圆形图片,建议10
     */
    public static void glideLoader(Context context, String url, int errorImg, int emptyImg, ImageView iv, int radius) {
        if (0 == radius) {
            Glide.with(context).load(url).placeholder(emptyImg).error(errorImg).transform(new GlideCircleTransform(context)).into(iv);
        } else {
            Glide.with(context).load(url).placeholder(emptyImg).error(errorImg).transform(new GlideRoundTransform(context,radius)).into(iv);
        }
    }
}
package org.lgf.ostrich.library.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;

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

/**
 * 创建时间: 2017/3/21.
 * Author: GuanFu Li
 * Desc:将图片转化为圆形
 * Transplant:添加Glide的jar或者在线注入依赖,
 * Related:class:null;
 * xml:null;
 */
public class GlideCircleTransform extends BitmapTransformation {
    public GlideCircleTransform(Context context) {
        super(context);
    }

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

    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        int size = Math.min(source.getWidth(), source.getHeight());
        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        // TODO this could be acquired from the pool too
        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);

        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        float r = size / 2f;
        canvas.drawCircle(r, r, r, paint);
        return result;
    }

    @Override
    public String getId() {
        return getClass().getName();
    }
}

针对第三方库Glide加载图片进行形状包装比如圆角和圆形

  • 在module中的dependencies的标签中的添加第三方库glide的依赖。 Glide官方地址 比如:
dependencies {
    compile 'com.github.bumptech.glide:glide:3.5.2'
}
  • GlideImgManager.javaGlideCircleTransform.javaGlideRoundTransform.java等文件复制到module中
  • 在Activity,Fragment等代码中使用示例:
  imgGlideCircle = (xxx)findViewById(xxx);
  imgGlideRound = (xxx)findViewById(xxx);
  String url="http://img4.imgtn.bdimg.com/it/u=2128568119,2020679487&fm=23&gp=0.jpg";
  GlideImgManager.glideLoader(this, url, R.mipmap.ic_launcher, R.mipmap.ic_launcher, imgGlideCircle, 0);
  GlideImgManager.glideLoader(this, url, R.mipmap.ic_launcher, R.mipmap.ic_launcher, imgGlideRound, 30);