android图片蒙层

这里我们使用一个自定义view来为图片蒙层。该方法投机取巧,直接把一张有透明效果的图片直接画到原图上。tranparent.png那张图片可以换成用bitmap自己画,以后改进。先上效果图:上面是原图,下面是蒙层后的效果
android图片蒙层_第1张图片

public class CenterImage extends ImageView { 
private Paint paint; 
private boolean isCenterImgShow; 
private Bitmap bitmap; 
public void setCenterImgShow(boolean centerImgShow) { 
    isCenterImgShow = centerImgShow; 
    if (isCenterImgShow) { 
    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.transparent); 
    invalidate(); 
} 
} 
public CenterImage(Context context) { 
    super(context); 
    init(); 
} 
public CenterImage(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 
    public CenterImage(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(); 
} 
private void init() { 
    paint = new Paint(); 
} 
@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    if (isCenterImgShow && bitmap != null) { 
    //从中心点开始画 
    canvas.drawBitmap(bitmap, getMeasuredWidth() / 2 - bitmap.getWidth() / 2, getMeasuredHeight() / 2 - bitmap.getHeight() / 2, paint); 
    } 
} 
}

调用:

CenterImage mGoodsImg =(CenterImage)findViewById(R.id.goodsImage);
mGoodsImg.setCenterImgShow(true);

透明图片:
android图片蒙层_第2张图片

你可能感兴趣的:(android图片蒙层)