Android中的方向 [英] Orientation in Android

查看:72
本文介绍了Android中的方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我需要您的大脑和思想d ^ _ ^ b.
这是问题:
我需要知道android设备如何管理其方向?我的意思是,我怎么知道它将得到风景"或肖像"或其他(倾斜程度如何).

我得到了这些代码:

Hi guys,
I need something from your brain and mind d^_^b.
Here''s the issue :
I need to know how do android device managing it''s orientation? I mean, how do I know it will get Landscape or Portrait or else (on what degree of obliquity).

I got these code :

  float angle = event.vector.roll;
if(angle >= 45 && angle < 135)
		newOrientationType  = OET_ASPECTLANDSCAPE_LEFT;
	else if(angle >= 135 && angle < 225)
		newOrientationType = OET_ASPECTPORTRAIT_REVERSED;
	else if(angle >= 225 && angle < 315)
		newOrientationType = OET_ASPECTLANDSCAPE_RIGHT;
	else
		newOrientationType = OET_ASPECTPORTRAIT;



这是上面的线索:
角度:确定已倾斜多少设备
newOrientationType:确定方向.

问题是:什么时候45,135,255,以及其他设备的学位?"

好的,可能不清楚.

所以看这个:

^ ^
_ _是正常位置(但度数是多少?)(OET_ASPECTLANDSCAPE_RIGHT)

| >
| > , 什么程度?? (OET_ASPECTLANDSCAPE_LEFT)

\
\ , 什么程度?? (OET_ASPECTLANDSCAPE_LEFT)

注意:"_ _":设备的位置

"^"和>" :设备的屏幕面向(对于(3),对角线,它应该面向45度(右上角).

感谢



Here''s the clue of above :
angle : determined how much device has been tilted
newOrientationType : determined the orientation.

The question is : "When''s 45,135,255,and else degree for the devices?"

Ok, that might be unclear.

So watch this :

^ ^
_ _ , is the normal position (but what degree??) (OET_ASPECTLANDSCAPE_RIGHT)

| >
| > , what degree?? (OET_ASPECTLANDSCAPE_LEFT)

\
\ , what degree?? (OET_ASPECTLANDSCAPE_LEFT)

Note : "_ _" : position of device

"^" and ">" : device''s screen facing to (as for the (3), diagonal, it''s should be facing 45 degree (top right).

Thanks

推荐答案

要启用角度数据,您必须编写特定的Eventlistener,并必须将其注册到SensorManager类中.


To enable the data for the angle, you have to write a certain Eventlistener and have to register it with the SensorManager class.


public class myAngleClass extends Activity
{   
    
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

        SensorManager Manager = (SensorManager)
this.getSystemService(SENSOR_SERVICE);        

        final float[] mValuesMagnet      = new float[3];
        final float[] mValuesAccel       = new float[3];
        final float[] mValuesOrientation = new float[3];
        final float[] mRotationMatrix    = new float[9];

       
        final SensorEventListener mEventListener = new SensorEventListener() {
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }

            public void onSensorChanged(SensorEvent event) {
               
                switch (event.sensor.getType()) {
                    case Sensor.TYPE_ACCELEROMETER:
                        System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
                        break;

                    case Sensor.TYPE_MAGNETIC_FIELD:
                        System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
                        break;
                }
            };
        };

// Register the event listener
    public void regListeners(SensorManager Manager , SensorEventListener list)
    {
       Manager 
             .registerListener(list, Manager  
             .getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
                SensorManager.SENSOR_DELAY_NORMAL);


        Manager
              .registerListener(list, 
               sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
                SensorManager.SENSOR_DELAY_NORMAL);

    }
}


这篇关于Android中的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆