Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

android - Detect device orientation when Activity orientation is locked

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 the device that say : "The View is locked in this position ..."

I can easily lock the orientation with this code:

if(version >= Build.VERSION_CODES.JELLY_BEAN_MR2){
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
    }else{
        int orientation = activity.getResources().getConfiguration().orientation;
        if(orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }else{
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
    }

With this code the view is locked but I cannot detect when the user change the device orientation.

And if I use this following code:

@Override
   public void onConfigurationChanged(Configuration newConfig) {
     //Check if the orientation has change
     if(newConfig.orientation != mOrientation){
        Toast.makeText(this, "Message for user", Toast.LENGTH_SHORT).show();
     }

     //Try to lock the view but not working
     newConfig.orientation = mOrientation;

     //Obligatory line that reload the view and cannot be delete
     //(is the biggest prolem of this code)
     super.onConfigurationChanged(newConfig);
   }

If someone know a way to lock the view and detect the change of the device orientation it will really help me.

Thanks all

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 :

<?xml version="1.0" encoding="utf-8"?>
<manifest package="permissionsmngr.com.apipas.rotationmanage"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-feature
        android:name="android.hardware.sensor.accelerometer"
        android:required="true"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="permissionsmngr.com.apipas.rotationmanage.MainActivity">

    <ImageView
        android:layout_centerInParent="true"
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        />
</RelativeLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...