我有一个 ImageView 并使用一个 ColorFilter (PorterDuff.Mode.MULTIPLY)。
是否可以使用此颜色过滤器但不能在整个图像上使用?它必须像“边距”/“填充”。
示例:图像宽度和高度 = 100dp。但是 colorFilter 必须是 ImageView 中心的 50dp(宽度和高度)。
下图就是我需要的(red = colorFilter)
我有一个 ImageView 并使用一个 ColorFilter (PorterDuff.Mode.MULTIPLY)。
是否可以使用此颜色过滤器但不能在整个图像上使用?它必须像“边距”/“填充”。
示例:图像宽度和高度 = 100dp。但是 colorFilter 必须是 ImageView 中心的 50dp(宽度和高度)。
下图就是我需要的(red = colorFilter)
您可以继承ImageView
并覆盖其onDraw()
方法。我发布了一个极简解决方案,根据您的需要进行修改!
public class OverlayImageView extends ImageView {
Paint paint;
float padding = 30;
public OverlayImageView(Context context) {
super(context);
init();
}
public OverlayImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public OverlayImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
paint.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(padding, padding, canvas.getWidth()-padding, canvas.getHeight()-padding, paint);
}
}