Tilt the image to the titration from any angle

I try to tilt the image at any angle, but I can’t do it. Here I provide the code that I worked on.

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
        text1.setText("X: " + x);
        text2.setText("Y: " + y);
        text3.setText("Z: " + z);
        if (Math.abs(x) > Math.abs(y)) {
            if (x < 0) {
                //Matrix matrix = new Matrix();
                //iv.setScaleType(ScaleType.MATRIX); // required
                //matrix.postRotate((float) 25, x, y);
                //iv.setImageMatrix(matrix);
             iv.setImageResource(R.drawable.images);
            }
            if (x > 0) {

                 iv.setImageResource(R.drawable.images);
            }
        } else {
            if (y < 0) {

                 iv.setImageResource(R.drawable.images);
            }
            if (y > 0) {

                 iv.setImageResource(R.drawable.images);
            }
        }
        if (x > (-2) && x < (2) && y > (-2) && y < (2)) {

             iv.setImageResource(R.drawable.images);
        }   }}

. Can anyone suggest how to make sure

+4
source share
2 answers

you should try something in the following order to rotate the image:

Matrix matrix=new Matrix();
imageView.setScaleType(ScaleType.MATRIX);   //required
matrix.postRotate((float) angle, pivX, pivY);
imageView.setImageMatrix(matrix);

Please give me some feedback.

Hope this helps.

+3
source

Its pretty simple. You do not need to write this code. Use yourImageView.setRotation (Rotation_angle) to tilt the image by certain degrees according to the value of Rotation_angle.

, - . :)

+2

Source: https://habr.com/ru/post/1524043/


All Articles