2014-10-29 27 views
1

當圖像是透明的,我弄黑...PorterDuff.Mode.DST_IN越來越黑在那裏應該是透明的

我已經做了同樣的事情在一個位圖的畫布,並將其與另一個圖像遮蔽的圖像完美工作,但是當我嘗試在自定義視圖的onDraw內部進行遮罩時,它不起作用。

我嘗試了一切,在圖像蒙版,路徑,其他圖像之前繪製矩形,所有內容都保持黑色,並且應該透明。我搜索了人們在CLEAR模式下遇到的很多其他問題,但沒有奏效。

請幫忙!

mBGPaint = new Paint(); 
    mBGPaint.setStyle(Paint.Style.FILL); 
    mBGPaint.setColor(Color.TRANSPARENT); 

    mRectSrc = new Rect(); 
    mRectDst = new Rect(); 

    mask = BitmapFactory.decodeResource(getResources(), R.drawable.big_balloon_path); 
    maskPaint = new Paint(); 
    maskPaint.setFilterBitmap(true); 
    maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 

    mRectSrc.set(0, 0, mask.getWidth(),mask.getHeight()); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

    mRectDst.set(0,0,this.getMeasuredWidth(),this.getMeasuredHeight()); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    canvas.drawRect(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight(), mBGPaint); 
    canvas.drawBitmap(mask, mRectSrc, mRectDst, maskPaint); 

回答

2

您可能需要在視圖的初始化打開軟件渲染:

setLayerType(LAYER_TYPE_SOFTWARE, null); 

很多Android的繪圖命令使用硬件渲染,這是默認值時,靜默失敗或產生不正確的結果設置。

+1

考慮到它可能會明顯變慢,尤其是在處理動畫時,這很糟糕。 – ollien 2015-04-03 19:05:33

+0

@ollien同意,並且在硬幣的另一面,似乎瘋狂地讓API失敗,並在默認設置下不做任何事情。 – pents90 2015-04-03 20:00:38