`

Android的screenOrientation设置

 
阅读更多

首先在AndroidManifest.xml中,对需要设置的Activity加上如下两条属性:

   android:configChanges="orientation|keyboard" (转屏时不重置Activity,
	若设置了此项,则无法为横向和纵向分别设置不同的布局。因为转屏时不会删除并重新加载)
    android:screenOrientation="sensor" (开启重力传感器) 

 转屏函数:

import android.content.pm.ActivityInfo;

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 
//强制指定纵向 value为5

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); 
//强制指定反方向纵向 value为9

 重力传感器监测:

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class asd extends Activity{
	SensorManager mManager;
	Sensor mSensor;
	SensorEventListener mListener;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setRequestedOrientation(5);//初始化屏幕方向
	       mManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 
	       mSensor = mManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
	       mListener = new SensorEventListener() { 
	        public void onAccuracyChanged(Sensor sensor, int accuracy) { 
	          };
	        public void onSensorChanged(SensorEvent event) { 
	         float x = event.values[SensorManager.DATA_X]; 
	         float y = event.values[SensorManager.DATA_Y]; 
	         float z = event.values[SensorManager.DATA_Z]; 
	         setCustomOrientation(x, y, z); 
	        }; 
	       }; 
	       mManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_GAME);
	}
	
	public void setCustomOrientation(float x, float y, float z) { 
		 
		 if(y > 1.5)
		 {
		  //setRequestedOrientation(
		  //	ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
		   setRequestedOrientation(5);
		 }
		 else if(y < -1.5)
		 {
		     setRequestedOrientation(9);
		 }
		};
		@Override  
		protected void onResume() {   
		    super.onResume(); 
		 mManager.registerListener(mListener, mSensor, 
		SensorManager.SENSOR_DELAY_GAME); 
		};

		@Override  
		protected void onPause() {   
		    super.onPause(); 
		 mManager.unregisterListener(mListener); 
		};
}

 

分享到:
评论

相关推荐

    Android XML設置屏幕方向(android:screenOrientation)详解

    以下是关于`android:screenOrientation`的详细解释和可用的设置选项。 1. **默认值:unspecified** 当设置为"unspecified"时,系统将根据设备的硬件和用户偏好来自动选择屏幕方向。在不同的设备上,这可能会导致横...

    android 横竖屏限制的配置方法

    总的来说,Android开发者可以通过设置AndroidManifest.xml中的`android:screenOrientation`属性或者在代码中调用`setRequestedOrientation()`方法,灵活控制应用的横竖屏显示,以满足不同场景下的用户体验需求。...

    Android 横竖屏切换

    禁止App内横竖屏切换的方法是在AndroidManifest.xml文件中对需要固定方向的Activity设置android:screenOrientation属性。该属性有几个可选值: 1. "unspecified":默认值,系统将根据设备的当前状态来判断显示方向...

    Android屏幕旋转适配

    首先,我们可以通过在AndroidManifest.xml中对特定Activity设置`android:configChanges`属性来控制屏幕旋转的行为。例如,添加`android:configChanges="orientation|screenSize"`,这会让系统知道当屏幕方向或尺寸...

    android网络视频播放器(完整可运行).zip

    视频&lt;activity android:name="com.android.ui.TypeActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar" android:launchMode="singleTask"/&gt; &lt;activity android:name=...

    Android代码-仿微信app.rar

    android:screenOrientation="portrait" android:theme="@style/horizontal_slide" android:windowSoftInputMode="adjustResize"&gt; android:name=...

    android 手机强制横竖屏

    通过在布局文件中使用`android:screenOrientation`属性,可以设置Activity的初始屏幕方向。例如: ```xml android:name=".YourActivity" android:screenOrientation="landscape"&gt; &lt;!-- 强制横屏 --&gt; ... ``` ...

    Android activity属性设置大全.doc

    17. android:screenOrientation=["unspecified" | "user" | "behind" | "landscape" | "portrait" | "sensor" | "nonsensor"] 控制activity的屏幕方向。"unspecified"表示由系统决定,"landscape"和"portrait"分别...

    Android studio通过切换不同layout实现横竖屏适配

    找到对应的标签,添加`android:screenOrientation`属性: ```xml android:name=".MainActivity" android:screenOrientation="sensorLandscape"&gt; &lt;!-- 对应横屏 --&gt; &lt;!-- 或者 --&gt; &lt;!-- android:...

    Android禁止横屏竖屏切换的有效方法

    在AndroidManifest.xml的activity(需要禁止转向的activity)配置中加入android:screenOrientation=”landscape”属性即可(landscape是横向,portrait是纵向)。例如:  &lt;?xml version=1.0 encoding=utf-8?&gt; &...

    android竖屏切换横屏

    你可以在标签内设置`screenOrientation`属性来指定默认启动时的屏幕方向。例如,若要强制应用始终处于横屏模式,可以这样写: ```xml &lt;activity android:name=".YourActivity" android:screenOrientation=...

    Android之横竖屏切换

    很多软件在开发过程中为了避免横竖屏切换时引发不必要的麻烦,通常禁止掉横竖屏的切换,即通过在 AndroidManifest.xml 中设置 activity 中的 android:screenOrientation 属性值来实现。 android:screenOrientation ...

    Android自适应屏幕大小和layout布局(横屏竖屏)

    - 对于每个Activity,可以设置其`screenOrientation`属性以指定固定的屏幕方向。 - 例如,在`AndroidManifest.xml`文件中为某个Activity添加`android:screenOrientation="portrait"`表示竖屏模式,`android:...

    Android webview调用摄像头扫描二维码

    android:screenOrientation="portrait" android:clearTaskOnLaunch="true" android:configChanges="orientation|keyboardHidden" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:...

    Android应用源码之(横竖屏切换处理-IT计算机-毕业设计.zip

    2. **自定义处理**:设置`android:configChanges="orientation"`或`android:configChanges="screenOrientation"`,当屏幕方向改变时,系统会调用`onConfigurationChanged(Configuration newConfig)`方法,让你在不...

    android屏幕自适应案例详解

    10. **适配横竖屏**:通过在AndroidManifest.xml中配置Activity的screenOrientation属性,可以指定应用在横屏或竖屏模式下的表现。 11. **使用RecyclerView和GridLayoutManager**:RecyclerView是一个高效的列表...

    Android 销毁当前的Activity

    另外,Activity 中还有一些其他的属性和屏幕方向有关,如 `android:screenOrientation`。这个属性可以指定 Activity 的默认屏幕方向,例如: ```xml ... android:screenOrientation="landscape" ...&gt; ... ```...

    Android 实现移动选择横竖屏和飞行模式

    android:screenOrientation="landscape"&gt; &lt;!-- 横屏 --&gt; &lt;!-- 或者 --&gt; android:screenOrientation="portrait"&gt; &lt;!-- 竖屏 --&gt; ``` 此外,还可以在代码中动态切换屏幕方向,例如在某个事件触发时: ```java ...

Global site tag (gtag.js) - Google Analytics