Graphics2D API:Canvas操作

Graphics2D API:Paint類、Canvas類中已經(jīng)介紹了Canvas基本的繪圖方法,本篇介紹一些基本的畫布操作.

注意:
1、畫布操作針對的是畫布,而不是畫布上的圖形
2、畫布變換、裁剪影響后續(xù)圖形的繪制,對之前已經(jīng)繪制過的內(nèi)容沒有影響

一、畫布變換

1、平移(translate)

畫布平移,畫布的原始狀態(tài)是以左上角為原點,向右是x軸正方向,向下是y軸正方向.


public void translate(float dx, float dy)
平移畫布,其實就是平移坐標(biāo)系,dx、dy分別代表水平、豎直方向平移的距離

注意:平移是基于當(dāng)前起點平移
例:最初起點是(0,0),第一次平移translate(100,100)后起點變?yōu)?100,100)
再平移一次translate(200,200)后起點變?yōu)?300,300)

測試

    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),默認是圍繞坐標(biāo)原點(0,0)來旋轉(zhuǎn)的,也可以指定旋轉(zhuǎn)中心


public void rotate(float degrees)
degrees:旋轉(zhuǎn)度數(shù),>0順時針旋轉(zhuǎn),<0逆時針旋轉(zhuǎ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ù)

測試

    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)

畫布縮放,默認以(0,0)點為縮放中心,也可以指定縮放中心

public void scale(float sx, float sy)
sx、sy:水平、豎直方向縮放比例
0~1代表縮小,>1代表放大,若為負數(shù),縮放之后以x軸或y軸為對稱軸翻轉(zhuǎn)
以默認原點(0,0)為縮放中心將畫布縮放

public final void scale(float sx, float sy, float px, float py)
以(px,py)為縮放中心將畫布縮放

PS:縮放后,繪制在畫布上的圖形也會等比例縮放

測試

    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ù)這個特性我們可以繪制一個"箭靶":

    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值對應(yīng)角度相加

測試

    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)

這些方法大同小異,都是從原畫布裁剪出一個新畫布,就不一一贅述了.
測試

    private void gogogo(Canvas canvas) {
        canvas.drawColor(Color.GRAY);//裁剪前---為整個畫布設(shè)置背景色

        canvas.clipRect(new Rect(200, 200, 400, 400));//裁剪

        canvas.drawColor(Color.YELLOW);//裁剪后---為整個畫布設(shè)置背景色
    }

三、畫布的保存(save)與恢復(fù)(restore)

上面介紹的畫布操作都是不可逆的,而且畫布操作會影響后面的繪圖,但是大多數(shù)時候我們需要使用之前狀態(tài)的畫布,所以我們需要保存畫布的狀態(tài)(save),在需要的時候恢復(fù)(restore).

public int save() 
每次調(diào)用,都會把當(dāng)前畫布狀態(tài)進行保存,然后放入特定的棧中

public void restore()
每當(dāng)調(diào)用,就會把棧中最頂層的畫布狀態(tài)取出來,并按照這個狀態(tài)恢復(fù)畫布,之后的繪圖就是在這個恢復(fù)的畫布上

測試

    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);
    }

上面的測試代碼進行了3次save,在最后一次save后又裁剪了一次畫布并將其背景色設(shè)為藍色

現(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)

這個方法會彈出棧中指定位置以及以上所有狀態(tài),并根據(jù)指定位置狀態(tài)進行恢復(fù)
比如此處參數(shù)傳1,會彈出第1、2、3次保存的畫布狀態(tài),并根據(jù)第1次保存的畫布狀態(tài)進行恢復(fù)

這里還有一個方法:

public int getSaveCount()
獲取棧中保存畫布狀態(tài)的次數(shù),這個方法的最小返回值為1,即使彈出了所有的畫布狀態(tài),返回值依舊為1,代表默認狀態(tài)

四、Demo

最后給出一個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);//平移畫布,讓接下來的圖形繪制在上一次圖形的下面
        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);//平移畫布,讓接下來的圖形繪制在上一次圖形的下面
        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();
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容