1、Xfermode分类:
1. AvoidXfermode
不支持硬件加速
2. PixelXorXfermode
不支持硬件加速
3. PorterDuffXfermode
部分支持硬件加速
PorterDuffXfermode模式使用分类:
1、当需要清空图像时:
Mode.CLEAR(清空源图像)
Mode.XOR(清空相交图像)
2、当目标图像和源图像混合,需要生成颜色的叠加特效
Mode.ADD(饱和度相加)
Mode.DARKEN(变暗)
Mode.LIGHTEN(变亮)
Mode.MULTIPLY(正片叠底)
Mode.OVERLAY(叠加)
Mode.SCREEN(滤色)
3、当不需要特效,根据某一张图像的透明像素来裁剪时,由于SRC相关模式与DST相关模式是相通的,唯一不同的是决定当前哪个是目标图像和源图像
Mode.SRC;
Mode.DST;
Mode.SRC_OVER;
Mode.DST_OVER;
Mode.SRC_IN;
Mode.DST_IN;
Mode.SRC_OUT;
Mode.DST_OUT;
Mode.SRC_ATOP;
Mode.DST_ATOP;
2、Xfermode使用
public class ProgressView extends View {
private int mProgress;
private int dstColor;
private int srcColor;
private Paint mPaint;
private int width = 800;
private int height = 400;
private Bitmap dstBmp;
private Bitmap srcBmp;
public ProgressView(Context context) {
this(context,null);
}
public ProgressView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* 初始化
*/
public void init(){
mPaint = new Paint();
dstColor= Color.RED;
srcColor=Color.BLUE;
dstBmp = makeDst(width,height);
srcBmp = makeSrc(width,height);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int layerID = canvas.saveLayer(0,0,width,height,mPaint,Canvas.ALL_SAVE_FLAG);
canvas.drawBitmap(dstBmp, 0, 0, mPaint);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
canvas.drawBitmap(srcBmp, 0, mProgress*400/100-400, mPaint);
mPaint.setXfermode(null);
canvas.restoreToCount(layerID);
}
public Bitmap makeDst(int w, int h) {
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(dstColor);
c.drawRoundRect(new RectF(0, 0, w, h),200,200, p);
return bm;
}
public Bitmap makeSrc(int w, int h) {
Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bm);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(srcColor);
c.drawRect(0, 0,w,h, p);
return bm;
}
public void setProgress(int progress){
this.mProgress=progress;
postInvalidate();
}
}
网友评论