setXfermode 用來自定義View的時候融合圖片很方便。
下圖是按照canvas的大小畫bitmap的效果,並不是直接畫圓形和方型。
主要有幾種模式,可以參見PorterDuff.Mode列舉,具體效果看下圖:

上圖 可以理解為作用域是第二次繪畫的bitmap大小

paint.setColor(Color.RED);
canvas.drawCircle(getWidth()/3,getHeight()/3,getHeight()/3,paint);
paint.setXfermode(new PorterDuffXfermode(mode));
paint.setColor(Color.GREEN);
canvas.drawRect(getWidth()/3,getHeight()/3,getWidth()/4*3,getHeight()/4*3,paint);
複製程式碼
為什麼會不一樣呢?
因為這次的作用區域是第二次畫的方型大小。