2012-09-28 65 views
6

在我的android應用程序中,我有全景圖像,並根據手機運動使用TYPE_ORIENTATION傳感器旋轉此圖像,它對景觀和potrait都正常工作。這是旋轉邏輯的代碼。如何在android中檢測顛倒的方向?

@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; 
} 
} 

所以我的問題是,如果設備的方向改變倒掛圖像將旋轉到錯誤的方向(方向剛好相反)。我如何解決這個問題,以及當設備方向顛倒時實際發生了什麼?
謝謝。

回答