在Graphics2D API:Paint類昔驱、Canvas類中已經(jīng)介紹了Canvas基本的繪圖方法骤肛,本篇介紹一些基本的畫布操作.
注意:
1窍蓝、畫布操作針對(duì)的是畫布,而不是畫布上的圖形
2吓笙、畫布變換、裁剪影響后續(xù)圖形的繪制絮蒿,對(duì)之前已經(jīng)繪制過(guò)的內(nèi)容沒(méi)有影響
一叁鉴、畫布變換
1幌墓、平移(translate)
畫布平移,畫布的原始狀態(tài)是以左上角為原點(diǎn)常侣,向右是x軸正方向,向下是y軸正方向.
public void translate(float dx, float dy)
平移畫布溯祸,其實(shí)就是平移坐標(biāo)系焦辅,dx、dy分別代表水平氨鹏、豎直方向平移的距離
注意:平移是基于當(dāng)前起點(diǎn)平移
例:最初起點(diǎn)是(0,0),第一次平移translate(100,100)后起點(diǎn)變?yōu)?100,100)
再平移一次translate(200,200)后起點(diǎn)變?yōu)?300,300)
測(cè)試
private void gogogo(Canvas canvas) {
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.GRAY);
canvas.translate(100, 100);
Rect rect = new Rect(0, 0, 100, 100);
canvas.drawRect(rect, mPaint);
canvas.translate(100, 100);
mPaint.setColor(Color.BLUE);
canvas.drawRect(rect, mPaint);
}
2跟继、旋轉(zhuǎn)(rotate)
畫布旋轉(zhuǎn)舔糖,默認(rèn)是圍繞坐標(biāo)原點(diǎn)(0莺匠,0)來(lái)旋轉(zhuǎn)的,也可以指定旋轉(zhuǎn)中心
public void rotate(float degrees)
degrees:旋轉(zhuǎn)度數(shù)摇庙,>0順時(shí)針旋轉(zhuǎn)遥缕,<0逆時(shí)針旋轉(zhuǎn)
以默認(rèn)原點(diǎn)(0,0)旋轉(zhuǎn)將畫布旋轉(zhuǎn)degrees度數(shù)
public final void rotate(float degrees, float px, float py)
以(px,py)為旋轉(zhuǎn)中心將畫布旋轉(zhuǎn)degrees度數(shù)
測(cè)試
private void gogogo(Canvas canvas) {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(10);
mPaint.setColor(Color.RED);
Rect rectCanvas = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
Rect rect = new Rect(300, 300, 400, 400);
canvas.drawRect(rectCanvas, mPaint);//繪制畫布矩形邊框夕凝,方便觀察
canvas.drawRect(rect, mPaint);
canvas.rotate(30);//旋轉(zhuǎn)
mPaint.setColor(Color.BLUE);
canvas.drawRect(rectCanvas, mPaint);//繪制旋轉(zhuǎn)后畫布矩形邊框户秤,方便觀察
canvas.drawRect(rect, mPaint);
}
3鸡号、縮放(scale)
畫布縮放,默認(rèn)以(0,0)點(diǎn)為縮放中心堪藐,也可以指定縮放中心
public void scale(float sx, float sy)
sx挑围、sy:水平杉辙、豎直方向縮放比例
0~1代表縮小,>1代表放大蜘矢,若為負(fù)數(shù),縮放之后以x軸或y軸為對(duì)稱軸翻轉(zhuǎn)
以默認(rèn)原點(diǎn)(0,0)為縮放中心將畫布縮放
public final void scale(float sx, float sy, float px, float py)
以(px,py)為縮放中心將畫布縮放
PS:縮放后品腹,繪制在畫布上的圖形也會(huì)等比例縮放
測(cè)試
private void gogogo(Canvas canvas) {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(10);
mPaint.setColor(Color.RED);
Rect rectCanvas = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
Rect rect = new Rect(40, 40, 200, 100);
canvas.drawRect(rectCanvas, mPaint);//繪制畫布矩形邊框舞吭,方便觀察
canvas.drawRect(rect, mPaint);
canvas.scale(0.5f, 0.5f);//縮放
mPaint.setColor(Color.BLUE);
canvas.drawRect(rectCanvas, mPaint);//繪制縮放后畫布矩形邊框,方便觀察
canvas.drawRect(rect, mPaint);
}
這里需要注意的是:平移蔑穴、旋轉(zhuǎn)惧浴、傾斜的疊加方式和縮放不同
例:
canvas.translate(100, 100);
canvas.translate(100, 100);
上面連續(xù)兩次平移 = canvas.translate(200, 200);
canvas.rotate(30);
canvas.rotate(30);
上面連續(xù)兩次旋轉(zhuǎn) = canvas.rotate(60);
canvas.scale(0.5f, 0.5f);
canvas.scale(0.5f, 1f);
上面連續(xù)兩次縮放 = canvas.rotate(0.5f*0.5f,0.5f*1f) = canvas.rotate(0.25f捐腿,0.5f)
根據(jù)這個(gè)特性我們可以繪制一個(gè)"箭靶":
private void gogogo(Canvas canvas) {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
canvas.translate(canvas.getWidth() / 2, canvas.getHeight() / 2);
for (int i = 0; i <= 30; i++) {
canvas.drawCircle(0, 0, 300, mPaint);
canvas.scale(0.88f, 0.88f);
}
}
4柿顶、傾斜(skew)
public void skew(float sx, float sy)
sx:將畫布在x軸方向上傾斜相應(yīng)的角度,sx為傾斜角度的tan值
sy:將畫布在y軸方向上傾斜相應(yīng)的角度绞佩,sy為傾斜角度的tan值
注意:這里的sx,sy是三角函數(shù)中的tan值品山,比如在x軸方向上傾斜45度肘交,tan45°=1扑馁,這里的sx = 1
傾斜也是可以疊加使用的,連續(xù)傾斜角度 = 每次傾斜的tan值對(duì)應(yīng)角度相加
測(cè)試
private void gogogo(Canvas canvas) {
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLUE);
Rect rect = new Rect(0, 0, 300, 300);
canvas.drawRect(rect, mPaint);
canvas.skew(1, 0);//tan45°=1,x軸方向傾斜45°
mPaint.setColor(Color.argb(0x66, 0, 0xff, 0));
canvas.drawRect(rect, mPaint);
}
二复罐、畫布裁剪
public boolean clipRect(Rect rect)
public boolean clipRect(RectF rect)
public boolean clipRect(Rect rect, Region.Op op)
public boolean clipRect(RectF rect, Region.Op op)
public boolean clipRect(int left, int top, int right, int bottom)
public boolean clipRect(float left, float top, float right, float bottom)
public boolean clipRect(float left, float top, float right, float bottom,Region.Op op)
public boolean clipPath(Path path)
public boolean clipPath(Path path, Region.Op op)
public boolean clipRegion(Region region)
public boolean clipRegion(Region region, Region.Op op)
這些方法大同小異效诅,都是從原畫布裁剪出一個(gè)新畫布,就不一一贅述了.
測(cè)試
private void gogogo(Canvas canvas) {
canvas.drawColor(Color.GRAY);//裁剪前---為整個(gè)畫布設(shè)置背景色
canvas.clipRect(new Rect(200, 200, 400, 400));//裁剪
canvas.drawColor(Color.YELLOW);//裁剪后---為整個(gè)畫布設(shè)置背景色
}
三咽笼、畫布的保存(save)與恢復(fù)(restore)
上面介紹的畫布操作都是不可逆的戚炫,而且畫布操作會(huì)影響后面的繪圖,但是大多數(shù)時(shí)候我們需要使用之前狀態(tài)的畫布双肤,所以我們需要保存畫布的狀態(tài)(save),在需要的時(shí)候恢復(fù)(restore).
public int save()
每次調(diào)用杨伙,都會(huì)把當(dāng)前畫布狀態(tài)進(jìn)行保存,然后放入特定的棧中
public void restore()
每當(dāng)調(diào)用限匣,就會(huì)把棧中最頂層的畫布狀態(tài)取出來(lái),并按照這個(gè)狀態(tài)恢復(fù)畫布米死,之后的繪圖就是在這個(gè)恢復(fù)的畫布上
測(cè)試
private void gogogo(Canvas canvas) {
canvas.drawColor(Color.GRAY);
canvas.save();//第1次保存畫布狀態(tài)---全屏幕
canvas.clipRect(new Rect(100, 100, 600, 600));
canvas.drawColor(Color.RED);
canvas.save();//第2次保存畫布狀態(tài)---矩形區(qū)域Rect(100, 100, 600, 600)
canvas.clipRect(new Rect(200, 200, 500, 500));
canvas.drawColor(Color.GREEN);
canvas.save();//第3次保存畫布狀態(tài)---矩形區(qū)域Rect(200, 200, 500, 500)
canvas.clipRect(new Rect(300, 300, 400, 400));
canvas.drawColor(Color.BLUE);
}
上面的測(cè)試代碼進(jìn)行了3次save,在最后一次save后又裁剪了一次畫布并將其背景色設(shè)為藍(lán)色
現(xiàn)在峦筒,我們一步步恢復(fù)看看效果:
private void gogogo(Canvas canvas) {
......
canvas.restore();
canvas.drawColor(Color.WHITE);//第1次恢復(fù)---截圖
canvas.restore();
canvas.drawColor(Color.BLACK);//第2次恢復(fù)---截圖
canvas.restore();
canvas.drawColor(Color.YELLOW);//第3次恢復(fù)---截圖
}
如果你想直接恢復(fù)到第一次保存的畫布狀態(tài),可以連續(xù)調(diào)用:
canvas.restore();
canvas.restore();
canvas.restore();
或者調(diào)用方法:
public void restoreToCount(int saveCount)
這個(gè)方法會(huì)彈出棧中指定位置以及以上所有狀態(tài)物喷,并根據(jù)指定位置狀態(tài)進(jìn)行恢復(fù)
比如此處參數(shù)傳1,會(huì)彈出第1峦失、2、3次保存的畫布狀態(tài)尉辑,并根據(jù)第1次保存的畫布狀態(tài)進(jìn)行恢復(fù)
這里還有一個(gè)方法:
public int getSaveCount()
獲取棧中保存畫布狀態(tài)的次數(shù),這個(gè)方法的最小返回值為1隧魄,即使彈出了所有的畫布狀態(tài),返回值依舊為1襟企,代表默認(rèn)狀態(tài)
四闸溃、Demo
最后給出一個(gè)Demo拱撵,演示本文內(nèi)容
public class XView extends View {
private Paint mPaint;
public XView(Context context) {
this(context, null);
}
public XView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(3);
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);//抗鋸齒功能
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();//保存畫布A
for (int i = 0; i < 5; i++) {
canvas.drawCircle(100, 100, 50, mPaint);
canvas.translate(60, 0);
}
canvas.restore();//恢復(fù)畫布A
canvas.translate(100, 200);//平移畫布,讓接下來(lái)的圖形繪制在上一次圖形的下面
canvas.save();//保存平移后的畫布B
for (int i = 0; i < 30; i++) {
canvas.drawRect(0, 0, 400, 400, mPaint);
canvas.scale(0.88f, 0.88f, 200, 200);//縮放畫布
}
canvas.restore();//恢復(fù)平移后的畫布B
canvas.translate(0, 400);//平移畫布,讓接下來(lái)的圖形繪制在上一次圖形的下面
canvas.save();
canvas.drawCircle(200, 200, 200, mPaint);
for (int i = 0; i < 12; i++) {
canvas.drawLine(200, 200, 400, 200, mPaint);
canvas.rotate(30, 200, 200);
}
canvas.restore();
}
}