基于方向的旋转视图已更改

时间:2016-09-12 08:00:48

标签: android rotation

我想像默认相机一样创建图标方向。旋转设备时,图标会相应旋转。但有时,它旋转270度,默认相机中的图标仅旋转0 - 180度。 这是我的源代码。

** RotationActivity **

public class RotationActivity extends Activity {

    public static final String TAG = "RotationActivity";

    private ImageView btnTest;


    OrientationEventListener orientationEventListener;
    private int current_orientation = 0;
    private int old_ui_rotation = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rotation_activity);

        btnTest = (ImageView) findViewById(R.id.imvImage);

        orientationEventListener = new  OrientationEventListener(RotationActivity.this) {

            @Override
            public void onOrientationChanged(int orientation) {

                orientationChanged(orientation);
            }
        };
    }

    private void orientationChanged(int orientation) {

        if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
            return;
        }
        int diff = Math.abs(orientation - current_orientation);
        if (diff > 180) {
            diff = 360 - diff;
        }
        // only change orientation when sufficiently changed
        if (diff > 60) {
            orientation = (orientation + 45) / 90 * 90;
            orientation = orientation % 360;
            if (orientation != current_orientation) {
                this.current_orientation = orientation;
                Log.d(TAG, "current_orientation is now: " + current_orientation);
                changeUI();
            }
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        orientationEventListener.enable();
    }

    @Override
    protected void onStop() {
        super.onStop();
        orientationEventListener.disable();
    }

    private static final float PIVOT_VALUE = 0.5f;
    private static final long DEFAULT_ROTATE_DURATION_MS = 500;

    private void changeUI() {
        int rotation = this.getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation)

        {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        int relative_orientation = (current_orientation + degrees) % 360;
        Log.d(TAG, "    current_orientation = " + current_orientation);
        Log.d(TAG, "    degrees = " + degrees);
        Log.d(TAG, "    relative_orientation = " + relative_orientation);
        final int ui_rotation = (360 - relative_orientation) % 360;
        Log.d(TAG, "    ui_rotation = " + ui_rotation);

        RotateAnimation rotateAnimation = new RotateAnimation(old_ui_rotation,
                ui_rotation,
                RotateAnimation.RELATIVE_TO_SELF, PIVOT_VALUE,
                RotateAnimation.RELATIVE_TO_SELF, PIVOT_VALUE);
        rotateAnimation.setDuration(DEFAULT_ROTATE_DURATION_MS);
        rotateAnimation.setFillAfter(true);

        btnTest.startAnimation(rotateAnimation);

        old_ui_rotation = ui_rotation;
    }

}

rotation_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imvImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher" />


</LinearLayout>

非常感谢。

2 个答案:

答案 0 :(得分:1)

int diff = Math.abs(orientation - current_orientation);

我认为问题在于您忽略了指示旋转方向的符号。您应该使用该符号来确定是旋转90°还是-90°。

答案 1 :(得分:1)

我认为问题出在这里:

final int ui_rotation = (360 - relative_orientation) % 360;

ui_rotation将永远不会是360.如果您将设备从270(old_ui_rotation)旋转到360,而不是将图标旋转90,它将旋转270.可以添加此项:

if((old_ui_rotation==270)&&(ui_rotation==0)) old_ui_rotation = -90;