3
我試圖從自定義相機活動拍攝照片時獲取設備方向。 以前,我在任何地方檢查瞭如何實現它,並沒有得到任何解決方案的結果。getDefaultDisplay()。getRotation();返回始終相同的值
我正在使用這一行代碼檢索沒有結果的方向。它總是返回1.如果有幫助,我將使用Samsung Galaxy S III進行開發。
int rotation = getWindowManager().getDefaultDisplay().getRotation();
Log.d(TAG, "Taken rotation " + rotation);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int orientation;
// if the device's natural orientation is portrait:
if ((rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90
|| rotation == Surface.ROTATION_270) && width > height) {
switch(rotation) {
case Surface.ROTATION_0:
orientation = 0;
break;
case Surface.ROTATION_90:
orientation = 90;
break;
case Surface.ROTATION_180:
orientation =180;
break;
case Surface.ROTATION_270:
orientation = 270;
break;
default:
Log.e(TAG, "Unknown screen orientation. Defaulting to " +
"portrait.");
orientation = 0;
break;
}
也許您將活動限制爲橫向方向?也許你將手機設置爲鎖定屏幕方向? –
感謝您的回答。我事先檢查了它,因爲我將它設置爲相機佈局的橫向。我退出了它,而不是讓我回來1一直是0。這就是我爲什麼要把它扔到這裏的原因 – datienza
也許我不正確地理解你。當您將活動鎖定到橫向時,您將只能從WindowManager接收橫向旋轉。如果鎖定到人像,您只會收到人像旋轉。即使方向鎖定,您也可以檢查有關歐拉天使的加速度計。 –