當你點擊全屏圖標在YouTube的應用程序,它會改變爲風景和全屏,發現仍然是自動旋轉模式,如何做到這一點?如何改變方向但保持自動旋轉屏幕
如果設置lanscape模式:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
(設備總是景觀)
- 點擊全屏
- lanscape
- 肖像
我解決了問題使用傳感器來檢測橫向或縱向
@Override
public void onPause() {
super.onPause();
videoview.pause();
mSensorManager.unregisterListener(this);
}
@Override
public void onResume() {
super.onResume();
mSensorManager = (SensorManager) getActivity().getSystemService(Activity.SENSOR_SERVICE);
mRotationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
mSensorManager.registerListener(this, mRotationSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor == mRotationSensor) {
if (event.values.length > 4) {
float[] truncatedRotationVector = new float[4];
System.arraycopy(event.values, 0, truncatedRotationVector, 0, 4);
update(truncatedRotationVector);
} else {
update(event.values);
}
}
}
private void update(float[] vectors) {
float[] rotationMatrix = new float[9];
SensorManager.getRotationMatrixFromVector(rotationMatrix, vectors);
int worldAxisX = SensorManager.AXIS_X;
int worldAxisZ = SensorManager.AXIS_Z;
float[] adjustedRotationMatrix = new float[9];
SensorManager.remapCoordinateSystem(rotationMatrix, worldAxisX, worldAxisZ, adjustedRotationMatrix);
float[] orientation = new float[3];
SensorManager.getOrientation(adjustedRotationMatrix, orientation);
// float pitch = (float) Math.toDegrees(orientation[1]);
float roll = (float) Math.toDegrees(orientation[2]);
boolean curentOrient = isPortrait;
if(roll >= -75 && roll <= 75){
isPortrait = true;
System.out.println("Portrait");
}else{
isPortrait = false;
System.out.println("Landscape");
}
if(curentOrient != isPortrait){
videoview.toggleFullScreen(isPortrait); // this is my videoview.
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
個
,讓工具SensorEventListener
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE_SENSOR,我試過了,但它只允許橫向,而不是縱向。謝謝你,先生 – vuhung3990 2014-12-04 03:48:43
所以,你只是想全屏,但傳感器方向? ActivityInfo.SCREEN_ORIENTATION_SENSOR是你想要的。 – 2014-12-04 03:50:41