赞
踩
一、Canvas与Bitmap
Bitmap bitmap = Bitmap.createBitmap(100,100, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//或者
// Canvas canvas = new Canvas();
// canvas.setBitmap(bitmap);
//定义一个绘制矩形的Bitmap
private 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(0xFF66AAFF);
c.drawRect(0, 0, w, h, p);
return bm;
}
//定义一个绘制圆形Bitmap
private 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(0xFFFFCC44);
c.drawOval(new RectF(0, 0, w, h), p);
return bm;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GREEN);//将背景设置为绿色
int sc = canvas.saveLayer(0, 0, screenW, screenH, null, Canvas.MATRIX_SAVE_FLAG |
Canvas.CLIP_SAVE_FLAG |
Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
Canvas.CLIP_TO_LAYER_SAVE_FLAG);//创建一个新的Bitmap,在新的画布上演示图形混合后的效果
canvas.drawBitmap(dstBitmap, 0,
0, mPaint);
mPaint.setXfermode(pdXfermode); //设置Paint的Xfermode
canvas.drawBitmap(srcBitmap, width / 2,
height / 2, mPaint);
mPaint.setXfermode(null);
canvas.restoreToCount(sc);// 还原画布,将新创建的画布合并到原画布上
}
canvas.drawColor(Color.GREEN);//将背景设置为绿色
canvas.drawBitmap(dstBitmap, 0, 0, mPaint);
mPaint.setXfermode(pdXfermode); //设置Paint的Xfermode
canvas.drawBitmap(srcBitmap, width / 2, height / 2, mPaint);
mPaint.setXfermode(null);
三、另一种实现方式
mBitmap = Bitmap.createBitmap(screenW, screenH, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
这就创建了一个新的透明的画布,然后将它传递给Canvas。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GREEN);//将背景设置为绿色
mCanvas.drawBitmap(dstBitmap, 0, 0, mPaint);
mPaint.setXfermode(pdXfermode); //设置Paint的Xfermode
mCanvas.drawBitmap(srcBitmap, width / 2, height / 2, mPaint);
mPaint.setXfermode(null);
canvas.drawBitmap(mBitmap,0,0,null);
}
注:以上的总结参考了http://www.2cto.com/kf/201605/505751.html ,然后加了一些自己的理解,也算是对自己的一个总结。如果有跑偏的地方,也希望能给出指正。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。