android圖形影像處理PorterDuffXfermode
設定兩張圖片重疊的模式。
在正常的情況下,在已有的映像上繪圖將會在其上面添加一層新的形狀。如果新的Paint是完全不透明的,那麼它將完全遮擋住下面的Paint;如果它是部分透明的,那麼它將會被染上下面的顏色。
而setXfermode就可以來解決這個問題 .
舉例
重疊的圖層使用BitmapCanvas畫布。。注意下面
public class MyBitmapView2 extends View { private int width; private int height; private Paint paintCircle; private Paint paintRect; private Bitmap bitmap; private Canvas bitmapCanvas; public MyBitmapView2(Context context, AttributeSet attrs) { super(context, attrs); paintCircle = new Paint(); paintRect = new Paint(); paintRect.setColor(Color.GREEN); paintCircle.setColor(Color.YELLOW); //設定畫筆圖層在上 PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.DST_OVER); paintRect.setXfermode(mode); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec); height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec); setMeasuredDimension(width, height); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmapCanvas = new Canvas(bitmap);//bitmap畫布 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.RED); bitmapCanvas.drawCircle(width / 2, height / 2, width / 2, paintCircle); bitmapCanvas.drawRect(0, 0, width / 2, width / 2, paintRect); canvas.drawBitmap(bitmap, 0, 0, null); }}