微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带边框的毕加索圆形变换

如何解决带边框的毕加索圆形变换

尝试应用带边框的圆形变换,但不考虑边框的大小并且抗锯齿也不起作用。

我想用给定的颜色和边框创建一个循环变换。

这是我的转换类:

public static class CircleTransformation implements Transformation {

    @ColorInt
    private final int borderColor;

    private final float borderRadius;

    public CircleTransformation() {
        this.borderColor = -1;
        this.borderRadius = -1;
    }

    public CircleTransformation(@ColorInt final int borderColor,final float borderRadius) {
        this.borderColor = borderColor;
        this.borderRadius = borderRadius;
    }

    @Override
    public Bitmap transform(final Bitmap source) {
        // if the source bitmap is null we can't do anything
        if (source == null) {
            return null;
        }

        final int size = Math.min(source.getWidth(),source.getHeight());

        final int x = (source.getWidth() - size) / 2;
        final int y = (source.getHeight() - size) / 2;

        final Bitmap squaredBitmap = Bitmap.createBitmap(source,x,y,size,size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        final Bitmap bitmap = Bitmap.createBitmap(size,source.getConfig());

        final Canvas canvas = new Canvas(bitmap);
        final Paint paint = new Paint();
        final BitmapShader shader = new BitmapShader(squaredBitmap,BitmapShader.TileMode.CLAMP,BitmapShader.TileMode.CLAMP);
        paint.setShader(shader);
        paint.setAntiAlias(true);

        final float r = size / 2f;

        // if border is configured we draw it
        if (borderRadius != -1) {
            // Prepare the background
            final Paint paintBg = new Paint();
            paintBg.setColor(borderColor);
            paintBg.setAntiAlias(true);

            // Draw the background circle
            canvas.drawCircle(r,r,paintBg);

            // Draw the image smaller than the background so a little border will be seen
            canvas.drawCircle(r,r - borderRadius,paint);

        } else {
            // Draw the image as a circle
            canvas.drawCircle(r,paint);
        }

        squaredBitmap.recycle();
        return bitmap;
    }

    @SuppressWarnings("HardCodedStringLiteral")
    @Override
    public String key() {
        return "circle(color=" + borderColor + ",radius=" + borderRadius + ")";
    }
}

显示图片

val borderColor = ContextCompat.getColor(context,borderColorRes)

//borderRadiusRes = 2dp
val borderRadius = context.resources.getDimension(borderRadiusRes)

PicassoProvider.with(view.context)
                            .load(url)
                            .placeholder(R.drawable.placeholder)
                            .transform(PicassoProvider.CircleTransformation(borderColor,borderRadius))
                            .into(view)

最终结果如下:

Result

如上图所示,红色边框一点都不平滑,边框没有2dp。 我在这里错过了什么?

解决方法

使用 Picasso 的方法 $except = A::instance()->attributes(); $attributes = $o->getAttributes(null,$except); resizeDimen 解决了这个问题。

用法:

resize

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。