Canvas: canvas的save()和restore()

 

save(): 用来保存canvas的状态。

restore(): 用来恢复Canvas旋转、缩放等之后的状态,当和canvas.save( )一起使用时,恢复到canvas.save( )保存时的状态。

注意:

1. 这里的状态包括矩阵的变换状态,如:平移(Translate), 缩放(Scale)旋转(Rotate)倾斜(Skew), 以及画布的裁剪区域clip

2. Canvas的save()和restore()方法只会在有效范围内生效,它是绘制状态的存储器,并不是画布内容的存储器, 在canvas上绘制的路径和位图并不是绘制状态的一部分。

Demo:

1. 绘制一个简单的圆:

76f15af2e751962edb8d36b746a9afba.png

package com.yongdaimi.android.androidapitest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

public class ClockView extends android.view.View {

    private Paint mPaint;

    public ClockView(Context context) {
        this(context, null, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        mPaint.setColor(Color.RED);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //1
        int measuredWidth = getMeasuredWidth();
        int x = measuredWidth, y = measuredWidth;

        int cx = x / 2;

        int radius = x / 2;
        int cy = radius;

        // Draw background circle
        canvas.drawCircle(cx, cy, radius, mPaint);

    }
}

 2. 画类似仪表盘效果的刻度

58a2699f2658b496ec8129ed7ca23c11.png

package com.yongdaimi.android.androidapitest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

public class ClockView extends android.view.View {

    private Paint mPaint;

    public ClockView(Context context) {
        this(context, null, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        mPaint.setColor(Color.RED);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //1
        int measuredWidth = getMeasuredWidth();
        int x = measuredWidth, y = measuredWidth;

        int cx = x / 2;

        int radius = x / 2;
        int cy = radius;

        // Draw background circle
        canvas.drawCircle(cx, cy, radius, mPaint);

        // 2.Draw scale
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(x / 2, y / 8, x / 2, 0, mPaint);
        canvas.save();

        for (int i = 0; i < 3; i++) {
            canvas.rotate(30, x / 2, y / 2); // 注意这里的后两个参数:代表以哪个位置为中心开始旋转,默认是以(0,0)位置为中心开始旋转,这里是以该圆的圆心为中心点开始旋转。
            canvas.drawLine(x / 2, y / 8, x / 2, 0, mPaint);
        }

    }
}

此时,画布已经顺时针旋转了3个30度,即90度。并且,在旋转画布之前已经调用了canvas.save()方法,所以此时保存的canvas的状态是未旋转时的正常状态。

3. 继续画一条直线,观察效果

6d2e1d86f9fa1bee965fec935340250e.png

package com.yongdaimi.android.androidapitest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

public class ClockView extends android.view.View {

    private Paint mPaint;

    public ClockView(Context context) {
        this(context, null, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        mPaint.setColor(Color.RED);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //1
        int measuredWidth = getMeasuredWidth();
        int x = measuredWidth, y = measuredWidth;

        int cx = x / 2;

        int radius = x / 2;
        int cy = radius;

        // Draw background circle
        canvas.drawCircle(cx, cy, radius, mPaint);

        // 2.Draw scale
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(x / 2, y / 8, x / 2, 0, mPaint);
        canvas.save();

        for (int i = 0; i < 3; i++) {
            canvas.rotate(30, x / 2, y / 2);
            canvas.drawLine(x / 2, y / 8, x / 2, 0, mPaint);
        }
        
        //3
        mPaint.setColor(Color.YELLOW);
        mPaint.setStrokeWidth(10);
        canvas.drawLine(x / 2, y / 2, x / 2, y / 4, mPaint);
    }
}

上面对画笔进行加粗,并且把画笔的颜色变成了黄色,注意到当前线条的方向和最后一条仪表盘刻度的方向是一致的(都旋转了90度)。下面在绘制黄线的code之前,调用 canvas.restore() 恢复画布的状态:

package com.yongdaimi.android.androidapitest.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;

import androidx.annotation.Nullable;

public class ClockView extends android.view.View {

    private Paint mPaint;

    public ClockView(Context context) {
        this(context, null, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(6);
        mPaint.setColor(Color.RED);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //1
        int measuredWidth = getMeasuredWidth();
        int x = measuredWidth, y = measuredWidth;

        int cx = x / 2;

        int radius = x / 2;
        int cy = radius;

        // Draw background circle
        canvas.drawCircle(cx, cy, radius, mPaint);

        // 2.Draw scale
        mPaint.setColor(Color.BLUE);
        canvas.drawLine(x / 2, y / 8, x / 2, 0, mPaint);
        canvas.save();

        for (int i = 0; i < 3; i++) {
            canvas.rotate(30, x / 2, y / 2);
            canvas.drawLine(x / 2, y / 8, x / 2, 0, mPaint);
        }

        canvas.restore();//如果不加这行,黄线就会和上面一样
        
        mPaint.setColor(Color.YELLOW);
        mPaint.setStrokeWidth(10);
        canvas.drawLine(x / 2, y / 2, x / 2, y / 4, mPaint);
    }
}

运行:

7d92f35dfbfbab3e957879cddb08e786.png

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值