2012-07-05 112 views
7

我想從它的中心點旋轉圖像單輪,但我不能停止在期望的位置,因爲我可以做旋轉,但我想在360'(1 round)後停止旋轉。360度後停止圖像旋轉

public class RotateRoundActivity extends Activity implements OnTouchListener 
{ 

    private ImageView dialer; 
    //private float y=0; 
    private float x=0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     dialer = (ImageView) findViewById(R.id.big_button); 
     dialer.setOnTouchListener(this); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
    // double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY()); 

     double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY()); 
     int rotation=(int)Math.toDegrees(r); 
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_MOVE: 
       x=event.getX(); 
       // y=event.getY(); 
       updateRotation(rotation); 
       break; 
      case MotionEvent.ACTION_UP: 
       break; 
     }//switch  

     return true; 
    } 

旋轉法@

private void updateRotation(double rot){ 
     float newRot=new Float(rot); 
     Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); 
     Matrix matrix=new Matrix(); 
     matrix.postRotate(newRot,bitmap.getWidth(),bitmap.getHeight()); 
     Log.i("demo===>", "matrix==>" + matrix); 
    // Log.i("demo===", "y===>" + y); 
     Log.i("demo===", "x===>" + x); 

     if(x>250){ 
      Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true); 
      dialer.setImageBitmap(reDrawnBitmap); 
     } 
     else{ 
      Bitmap reDrawnBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true); 
      dialer.setImageBitmap(reDrawnBitmap); 
     } 
    } 

} 

您的建議是明顯的。

+0

在哪個方向要旋轉?我的意思是順時針/逆時針? – 2012-07-05 09:35:44

+0

在方向時鐘和逆時針。 – Maulik 2012-07-05 09:39:59

回答

3

您必須保存以前的rot值。如果previousRot位於360度的左側,而rot位於360度的右側,則添加檢入updateRotation方法,然後我們進行1輪並停止旋轉。順時針情況下

if (previousRot >= 300 && previousRot <= 360 && rot >= 0 && rot <= 60) { 
    rot = 359.99; // or here can be 360' 
} 

對於逆時針情況

示例代碼幾乎是一樣的,但值交換

if (previousRot >= 0 && previousRot <= 60 && rot >= 300 && rot <= 360) { 
    rot = 0; 
} 

此代碼將停止轉動。從一開始就應該previousRot爲逆時針


另一種方法是添加一個變量來存儲總行進角爲0爲順時針的情況下和359.99。從開始traveledAngle必須等於0.如果您要順時針方向旋轉,則必須增加rotpreviousRot之間的差值。逆時針旋轉時減少相同的值。

traveledAngle += rot - previousRot; 

traveledAngle變得大於360' 你需要停止在順時針方向上旋轉,而當它變得小於0,則需要停止在逆時針方向方向上旋轉。

+0

我怎樣才能獲得超過360度的角度?它將從1'增加到360'。 360'後需要1度。 – Maulik 2012-07-05 09:36:07

+0

沒錯!對於順時針方向,'previousRot'應該是350-360','rot'應該是0-10'左右。但是這個數字只是舉例,它可能是300-360'和0-60'。主要思想是使用'previousRot'值 – vasart 2012-07-05 10:01:39

+0

好吧。但我怎樣才能停止使用previousRot旋轉?如果我在1轉後1',我的previousRot值是360'那麼?什麼應該是逆時針的邏輯? – Maulik 2012-07-05 10:39:12

2

我用您的演示,並增加了一些邏輯,更新的演示是如下:

public class RotateRoundActivity extends Activity implements OnTouchListener { 
    float rot1=0.0F, rot2=0.0F; 
    boolean clockwise, rotationDone = false, halfrotated = false; 
    int rotcall=0; 

    private ImageView dialer; 
    //private float y=0; 
    private int x=0; 
    //private int y=0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     dialer = (ImageView) findViewById(R.id.big_button); 
     dialer.setOnTouchListener(this); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
    // double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY()); 
     double r=Math.atan2(event.getX()-dialer.getWidth()/2, dialer.getHeight()/2-event.getY()); 
     int rotation=(int)Math.toDegrees(r); 
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_MOVE: 
       x=(int) event.getX(); 
       //y=(int) event.getY(); 
       updateRotation(rotation); 
       break; 
      case MotionEvent.ACTION_UP: 
       break; 
     }//switch  

     return true; 
    } 

    private void updateRotation(double rot){ 
     float newRot = new Float(rot); 

     rotcall++; 
     if(rotcall == 1) 
      rot1 = new Float(rot); 
     if(rotcall == 2) 
      rot2 = new Float(rot); 
     if(rot1 != 0.0F && rot2 != 0.0F) 
      if(rot1 < rot2) 
       clockwise = true; 
      else 
       clockwise = false; 
     System.out.println("Rotate :: "+newRot); 

     if(clockwise && rot1>=0) { 
      if(newRot < 0) 
       halfrotated = true; 
      if(halfrotated && newRot > 0) 
       rotationDone = true; 
      if(rotationDone) 
       newRot = 0; 
     } 
     if(clockwise && rot1<0) { 
      if(newRot > 0) 
       halfrotated = true; 
      if(halfrotated && newRot < 0) 
       rotationDone = true; 
      if(rotationDone) 
       newRot = 0; 
     } 
     if(!clockwise && rot1<0) { 
      if(newRot > 0) 
       halfrotated = true; 
      if(halfrotated && newRot < 0) 
       rotationDone = true; 
      if(rotationDone) 
       newRot = 0; 
     } 
     if(!clockwise && rot1>=0) { 
      if(newRot < 0) 
       halfrotated = true; 
      if(halfrotated && newRot > 0) 
       rotationDone = true; 
      if(rotationDone) 
       newRot = 0; 
     } 

     System.out.println("Rotation Done :: "+rotationDone); 

     if(!rotationDone) { 
      //BitmapDrawable bitmapDrawable = (BitmapDrawable) dialer.getDrawable(); 
      //Bitmap bitmap = bitmapDrawable.getBitmap(); 
      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable. YOUR_DRBL ); 
      int width = bitmap.getWidth(); 
      int height = bitmap.getHeight(); 
      Matrix matrix = new Matrix(); 
      matrix.postRotate(newRot, width, height); 
      System.out.println("x===>" + x); 
      //System.out.println("y===>" + y); 

      //if (x > 250) { 
       Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); 
       dialer.setImageBitmap(reDrawnBitmap); 
      /*} else { 
       Bitmap reDrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
         width, height, matrix, true); 
       dialer.setImageBitmap(reDrawnBitmap); 
      }*/ 
     } 
    } 

} 
+0

有2個副作用。 1.]從左邊的硬件部分或2開始順時針旋轉。]從右半部分逆時針旋轉開始旋轉。在這兩種情況下,它將圖像旋轉1.5圈。這是我的邏輯限制,直到現在。如果我會更新它,我會通知你。 – 2012-07-05 14:18:43

+0

@Maulik,如果你嘗試我的演示,並得到任何問題,然後在這裏通過評論通知我。 – 2012-07-05 14:20:38