android圖片蒙層

這裏我們使用一個自定義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);

透明圖片:
圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章