Android的ImageView点击态增加遮罩

注意,这个需求的Image不是固定的,而是针对当前ImageView上的图片。也就是说用Selector的做法是不行的。

public class PressedRoundImageView extends ImageView {

    private boolean showZheZhao = false;

    public PressedRoundImageView(Context context) {
        super(context);
    }

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

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 如果启用了,则 加上一层遮罩
        if (isPressed() && showZheZhao) {
            canvas.drawColor(0x19000000);
        }
    }

    @Override
    protected void dispatchSetPressed(boolean pressed) {
        super.dispatchSetPressed(pressed);
        invalidate();
    }

    public void setZheZhao(boolean flag) {
        this.showZheZhao = flag;
    }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *