如何检测倒挂取向的Andr​​oid?取向、Andr、oid

2023-09-07 23:55:39 作者:爷,独身万岁,失恋无罪

在我的Andr​​oid应用我有全景图像和我使用TYPE_ORIENTATION传感器,它正在罚款风景和potrait根据手机旋转运动这一形象。这里是code旋转逻辑。

In my android app i have panorama image and i am rotating this image according to phones motion using TYPE_ORIENTATION sensor it is working fine for both landscape and potrait. Here is the code for rotation logic.

@Override
public void onSensorChanged(SensorEvent event) {
  switch (event.sensor.getType()) {
  case Sensor.TYPE_ORIENTATION:

    float[] insideval = getValPoints();
    event.values[1]=(float) (event.values[1]*2.8);
    event.values[2]=(float) (event.values[2]*2.8);
    setValPoints(event.values.clone());
        if (insideval != null) {
            float check= insideval[0] - event.values[0];            
            if (-1<=check && check <=1) {
                //Animation stoped      
                this.stopAnimation();
            }else{
                if (getdefaltDeviceOrientation() == ORENTATION_LANDSCAPE) {
                     // Natural Orientation is landscape
                      if (getcurrentDeviceOrientation() == 1) {
                        //ORIENTATION_PORTRAIT
                        startPoint = CGPoint.CGPointMake(insideval[2], insideval[1]);
                                if (insideval[0] < event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[2]+ event.values[2], insideval[1]);
                                } else if (insideval[0] > event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[2]- event.values[2], insideval[1]);
                                }
                            } else if (getcurrentDeviceOrientation() == 2) {
                                //Log.i(TAG, "ORIENTATION_LANDSCAPE");
                                startPoint = CGPoint.CGPointMake(insideval[1], insideval[2]);
                                if (insideval[0] < event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[1]- event.values[1], insideval[2]);
                                } else if (insideval[0] > event.values[0]) {
                                    endPoint = CGPoint.CGPointMake( insideval[1]+event.values[1] ,insideval[2]);                    
                                }
                            }
                        } else if (getdefaltDeviceOrientation() == ORENTATION_PROTRAIT) {
                            // Natural Orientation is portrait

                            if (getcurrentDeviceOrientation() == 1) {
                                //Log.i(TAG, "ORIENTATION_PORTRAIT");
                                startPoint = CGPoint.CGPointMake(insideval[1], insideval[2]);
                                if (insideval[0] < event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[1]+ event.values[1], insideval[2]);
                                } else if (insideval[0] > event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[1]- event.values[1], insideval[2]);
                                }
                            } else if (getcurrentDeviceOrientation() == 2) {
                                //Log.i(TAG, "ORIENTATION_LANDSCAPE");
                                startPoint = CGPoint.CGPointMake(insideval[2], insideval[1]);
                                if (insideval[0] < event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[2]- event.values[2], insideval[1]);
                                } else if (insideval[0] > event.values[0]) {
                                    endPoint = CGPoint.CGPointMake(insideval[2]+event.values[2],insideval[1]);
                                }
                            }
                        }
                        this.drawView();
                    }
                }

                break;
}
}

所以我的问题是如果设备的方向改变倒挂图像旋转到错误的方向(方向刚好相反)。我怎样才能解决这个问题,到底发生了什么,当设备方向更改倒挂?结果谢谢你。

So my problem is if device's orientation changes upside down image will rotate to wrong direction(exactly opposite direction). How can i fix this issue and what actually happens when devices orientation change upside down ? Thanks.

推荐答案

使用该设备当前旋转的http://developer.android.com/reference/android/view/Display.html#getRotation()

它返回的4个选项ROTATION_0,ROTATION_90,ROTATION_180,ROTATION_270有一个

It returns one of 4 options ROTATION_0, ROTATION_90, ROTATION_180, ROTATION_270