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

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); 
    } 
} 
}
<com.epos.testrxjava.CenterImage
android:layout_below="@id/image"
android:id="@+id/goodsImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/dog" />

调用:

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

透明图片:
图片描述


zorro
52 声望2 粉丝