Android canvas平移 缩放 旋转 倾斜 切割 矩阵

translate 平移后原点(0,0)坐标点的位置也会移动

 canvas.drawRect(0,0, 400, 400, mPaint);
 canvas.translate(50, 50);
 mPaint.setColor(Color.GRAY);
 canvas.drawRect(0,0, 400, 400, mPaint);
 canvas.drawLine(0, 0, 600,600, mPaint);
平移.png

scale 缩放

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.drawLine(0,0, 400, 600, mPaint);

rotate 旋转

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

skew 倾斜

        canvas.drawRect(0,0, 400, 400, mPaint);
//        canvas.skew(1, 0); //在X方向倾斜45度,Y轴逆时针旋转45
        canvas.skew(0, 1); //在y方向倾斜45度, X轴顺时针旋转45
        mPaint.setColor(Color.GRAY);
        canvas.drawRect(0, 0, 400, 400, mPaint);

clip 切割

//切割所选区域
        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);//坐标超出裁剪区域,无法绘制

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

你可能感兴趣的:(Android canvas平移 缩放 旋转 倾斜 切割 矩阵)