您的位置:首页 > 其它

Glide用在CircleImageView上每次进入第一次显示默认图片

2016-04-11 10:06 483 查看
       用CircleImageView+glide  下载图片 ,下载成功 ,每次第一次会显示的是下载失败的图片,很郁闷,去网上找了下,说是CircleImageView 把位置占了,自己搞不懂,最后解决是去看下glide的demo,glide 有各类专门解决图片形状的问题,

public class GlideCircleImage extends BitmapTransformation {

    public GlideCircleImage(Context context) {

        super(context);

    }

    

    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.with(this).load(url).transform(new GlideCircleImage(this)).placeholder(R.drawable.ic_launcher).into(imageView);

  下载图片就是圆形图片 ,xml布局CircleImageView 改为 imageview

       

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