GlideApp实现圆形圆角以及带边框颜色

 

1.圆形圆角图片

public void loadRoundImage(String url, ImageView imageView, int placeholderResId, int radius) {
    if (imageView != null) {
        if (!TextUtils.isEmpty(url)) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                url = url.replace("https://", "http://");
            }
            if (radius > 0) {
                // 设置图片圆角角度
                RoundedCorners roundedCorners = new RoundedCorners(radius);
                // 通过RequestOptions扩展功能
                RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(radius * 2, radius * 2).circleCrop();
                // 加载图片
                GlideApp.with(BaseApplication.getInstance())
                        .load(url)
                        .placeholder(placeholderResId)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .apply(options)
                        .into(imageView);
            } else {
                // 加载图片
                GlideApp.with(BaseApplication.getInstance())
                        .load(url)
                        .placeholder(placeholderResId)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(imageView);
            }
        } else {
            // 暂无图片
        }
    }
}

 

2.圆形带边框图片

public void loadRoundImage(String url, ImageView imageView, int placeholderResId, int borderWidth, int borderColor) {
    if (imageView != null) {
        if (!TextUtils.isEmpty(url)) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                url = url.replace("https://", "http://");
            }
            // 加载图片
            GlideApp.with(BaseApplication.getInstance())
                    .load(url)
                    .placeholder(placeholderResId)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .transform(new GlideCircleTransform(borderWidth, borderColor))
                    .into(imageView);
        } else {
            // 暂无图片
        }
    }
}
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.NonNull;

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

import java.security.MessageDigest;

public class GlideCircleTransform extends BitmapTransformation {

    private Paint mBorderPaint;
    private float mBorderWidth;

    public GlideCircleTransform(int borderWidth, int borderColor) {
        mBorderWidth = borderWidth;
        mBorderPaint = new Paint();
        mBorderPaint.setDither(true);
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setColor(borderColor);
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setStrokeWidth(mBorderWidth);
    }

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

        int size = (int) (Math.min(source.getWidth(), source.getHeight()) - (mBorderWidth / 2));
        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);
        if (mBorderPaint != null) {
            float borderRadius = r - mBorderWidth / 2;
            canvas.drawCircle(r, r, borderRadius, mBorderPaint);
        }
        return result;
    }

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

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
    }

}

 

你可能感兴趣的:(android知识分享)