使用传感器在3 * 3矩阵上旋转/动画图像

时间:2018-12-06 13:38:15

标签: android

我正在实现传感器旋转矩阵,从SensorManager.getRotationMatrix获取读数(请参见下面的代码)。

我有一个imageView和3 * 3矩阵,我想将此矩阵应用于imageview以在xyz轴上旋转或设置动画。 但是我找不到任何完美或适当的解决方案来通过矩阵旋转图像视图。

我尝试过通过画布和相机旋转图像视图,但是没有用,请帮忙。 预先感谢!

这是我的大部分代码:

@Override
    public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType() ==  Sensor.TYPE_GAME_ROTATION_VECTOR){
            float[] rotMatrix = new float[9];
            float[] rotVals = new float[3];

            SensorManager.getRotationMatrixFromVector(rotMatrix, event.values);
            SensorManager.remapCoordinateSystem(rotMatrix,
                    SensorManager.AXIS_X, SensorManager.AXIS_Y, rotMatrix);

            SensorManager.getOrientation(rotMatrix, rotVals);
            float azimuth = (float) Math.toDegrees(rotVals[0]);
            float pitch = (float) Math.toDegrees(rotVals[1]);
            float roll = (float) Math.toDegrees(rotVals[2]);


            Matrix m = new Matrix();
            m.setValues(rotMatrix);
            imageView.setImageMatrix(m);

            }
    }

    another method tried is 

    @Override
    public void onSensorChanged(SensorEvent event) {
        // This timestep's delta rotation to be multiplied by the current rotation
        // after computing it from the gyro sample data.
        if (timestamp != 0) {
            final float dT = (event.timestamp - timestamp) * NS2S;
            // Axis of the rotation sample, not normalized yet.
            float axisX = event.values[0];
            float axisY = event.values[1];
            float axisZ = event.values[2];

            // Calculate the angular speed of the sample
            float omegaMagnitude = (float) sqrt(axisX*axisX + axisY*axisY + axisZ*axisZ);

            // Normalize the rotation vector if it's big enough to get the axis
            // (that is, EPSILON should represent your maximum allowable margin of error)
            if (omegaMagnitude > EPSILON) {
                axisX /= omegaMagnitude;
                axisY /= omegaMagnitude;
                axisZ /= omegaMagnitude;
            }

            // Integrate around this axis with the angular speed by the timestep
            // in order to get a delta rotation from this sample over the timestep
            // We will convert this axis-angle representation of the delta rotation
            // into a quaternion before turning it into the rotation matrix.
            float thetaOverTwo = omegaMagnitude * dT / 2.0f;
            float sinThetaOverTwo = (float) sin(thetaOverTwo);
            float cosThetaOverTwo =(float) cos(thetaOverTwo);
            deltaRotationVector[0] = sinThetaOverTwo * axisX;
            deltaRotationVector[1] = sinThetaOverTwo * axisY;
            deltaRotationVector[2] = sinThetaOverTwo * axisZ;
            deltaRotationVector[3] = cosThetaOverTwo;
        }
        timestamp = event.timestamp;
        float[] deltaRotationMatrix = new float[9];
        SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);

        Matrix matrix=new Matrix();
        matrix.setValues(deltaRotationMatrix);
        imageView.setImageMatrix(matrix);


        // User code should concatenate the delta rotation we computed with the current rotation
        // in order to get the updated rotation.
        // rotationCurrent = rotationCurrent * deltaRotationMatrix;

    }

0 个答案:

没有答案
相关问题