【问题标题】:How to make color filter round on an Android ImageView?如何在 Android ImageView 上制作彩色滤镜?
【发布时间】:2020-11-13 17:57:19
【问题描述】:

我正在使用自定义类(ImageView 的扩展)来获得 XML 圆形图像视图。问题是当我调用 .setColorFilter() 时,它不遵守相同的圆形/圆形边界。

如何让颜色过滤器只影响图像而不影响视图的整个矩形?

这是我的自定义类供参考:

public class RoundedCornerImageFilterView extends ImageFilterView {
    public RoundedCornerImageFilterView(Context context) {
        super(context);
    }

    public RoundedCornerImageFilterView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoundedCornerImageFilterView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setImageFilter(int color) {
        this.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Drawable drawable = getDrawable();

        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }
        Bitmap b = ((BitmapDrawable) drawable).getBitmap();
        Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
        int w = getWidth();
        int h = getHeight();
        Bitmap roundedCornerBitmap = getRoundedCornerBitmap(bitmap, h, w);
        canvas.drawBitmap(roundedCornerBitmap, 0, 0, null);

    }

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int height, int width) {
        Bitmap sbmp;
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0,
                (width), (height));
        final RectF rectF = new RectF(rect);
        final float roundPx = 28;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }

}

我的xml实现:

<MY_PATH.RoundedCornerImageFilterView
    android:id="@+id/MY_IMAGE_VIEW"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    />

我正在尝试设置颜色过滤器:

MY_IMAGE_VIEW.setColorFilter(Color.parseColor(color), PorterDuff.Mode.OVERLAY)

在过滤器之前(看起来应该这样):

设置过滤器后(现在可以看到方形边缘):

【问题讨论】:

    标签: android android-imageview android-custom-view android-color


    【解决方案1】:

    虽然图像(位图)被赋予了圆角,但写入它的画布却没有。由于颜色过滤器被应用到画布上,颜色溢出到角落。

    我建议您将圆角矩形应用于路径,然后将路径剪切到画布。像这样的:

    public class RoundedImageView extends AppCompatImageView {
        private final Path mPath = new Path();
    
        public RoundedImageView(Context context) {
            super(context);
            init();
        }
    
        public RoundedImageView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public RoundedImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            setColorFilter(Color.RED, PorterDuff.Mode.OVERLAY);
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            mPath.reset();
            mPath.addRoundRect(new RectF(0, 0, w, h), 128, 128, Path.Direction.CW);
        }
    
        @Override
        public void draw(Canvas canvas) {
            canvas.save();
            canvas.clipPath(mPath);
            super.draw(canvas);
            canvas.restore();
        }
    }
    

    我这里用的是ImageView,但是概念还是一样的。

    如果您进行这种类型的裁剪,那么对位图进行四舍五入就变得多余了,因为它也会被裁剪到路径中。

    【讨论】:

    • 啊,我在想过滤器是应用于可绘制对象而不是画布本身。非常感谢!
    • @Mr.Drew 我认为过滤器应用于可绘制对象而不是画布。绘制圆角位图时,不就是一个带有透明角的矩形位图吗?接受彩色滤光片的是那些透明的角落。 (见this)..
    • 好吧,我不是自定义 UI(Android 或其他)方面的专家,所以我对每一层中绘制的内容和位置有点困惑。当您将矩形剪辑到画布上时,在背景中可绘制的位图是否仍然作为矩形存在,但在角落顶部有一个视图以使其具有圆形边缘?
    • @Mr.Drew 剪裁使可绘制对象保持不变。 Clipping 指定在哪里绘制,以及在哪里不绘制。产生的效果是圆润的外观。可以将drawable本身更改为圆角,但drawable仍然是一个矩形,除非应用剪辑,否则颜色过滤器将为整个矩形区域着色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 2019-01-07
    • 2014-01-22
    相关资源
    最近更新 更多