您的位置:首页 > 移动开发 > Android开发

Android 遮罩效果

2015-09-11 11:15 483 查看

最终效果图:



背景图




由于图片是白色的,所以跟网页背景混在一起了

前景图






代码

[code]public class MaskImageView extends ImageView {

    private Xfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);

    public MaskImageView(Context context) {
        this(context, null);
    }

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

    public MaskImageView(Context context, AttributeSet attrs, int defStyle, Paint paint) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (!(getDrawable() instanceof BitmapDrawable)) {
            super.onDraw(canvas);
            return;
        }

        int saveFlags = Canvas.MATRIX_S***E_FLAG
            | Canvas.CLIP_S***E_FLAG
            | Canvas.HAS_ALPHA_LAYER_S***E_FLAG
            | Canvas.FULL_COLOR_LAYER_S***E_FLAG
            | Canvas.CLIP_TO_LAYER_S***E_FLAG;
        canvas.saveLayer(0, 0, getWidth(), getHeight(), null, saveFlags);

        getBackground().draw(canvas);
        BitmapDrawable image = (BitmapDrawable) getDrawable();
        image.getPaint().setXfermode(mXfermode);
        image.draw(canvas);

        canvas.restore();
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: