android圖片蒙層

這裏咱們使用一個自定義view來爲圖片蒙層。該方法投機取巧,直接把一張有透明效果的圖片直接畫到原圖上。tranparent.png那張圖片能夠換成用bitmap本身畫,之後改進。先上效果圖:上面是原圖,下面是蒙層後的效果
圖片描述java

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" />

調用:android

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

透明圖片:
圖片描述canvas

相關文章
相關標籤/搜索