Android Canvas绘制图片层叠处理方式porterduff xfermode

这篇博客介绍了如何在Android的Canvas中使用PorterDuff模式进行图片层叠处理。通过drawColor方法结合PorterDuff.Mode参数,可以实现不同的绘图效果。同时,文章提到了setXfermode方法,用于设置或清除Xfermode对象,以控制像素混合行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一直以来,看到别人对Canvas和Paint操作,都喜欢设置一个参数,但是我一直不理解,今天花点时间好好理解一下~
Canvas的drawColor()有2个方法,注意看注释,里面都提到了一个 porterduff 

Canvas:

/**
* Fill the entire canvas' bitmap (restricted to the current clip) with the
* specified color, using srcover porterduff mode.
*
* @param color the color to draw onto the canvas
*/
public void drawColor(int color) {
native_drawColor(mNativeCanvas, color);
}

/**
* Fill the entire canvas' bitmap (restricted to the current clip) with the
* specified color and porter-duff xfermode.
*
* @param color the color to draw with
* @param mode the porter-duff mode to apply to the color
*/
public void drawColor(int color, PorterDuff.Mode mode) {
native_drawColor(mNativeCanvas, color, mode.nativeInt);
}


Paint:

/**
* Set or clear the xfermode object.
* <p />
* Pass null to clear any previous xfermode.
* As a convenience, the parameter passed is also returned.
*
* @param xfermode May be null. The xfermode to be installed in the paint
* @return xfermode
*/
public Xfermode setXfermode(Xfermode xfermode) {
int xfermodeNative = 0;
if (xfermode != null)
xfermodeNative = xfermode.native_instance;
native_setXfermode(mNativePaint, xfermodeNative);
mXfermode = xfermode;
return xfermode;
}


涉及到的几个类,可以看看源码,可能加深理解:
PorterDuff
Xfermode
PorterDuffXfermode


其实就是在绘制图像的时候,当有层叠的时候,如何显示图片做一个限定,比如:有A,B两个图片有重叠的位置,那么重叠的位置是显示A的部分还是显示B的部分,还是都不显示,设置这个方法后,里面有很多类型可以选择,具体类型看下面的链接中有图片,很直观的就可以看懂。


参考链接:

http://meiyitianabc.blog.163.com/blog/static/10502212720132533933105/
http://www.2cto.com/kf/201310/253772.html
http://www.havenliu.com/java/689.html
http://www.cnblogs.com/DonkeyTomy/articles/3215137.html
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值