[英]Get phone orientation but fix screen orientation to portrait
我想獲得手機方向,但將屏幕方向保持為縱向。 所以無論用戶把手機調成橫向還是縱向,視圖都保持不變,但我可以知道它是橫向還是縱向。
將活動設置為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;
}
}
有誰知道如何解決這個問題?
這是一個用於輕松管理屏幕方向更改的多用途類:
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);
}
}
這是更簡單、可重用的方式,您還可以獲得 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;
}
}
}
你能用加速度計滿足你的要求嗎? 如果是這樣,也許像這樣(未經測試的)片段會適合您的目的。
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);
我需要一個解決方案,該解決方案只能按需為我提供定位。 這個對我有用:
public class SensorOrientationChecker {
public final String TAG = getClass().getSimpleName();
int mOrientation = 0;
private SensorEventListener mSensorEventListener;
private SensorManager mSensorManager;
private static SensorOrientationChecker mInstance;
public static SensorOrientationChecker getInstance() {
if (mInstance == null)
mInstance = new SensorOrientationChecker();
return mInstance;
}
private SensorOrientationChecker() {
mSensorEventListener = new Listener();
Context applicationContext = GlobalData.getInstance().getContext();
mSensorManager = (SensorManager) applicationContext.getSystemService(Context.SENSOR_SERVICE);
}
/**
* Call on activity onResume()
*/
public void onResume() {
mSensorManager.registerListener(mSensorEventListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
/**
* Call on activity onPause()
*/
public void onPause() {
mSensorManager.unregisterListener(mSensorEventListener);
}
private class Listener implements SensorEventListener {
@Override
public void onSensorChanged(SensorEvent event) {
float x = event.values[0];
float y = event.values[1];
if (x<5 && x>-5 && y > 5)
mOrientation = 0;
else if (x<-5 && y<5 && y>-5)
mOrientation = 90;
else if (x<5 && x>-5 && y<-5)
mOrientation = 180;
else if (x>5 && y<5 && y>-5)
mOrientation = 270;
//Log.e(TAG,"mOrientation="+mOrientation+" ["+event.values[0]+","+event.values[1]+","+event.values[2]+"]");
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
public int getOrientation(){
return mOrientation;
}
}
如果您禁用屏幕方向更改,那么顯然永遠不會調用 onConfigurationChanged ...
我認為唯一的方法是使用加速度計傳感器,檢查此鏈接。
這比編寫一個全新的類要簡單得多:
final OrientationEventListener orientationEventListener = new OrientationEventListener( getApplicationContext() ) {
@Override
public void onOrientationChanged( final int orientation ) {
Log.i("", "orientation = " + orientation );
}
};
orientationEventListener.enable();
要進入您想要在該活動的清單文件中設置的內容
android:configChanges="orientation|keyboardHidden"
然后當用戶旋轉手機時,它將進入 public void onConfigurationChanged() 方法。 也刪除
android:screenOrientation="portrait"
來自同一個活動。
如果有人正在尋找該問題的 Webview/javascript 解決方案,下面可以做到這一點。
這將觸發窗口上的自定義“翻轉”事件,並帶有“額外參數”,因為 jquery 具有它們。 它還設置 window.flip,類似於 window.orientation:
$(window).on('flip',function(ev,angle,orientation) {
console.log(angle,orientation);
alert(window.flip);
});
if (window.DeviceOrientationEvent) {
jQuery.flip = {
debug : false,
interval : 1000,
checked : false,
betaflat : 25,
gammaflat : 45,
orientation : 'portrait-primary',
angles : {
'portrait-primary' : 0,
'portrait-secondary' : 0,
'landscape-primary' : 90,
'landscape-secondary' : -90
},
timer : null,
check : function(ev) {
if (!this.checked) {
var trigger=false;
if (this.debug) console.log([ev.alpha,ev.beta,ev.gamma]);
if (ev.beta>this.betaflat) {
// if beta is big its portrait
if (this.debug) console.log('beta portrait pri');
if (this.orientation!='portrait-primary') {
this.orientation='portrait-primary';
trigger=true;
}
} else if (ev.beta<-this.betaflat) {
// if beta is big its portrait
if (this.debug) console.log('beta portrait sec');
if (this.orientation!='portrait-secondary') {
this.orientation='portrait-secondary';
trigger=true;
}
} else if (ev.gamma>this.gammaflat) {
// else if gamma is big its landscape
if (this.debug) console.log('gamma landscape pri');
if (this.orientation!='landscape-primary') {
this.orientation='landscape-primary';
trigger=true;
}
} else if (ev.gamma<-this.gammaflat) {
// else if gamma is big its landscape
if (this.debug) console.log('gamma landscape sec');
if (this.orientation!='landscape-secondary') {
this.orientation='landscape-secondary';
trigger=true;
}
}
if (trigger) {
if (this.debug) console.log('trigger flip');
window.flip = this.angles[this.orientation];
$(window).trigger('flip',[window.flip,this.orientation]);
this.checked=true;
}
}
}
}
$(document).ready(function() {
setInterval(function() {jQuery.flip.checked=false},jQuery.flip.interval);
$(window).on('deviceorientation',function(ev) { jQuery.flip.check(ev.originalEvent) });
});
} else {
if (this.debug) console.log('DeviceOrientationEvent not supported');
}
jquery並不是真正需要的。 反正我有它需要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.