androidピクチャマスク

1579 ワード

ここでは、カスタム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); 
    } 
} 
}

呼び出し:
CenterImage mGoodsImg =(CenterImage)findViewById(R.id.goodsImage);
mGoodsImg.setCenterImgShow(true);

透明な画像: