概述
當以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時:
我們猿類工作壓力大,很需要有自己的樂趣,於是乎,我開通了音樂人賬號,以後的作品將會上傳到我的音樂人小站上。如果這篇博客幫助到您,希望您能多關注,支持,鼓勵我將創作進行下去,同時也祝你能在工作和生活樂趣兩發麪都能出彩!