How to rotate a canvas at a specific point using android.graphics.Camera.rotateX(angle)

前端 未结 4 1055
一向
一向 2021-02-06 12:27

I am trying to use the Camera (android.graphics.Camera not the hardware camera) to rotate a views canvas around a specific point, in this instance the middle of the canvas.

4条回答
  •  故里飘歌
    2021-02-06 12:48

    I like to do things the difficult way and "roll my own". Moreover, if you are doing animations cumulative errors can creep in concat'ing Matrix's.

    float rotate; // rotation in degrees
    
    float vcx; // center of rotation of view
    float vcy;
    
    float gcx; // center of rotation of graphic
    float gcy;
    
    float theta = (float) (Math.PI/180.0*rotate);
    float sin = (float) Math.sin(theta);
    float cos = (float) Math.cos(theta);
    
    float[] a = new float[9];
    a[Matrix.MSCALE_X] = cos;
    a[Matrix.MSKEW_X] = sin;
    a[Matrix.MTRANS_X] = vcx-gcx*cos-gcy*sin;
    a[Matrix.MSCALE_Y] = cos;
    a[Matrix.MSKEW_Y] = -sin;
    a[Matrix.MTRANS_Y] = vcy-gcy*cos+gcx*sin;
    a[Matrix.MPERSP_0] = 0.0f;
    a[Matrix.MPERSP_1] = 0.0f;
    a[Matrix.MPERSP_2] = 1.0f;
    
    Matrix m = new Matrix();
    m.setValues(a);
    view.setImageMatrix(m); // or setMatrix or whatever you're using.
    

提交回复
热议问题