我有一个显示相机预览的活动,因此需要将其设置为仅景观.在底部(无论设备旋转)我想显示文本视图.我正在使用OrientationEventListener,它从自然位置给出设备的方向.我可以实现一个在纵向默认设备上运行良好的解决方案,但是为了使它在横向默认设备上工作,我需要知道在这样的设备上运行.那么问题是如何检查呢?
use*_*292 75
这种方法可以帮助: -
public int getDeviceDefaultOrientation() {
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Configuration config = getResources().getConfiguration();
int rotation = windowManager.getDefaultDisplay().getRotation();
if ( ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) &&
config.orientation == Configuration.ORIENTATION_LANDSCAPE)
|| ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) &&
config.orientation == Configuration.ORIENTATION_PORTRAIT)) {
return Configuration.ORIENTATION_LANDSCAPE;
} else {
return Configuration.ORIENTATION_PORTRAIT;
}
}
Run Code Online (Sandbox Code Playgroud)
Nan*_*nne 12
那么,你可以找出@Urboss所说的当前方向.你不能得到那种方式的布局(风景/肖像),所以你必须得到屏幕宽度/高度来检查当前是否(无论是否改变,但你已经检查过了;))位置是风景或者肖像:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
wPix = dm.widthPixels;
hPix = dm.heightPixels;
Run Code Online (Sandbox Code Playgroud)
(因此,如果旋转为0并且您获得横向上方的横向,则您的设备是默认横向.如果旋转是90度并且您是纵向,则默认也是横向,依此类推)
经过几个小时的努力来解决这个问题.这是不可能的.但是,您最接近的方法是将方向设置为"NOSENSOR"
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Run Code Online (Sandbox Code Playgroud)
这样做是将您的应用程序设置为设备的自然方向.此时,您可以使用DisplayMetrics类获取显示的高度和宽度,并计算您是横向还是纵向.然后,当你弄清楚它是风景还是肖像之后,你就可以做到这一点
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_XXXXX);
其中XXXX是LANDSCAPE或PORTRAIT.
如果你有一个滑出键盘,这样做可能不起作用的情况.
感谢diyism上面的优秀答案,我还添加了检查实际方向位置的逻辑(横向右侧,纵向,横向左侧,纵向翻转):
这是代码:
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
//The coordinate-system is defined relative to the screen of the phone in its default orientation
int orientation = 0;
float roll=0;
float pitch=0;
switch (getWindowManager().getDefaultDisplay().getRotation()) {
case Surface.ROTATION_0:
roll=event.values[2];
pitch=event.values[1];
break;
case Surface.ROTATION_90:
roll=event.values[1];
pitch=-event.values[2];
break;
case Surface.ROTATION_180:
roll=-event.values[2];
pitch=-event.values[1];
break;
case Surface.ROTATION_270:
roll=-event.values[1];
pitch=event.values[2];
break;
}
if (pitch >= -45 && pitch < 45 && roll >= 45)
orientation = 0;
else if (pitch < -45 && roll >= -45 && roll < 45)
orientation = 1;
else if (pitch >= -45 && pitch < 45 && roll < -45)
orientation = 2;
else if (pitch >= 45 && roll >= -45 && roll < 45 )
orientation = 3;
if (m_nOrientation != orientation) { //orientation changed event
m_Inst.Debug(LOG_TAG,"onSensorChanged: orientation:" + orientation);
m_nOrientation = orientation;
// fire event for new notification, or update your interface here
}
}
Run Code Online (Sandbox Code Playgroud)
此代码也发布在这里:http://www.pocketmagic.net/? p = 2847
小智 6
这是我的解决方案:
public class ActivityOrientationTest extends Activity {
private static final String TAG = "ActivityOrientationTest";
private int mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
private TextView mTextView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView)findViewById(R.id.text);
setDefaultOrientation();
}
private void setDefaultOrientation(){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
Display display;
display = getWindow().getWindowManager().getDefaultDisplay();
int rotation = display.getRotation();
int width = 0;
int height = 0;
switch (rotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_180:
Log.i(TAG, "Rotation is: 0 or 180");
width = display.getWidth();
height = display.getHeight();
break;
case Surface.ROTATION_90:
case Surface.ROTATION_270:
Log.i(TAG, "Rotation is: 90 or 270");
width = display.getHeight();
height = display.getWidth();
break;
default:
break;
}
if(width > height){
Log.i(TAG, "Natural Orientation is landscape");
mTextView.setText("Natural Orientation is landscape");
mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
Log.i(TAG, "Natural Orientation is portrait");
mNaturalOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
mTextView.setText("Natural Orientation is portrait");
}
setRequestedOrientation(mNaturalOrientation);
}
Run Code Online (Sandbox Code Playgroud)
Display.getRotation()仅在API级别8或更高级别受支持,因此如果您需要支持旧设备,则需要调用Display.getOrientation().
Urb*_*oss -4
可以使用Display.getOrientation()或Display.getRotation()来完成(对于 API 级别 >= 8)。
| 归档时间: |
|
| 查看次数: |
31855 次 |
| 最近记录: |