概述
当以Bitmap作为画布材料时,可以绘制出以下各种图案:
demo
/**
* 图形图像处理:在Bitmap上绘画
*/
public class MyBitMapViewSec extends View {
private int width;
private int height;
private Paint mPaintCircle;
private Paint mPaintRect;
private Bitmap mBitmap;
private Canvas mCanvasBm;
public MyBitMapViewSec(Context context) {
super(context);
}
public MyBitMapViewSec(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintCircle = new Paint();
mPaintCircle.setColor(Color.YELLOW);
mPaintRect = new Paint();
mPaintRect.setColor(Color.GREEN);
//XOR:交叠和被交叠部分均不显示;DST_OVER:自身交叠部分不显示;SRC_OVER交叠部分只显示自己
PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.XOR);
mPaintRect.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);
//参数:画布宽、长尺寸和格式
mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);
mCanvasBm = new Canvas(mBitmap);//自定义一个画布,画布材料是Bitmap对象
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.RED);
//先在bitmap上画
mCanvasBm.drawCircle(width / 2, height / 2, width / 2, mPaintCircle);
mCanvasBm.drawRect(0,0,width/2,width/2,mPaintRect);
//将画好的bitmap画出来
canvas.drawBitmap(mBitmap,0,0,null);//这一步必不可少
}
}
结果演示:
PorterDuffXfermode mode = new PorterDuffXfermode(参数);
参数为PorterDuff.Mode.DST_OVEL时:
参数为PorterDuff.Mode.XOR时:
我们猿类工作压力大,很需要有自己的乐趣,于是乎,我开通了音乐人账号,以后的作品将会上传到我的音乐人小站上。如果这篇博客帮助到您,希望您能多关注,支持,鼓励我将创作进行下去,同时也祝你能在工作和生活乐趣两发面都能出彩!