0

ImageView があり、ColorFilter (PorterDuff.Mode.MULTIPLY) を使用しています。

画像全体ではなく、この colorFilter を使用することは可能ですか? 「マージン」/「パディング」のようなものでなければなりません。

例: 画像の幅と高さ = 100 dp。ただし、colorFilter は ImageView の中央で 50 dp (幅と高さ) である必要があります。

下の写真は私が必要なものです(赤= colorFilter)

ここに画像の説明を入力

4

1 に答える 1

1

そのメソッドをサブクラス化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);
    }
}
于 2015-03-24T19:38:11.187 に答える