您的位置:首页 > 其它

Glide加载图片与设置图片圆角

2017-12-15 15:24 357 查看
compile 'com.github.bumptech.glide:glide:3.8.0'  图形加载库

   ImageView imgView=(ImageView) findViewById(R.id.circleImage);

 //普通图片

   Glide.with(context).load(url).into(img);

 //圆形图片

   Glide.with(context).load("http://oo6pz0u05.bkt.clouddn.com/17-12-13/69427561.jpg").transform(new GlideCircleImageView(context)).into(imgView);

 //圆角图片

   Glide.with(context).load("http://oo6pz0u05.bkt.clouddn.com/17-12-13/69427561.jpg").transform(new GlideRoundImageView(context,15)).into(imgView);

public class GlideCircleImageView extends BitmapTransformation {

    public GlideCircleImageView(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;

    }

    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);

        }

        float    radius = Resources.getSystem().getDisplayMetrics().density * 15f;

        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();

    }

}

public class GlideRoundImageView  extends BitmapTransformation {

    private static float radius = 0f;

    public GlideRoundImageView(Context context) {

        this(context, 4);

    }

    public GlideRoundImageView(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);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: