Detect device orientation when Activity orientation is locked

后端 未结 3 1699
悲&欢浪女
悲&欢浪女 2020-12-06 22:27

In my project, I have to lock the orientation of an activity. (I cannot recreate the activity)

I want to display a message when the user change the orientation of th

3条回答
  •  我在风中等你
    2020-12-06 23:06

    It can also be solved with SensorManager. Here is a simple demo shows how to keep image orientation correct for locked activity to portrait:

    MainActivity :

    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.Surface;
    import android.widget.ImageView;
    
    public class MainActivity extends AppCompatActivity {
        private ImageView img;
    
        private SensorManager mSensorManager;
        private Sensor mOrientation;
    
        float value_0 = -10000;
        float value_1 = -10000;
        private SensorEventListener mOrientationSensorListener = new SensorEventListener() {
            int orientation = -1;
    
            @Override
            public void onSensorChanged(SensorEvent event) {
                int value ;
                if(value_0 == event.values[0] && value_1==event.values[1]){
                    return;
                }
    //            Log.d("values:", "values:" + event.values[0]+", "+event.values[1]);
                if (event.values[1] > 0 && event.values[0] == 0) {
                    value = Surface.ROTATION_0;//portrait
                    if (orientation != value) {
                        updateImageRotation(value);
                        Log.d("orientation", "portrait  + update");
                    }
                    orientation = value;
                    Log.d("orientation", "portrait ");
                }
    
    
                if (event.values[1] < 0 && event.values[0] == 0) {
                    value = Surface.ROTATION_180;//portrait reverse
                    if (orientation != value) {
                        updateImageRotation(value);
                        Log.d("orientation", "portrait reverse + update");
                    }
                    orientation = value;
                    Log.d("orientation", "portrait reverse");
                }
    
                if (event.values[0] > 0 && event.values[1] == 0) {
                    value = Surface.ROTATION_90;//portrait reverse
                    if (orientation != value) {
                        updateImageRotation(value);
                        Log.d("orientation", "landscape  + update");
                    }
                    orientation = value;
                    Log.d("orientation", "landscape");
                }
    
                if (event.values[0] < 0 && event.values[1] == 0) {
                    value = Surface.ROTATION_270;//portrait reverse
                    if (orientation != value) {
                        updateImageRotation(value);
                        Log.d("orientation", "landscape  + update");
                    }
                    orientation = value;
                    Log.d("orientation", "landscape reverse");
                }
    
                value_0=event.values[0];
                value_1=event.values[1];
            }
    
            @Override
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            img = (ImageView) findViewById(R.id.img);
            // Get sensor manager
            mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            // Get the default sensor of specified type
            mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            if (mOrientation != null) {
                mSensorManager.registerListener(mOrientationSensorListener, mOrientation,
                        SensorManager.SENSOR_DELAY_GAME);
            }
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            if (mOrientation != null) {
                mSensorManager.unregisterListener(mOrientationSensorListener);
            }
        }
    
    
        private void updateImageRotation(int degree) {
    
            switch (degree) {
                case Surface.ROTATION_0:
                    img.setRotation(0);
                    break;
                case Surface.ROTATION_90:
                    img.setRotation(90);
                    break;
                case Surface.ROTATION_180:
                    img.setRotation(180);
                    break;
                case Surface.ROTATION_270:
                    img.setRotation(270);
                    break;
            }
        }
    }
    

    In manifest. Orientation is locked to portrait and sensor is required as feature manifest.xml :

    
    
    
        
        
            
                
                    
    
                    
                
            
        
    
    
    

    activity_main.xml :

    
    
    
        
    
    

提交回复
热议问题