【问题标题】:Picasso Library rounded corners for placeholder毕加索图书馆圆角占位符
【发布时间】:2016-04-18 17:57:25
【问题描述】:

使用, Picasso.with(activity).load(url).transform(new CircleTransform(22,12)).into(imageView);我们可以有圆角来加载图像。但是占位符和错误图像没有圆角?

我引用的链接Make ImageView with Round Corner Using picasso

【问题讨论】:

  • 使用 glide 可以做到这一点

标签: android listview picasso


【解决方案1】:

毕加索无法做到这一点。查看答案here

【讨论】:

    【解决方案2】:
     Picasso.with(getApplicationContext()).load(url).placeholder(setCircularImage(R.drawable.profile_sample)).error(setCircularImage(R.drawable.profile_sample)).transform(new CircleTransform()).into(ivMenuProfile);
    

    添加带有占位符的 setCircularImage 方法,用于在圆形视图中制作占位符

    private RoundedBitmapDrawable setCircularImage(int id) {
            Resources res = getApplicationContext().getResources();
            Bitmap src = BitmapFactory.decodeResource(res, id);
            RoundedBitmapDrawable roundedBitmapDrawable = 
            RoundedBitmapDrawableFactory.create(res, src);
            roundedBitmapDrawable.setCornerRadius(Math.max(src.getWidth(), src.getHeight()) / 2.0f);
            return roundedBitmapDrawable;
        }
    

    添加带有变换的 CircleTransform() 以将形状更改为圆形以加载 URL 图像。

    public class CircleTransform  implements Transformation {
        @Override
        public Bitmap transform(Bitmap source) {
            int size = Math.min(source.getWidth(), source.getHeight());
    
            int x = (source.getWidth() - size) / 2;
            int y = (source.getHeight() - size) / 2;
    
            Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
            if (squaredBitmap != source) {
                source.recycle();
            }
    
            Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
    
            Canvas canvas = new Canvas(bitmap);
            Paint paint = new Paint();
            BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
            paint.setShader(shader);
            paint.setAntiAlias(true);
    
            float r = size/2f;
            canvas.drawCircle(r, r, r, paint);
    
            squaredBitmap.recycle();
            return bitmap;
        }
    
        @Override
        public String key() {
            return "circle";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-10
      • 2014-10-29
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2014-04-17
      • 1970-01-01
      相关资源
      最近更新 更多