android 屏幕旋转(横屏、竖屏、反向横屏、反向竖屏)的实现

android 屏幕旋转(横屏、竖屏、反向横屏、反向竖屏)的实现

前言:根据屏幕的旋转自动实现应用界面旋转,横屏、竖屏、反向横屏、反向竖屏。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_ori"
        android:text="方向"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView
        android:src="@mipmap/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</android.support.constraint.ConstraintLayout>

监听器类:

public class OrientationSensorListener implements SensorEventListener {

    private static final int DATA_X = 0;
    private static final int DATA_Y = 1;
    private static final int DATA_Z = 2;

    public static final int ORIENTATION_UNKNOWN = -1;

    private Handler rotateHandler;

    public OrientationSensorListener(Handler handler) {
        rotateHandler = handler;
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        float[] values = sensorEvent.values;
        int orientation = ORIENTATION_UNKNOWN;
        float X = -values[DATA_X];
        float Y = -values[DATA_Y];
        float Z = -values[DATA_Z];
        float magnitude = X * X + Y * Y;
        // Don't trust the angle if the magnitude is small compared to the y value
        if (magnitude * 4 >= Z * Z) {
            float OneEightyOverPi = 57.29577957855f;
            float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;
            orientation = 90 - (int) Math.round(angle);
            // normalize to 0 - 359 range
            while (orientation >= 360) {
                orientation -= 360;
            }
            while (orientation < 0) {
                orientation += 360;
            }
        }

        if (rotateHandler != null) {
            rotateHandler.obtainMessage(888, orientation, 0).sendToTarget();
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}

Activity类

public class OrientationActivity extends AppCompatActivity {

    private Button button;

    private Handler handler;
    private OrientationSensorListener listener;
    private SensorManager sm;

    private Sensor sensor;

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

        Log.i("mylog:", "onCreate()");

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 888) {
                    int orientation = msg.arg1;
                    if (orientation > 45 && orientation < 135) {
                        OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                        Log.d("mylog", "横屏翻转: ");
                    } else if (orientation > 135 && orientation < 225) {
                        OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
                        Log.d("mylog", "竖屏翻转: ");
                    } else if (orientation > 225 && orientation < 315) {
                        OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                        Log.d("mylog", "横屏: ");
                    } else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {
                        OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                        Log.d("mylog", "竖屏: ");
                    }
                }
                super.handleMessage(msg);
            }
        };

        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        listener = new OrientationSensorListener(handler);
        sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);

        button = (Button) findViewById(R.id.btn_ori);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("mylog:", getResources().getConfiguration().orientation +"");
            }
        });

    }

    @Override
    protected void onPause() {
        super.onPause();
        sm.unregisterListener(listener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
    }
}

AndroidManifest.xml文件

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".com.OrientationActivity"
            android:configChanges="keyboardHidden|screenSize|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

    </application>

</manifest>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Windows 10中使用QML实现屏幕上下翻转,可以使用以下步骤: 1. 创建一个新的QML项目,并在主窗口中添加一个Rectangle元素。 2. 在Rectangle元素中添加一个MouseArea元素,并设置其acceptedButtons属性为Qt.LeftButton和Qt.RightButton,以便支持触摸操作。 3. 添加一个Rotation元素作为Rectangle元素的子元素,并设置其origin属性为Rectangle的中心点,以便在旋转时保持中心点不变。 4. 在MouseArea元素中添加onPressed和onPositionChanged信号的处理程序,并计算触摸点与屏幕中心点之间的角度。 5. 将计算出的角度应用于Rotation元素的angle属性,以实现屏幕的上下翻转。 下面是一个示例代码: ``` import QtQuick 2.0 Rectangle { id: root width: 640 height: 480 color: "black" Rotation { id: rotation origin.x: root.width / 2 origin.y: root.height / 2 axis.x: 0 axis.y: 0 angle: 0 } MouseArea { id: mouseArea anchors.fill: parent acceptedButtons: Qt.LeftButton | Qt.RightButton property real lastAngle: 0 onPressed: { mouseArea.lastAngle = Math.atan2(mouseY - rotation.origin.y, mouseX - rotation.origin.x) * 180 / Math.PI } onPositionChanged: { var angle = Math.atan2(mouseY - rotation.origin.y, mouseX - rotation.origin.x) * 180 / Math.PI rotation.angle += angle - mouseArea.lastAngle mouseArea.lastAngle = angle } } } ``` 这个示例代码中,通过计算鼠标移动的角度来实现屏幕旋转,使用了Math.atan2()函数来计算角度,其中lastAngle属性用于保存上一次计算的角度,以便计算当前角度与上一次角度之间的差值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值