Canvas使用技巧

作者: migill | 来源:发表于2019-10-02 17:02 被阅读0次

    1、绘制几何图像/位图

    void drawBitmap(Bitmap bitmap, float left, float top, Paint paint):在指定位置绘制位图.
    void drawLine(float startX, float startY, float stopX, float stopY,Paint paint):根据给定的起始点和结束点之间绘制连线.
    void drawPath(Path path, Paint paint):根据给定的path,绘制连线.
    void drawPoint(float x, float y, Paint paint):根据给定的坐标,绘制点.

    2、位置/形状变换

    1、平移操作

            //1、平移操作
            mPaint.setColor(Color.RED);
            canvas.drawRect(0,0, 400, 400, mPaint);
            canvas.translate(50, 50);
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(0,0, 400, 400, mPaint);
    

    2、缩放操作

            //缩放操纵
            canvas.drawRect(200,200, 700,700, mPaint);
            canvas.scale(0.5f, 0.5f);
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(200,200, 700,700, mPaint);
    
            //缩放操纵
            canvas.drawRect(200,200, 700,700, mPaint);
            //先translate(px, py),再scale(sx, sy),再反translate
            canvas.scale(0.5f, 0.5f, 200,200);
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(200,200, 700,700, mPaint);
    

    3、旋转操作

            //旋转操作
            canvas.translate(50,50);
            canvas.drawRect(0,0, 700,700, mPaint);
            canvas.rotate(45);
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(0,0, 700,700, mPaint);
    
            //旋转操作
            canvas.drawRect(400, 400, 900, 900, mPaint);
            canvas.rotate(45, 650, 650); //px, py表示旋转中心的坐标
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(400, 400, 900, 900, mPaint);
    

    4、倾斜操作
             //倾斜操作
            canvas.skew(1, 0); //在X方向倾斜45度,Y轴逆时针旋转45
            canvas.skew(0, 1); //在y方向倾斜45度, X轴顺时针旋转45
    

    5、切割操作

            //切割
            canvas.drawRect(200, 200,700, 700, mPaint);
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(200, 800,700, 1300, mPaint);
            canvas.clipRect(200, 200,700, 700); //画布被裁剪
            canvas.drawCircle(100,100, 100,mPaint); //坐标超出裁剪区域,无法绘制
            canvas.drawCircle(300, 300, 100, mPaint); //坐标区域在裁剪范围内,绘制成功
    
            //切割
            canvas.drawRect(200, 200,700, 700, mPaint);
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(200, 800,700, 1300, mPaint);
            canvas.clipOutRect(200,200,700,700); //画布裁剪外的区域
            canvas.drawCircle(100,100,100,mPaint); //坐标区域在裁剪范围内,绘制成功
            canvas.drawCircle(300, 300, 100, mPaint);//坐标超出裁剪区域,无法绘制
    

    6、通过matrix实现平移,缩放,旋转等操作

            //矩阵
            canvas.drawRect(0,0,700,700, mPaint);
            Matrix matrix = new Matrix();
            matrix.setTranslate(50,50);
            matrix.setRotate(45);
            matrix.setScale(0.5f, 0.5f);
            canvas.setMatrix(matrix);
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(0,0,700,700, mPaint);
    

    3、状态保存和恢复

    Canvas调用了translate、rotate、scale、skew、clipRect等变化后,后续的操作都是基于变换后的Canvas,都会受到影响,对于后续的操作很不方便,Canvas提供了save、saveLayer、saveLayerAlpha、restore、restoreToCount来保护和恢复状态。
    1.canvas内部对于状态的保存存放在栈中
    2.可以多次调用save保存canvas的状态,并且可以通过getSaveCount方法获取保存的状态个数
    3.可以通过restore方法返回最近一次save前的状态,也可以通过restoreToCount返回指定save状态。指定save状态之后的状态全部被清除
    4.saveLayer可以创建新的图层,之后的绘制都会在这个图层之上绘制,直到调用restore方法
    注意:绘制的坐标系不能超过图层的范围, saveLayerAlpha对图层增加了透明度信息

            int state = canvas.save();//第一次保存
            canvas.translate(50,50);
            mPaint.setColor(Color.GRAY);
            canvas.drawRect(0,0,500,500, mPaint);
            canvas.save();
            canvas.translate(50,50);
            mPaint.setColor(Color.BLUE);
            canvas.drawRect(0,0,500,500, mPaint);
            canvas.restoreToCount(state);
    
            canvas.drawRect(200,200, 700,700, mPaint);
      
            int layerId = canvas.saveLayer(0,0, 700, 700, mPaint);
            mPaint.setColor(Color.GRAY);
            Matrix matrix = new Matrix();
            matrix.setTranslate(100,100);
            canvas.setMatrix(matrix);
            canvas.drawRect(0,0,700,700, mPaint); //由于平移操作,导致绘制的矩形超出了图层的大小,所以绘制不完全
            canvas.restoreToCount(layerId);
    
            mPaint.setColor(Color.RED);
            canvas.drawRect(0,0,100,100, mPaint);
    

    3、粒子爆炸效果

    这个动画效果是通过属性动画的更新不断的去更新粒子x,y, vX,vY的值,重新绘制,通过不断的绘制达到爆炸的效果。

    /**
     * 粒子封装对象
     */
    public class Ball {
    
        public int color; //图片像素点颜色值
        public float x; //粒子圆心坐标x
        public float y; //粒子圆心坐标y
        public float r; //粒子半径
    
        public float vX;//粒子运动水平方向速度
        public float vY;//粒子运动垂直方向速度
        public float aX;//粒子运动水平方向加速度
        public float aY;//粒子运动垂直方向加速度
    
    }
    
    public class SplitView extends View {
    
        private Paint mPaint;
        private Bitmap mBitmap;
        private float d = 3;//粒子直径
        private ValueAnimator mAnimator;
        private List<Ball> mBalls = new ArrayList<>();
    
        public SplitView(Context context) {
            this(context, null);
        }
    
        public SplitView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public SplitView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            mPaint = new Paint();
            mBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.pic);
            for (int i = 0; i < mBitmap.getWidth(); i++) {
                for (int j = 0; j < mBitmap.getHeight(); j++) {
                    Ball ball = new Ball();
                    ball.color = mBitmap.getPixel(i, j);
                    ball.x = i * d + d / 2;
                    ball.y = j * d + d / 2;
                    ball.r = d / 2;
    
                    //速度(-20,20)
                    ball.vX = (float) (Math.pow(-1, Math.ceil(Math.random() * 1000)) * 20 * Math.random());
                    ball.vY = rangInt(-15, 35);
                    //加速度
                    ball.aX = 0;
                    ball.aY = 0.98f;
    
                    mBalls.add(ball);
                }
            }
    
            mAnimator = ValueAnimator.ofFloat(0, 1);
            mAnimator.setRepeatCount(1);
            mAnimator.setDuration(2000);
            mAnimator.setInterpolator(new LinearInterpolator());
            mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    updateBall();
                    invalidate();
                }
            });
    
        }
    
    
    
        private int rangInt(int i, int j) {
            int max = Math.max(i, j);
            int min = Math.min(i, j) - 1;
            //在0到(max - min)范围内变化,取大于x的最小整数 再随机
            return (int) (min + Math.ceil(Math.random() * (max - min)));
        }
    
    
        private void updateBall() {
            //更新粒子的位置
            for (Ball ball : mBalls) {
                ball.x += ball.vX;
                ball.y += ball.vY;
    
                ball.vX += ball.aX;
                ball.vY += ball.aY;
            }
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            canvas.translate(500, 500);
            for (Ball ball : mBalls) {
                mPaint.setColor(ball.color);
                canvas.drawCircle(ball.x, ball.y, ball.r, mPaint);
            }
    
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                //执行动画
                mAnimator.start();
            }
            return super.onTouchEvent(event);
        }
    }
    

    相关文章

      网友评论

        本文标题:Canvas使用技巧

        本文链接:https://www.haomeiwen.com/subject/elbkpctx.html