Picasso加载圆角图片

2017-04-07  本文已影响0人  GamePlayer

使用Picasso加载自定义圆角的圆形图片

private void loadRoundImg(String url, ImageView img, final int corner,int width,int height) {      
           Picasso.with(context).load(url).error(R.drawable.shangcheng).resize(width,height).placeholder(R.drawable.shangcheng).transform(new Transformation() {
            @Override
            public Bitmap transform(Bitmap source) {
                final Paint paint = new Paint();
                paint.setAntiAlias(true);
                paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

                Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(output);
                canvas.drawRoundRect(new RectF(0, 0, source.getWidth(), source.getHeight()), corner, corner, paint);
                if (source != output) {
                    source.recycle();
                }
                return output;
            }

            @Override
            public String key() {
                return "circle";
            }
        }).into(img);
    }
上一篇下一篇

猜你喜欢

热点阅读