Arm*_*man 28 android screen-orientation orientation-changes
我希望获得手机方向,但保持屏幕方向为纵向.因此,无论用户将手机转为横向还是纵向,视图都保持不变,但我可以看到它是转向横向还是纵向.
将活动设置为android:screenOrientation ="portrait"将修复两者但我无法通过以下方式检测到手机方向
public void onConfigurationChanged(Configuration newConfig) {
switch (newConfig.orientation) {
case Configuration.ORIENTATION_PORTRAIT:
Toast.makeText(this, "Portrait", Toast.LENGTH_SHORT).show();
break;
case Configuration.ORIENTATION_LANDSCAPE:
Toast.makeText(this, "Landscape", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?
edr*_*ian 32
这是一个用于轻松管理屏幕方向更改的多功能类:
public class OrientationManager extends OrientationEventListener {
public enum ScreenOrientation {
REVERSED_LANDSCAPE, LANDSCAPE, PORTRAIT, REVERSED_PORTRAIT
}
public ScreenOrientation screenOrientation;
private OrientationListener listener;
public OrientationManager(Context context, int rate, OrientationListener listener) {
super(context, rate);
setListener(listener);
}
public OrientationManager(Context context, int rate) {
super(context, rate);
}
public OrientationManager(Context context) {
super(context);
}
@Override
public void onOrientationChanged(int orientation) {
if (orientation == -1){
return;
}
ScreenOrientation newOrientation;
if (orientation >= 60 && orientation <= 140){
newOrientation = ScreenOrientation.REVERSED_LANDSCAPE;
} else if (orientation >= 140 && orientation <= 220) {
newOrientation = ScreenOrientation.REVERSED_PORTRAIT;
} else if (orientation >= 220 && orientation <= 300) {
newOrientation = ScreenOrientation.LANDSCAPE;
} else {
newOrientation = ScreenOrientation.PORTRAIT;
}
if(newOrientation != screenOrientation){
screenOrientation = newOrientation;
if(listener != null){
listener.onOrientationChange(screenOrientation);
}
}
}
public void setListener(OrientationListener listener){
this.listener = listener;
}
public ScreenOrientation getScreenOrientation(){
return screenOrientation;
}
public interface OrientationListener {
public void onOrientationChange(ScreenOrientation screenOrientation);
}
}
Run Code Online (Sandbox Code Playgroud)
这样更简单,可重用,您还可以获得REVERSE_LANDSCAPE和REVERSE_PORTRAIT方向.
您必须实现OrientationListener才能在发生方向更改时收到通知.
不要忘记调用orientationManager.enable()开始方向跟踪,然后调用orientationManager.disable()(这两个方法都继承自OrientationEventListener类)
更新:用例示例
MyFragment extends Fragment implements OrientationListener {
...
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
orientationManager = new OrientationManager(getActivity(), SensorManager.SENSOR_DELAY_NORMAL, this);
orientationManager.enable();
}
@Override
public void onOrientationChange(ScreenOrientation screenOrientation) {
switch(screenOrientation){
case PORTRAIT:
case REVERSED_PORTRAIT:
MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
case REVERSED_LANDSCAPE:
MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
break;
case LANDSCAPE:
MainActivityBase.getInstance().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Phi*_*ons 20
你能用加速度计满足你的要求吗?如果是这样的话,也许这样的(未经测试的)片段会适合您的目的.
SensorManager sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(new SensorEventListener() {
int orientation=-1;;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[1]<6.5 && event.values[1]>-6.5) {
if (orientation!=1) {
Log.d("Sensor", "Landscape");
}
orientation=1;
} else {
if (orientation!=0) {
Log.d("Sensor", "Portrait");
}
orientation=0;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11894 次 |
| 最近记录: |