2012-07-05 8 views
7

画像を1回転させようとしていますが、私は回転をすることができますが、希望位置で止めることはできませんが、360'(1 round)の後に回転を止めたいと思います。あなたの提案はかなりある360度後の画像の回転を停止する

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

} 

@

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

回転法。

+0

どの方向に回転させたいですか?私は時計回り/反時計回りを意味しますか? –

+0

時計方向および反時計回りの両方向。 – Maulik

答えて

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


なければならないもう一つのアプローチは、総走行角を格納するために1つの以上の変数を追加することです。最初からtraveledAngleは0に等しくなければなりません。そして、時計回りに回転させる場合は、rotpreviousRotの差だけそれを増やす必要があります。反時計回りに回すと、同じ値だけ減少します。

traveledAngle += rot - previousRot; 

traveledAngleが360' より大きくなるとあなたが時計方向に回転を停止する必要があり、それが0未満になると、あなたは反時計方向に回転を停止する必要があります。

+0

どのように角度を360度以上にすることができますか? 1 'から360'に増加します。 360度後には1度になります。 – Maulik

+0

そうですよ! 'previousRot'は時計回りの場合は350-360 '、' rot'は約0-10'でなければなりません。しかし、この数字は例えば300-360 'と0-60'の数字だけです。主な考え方は 'previousRot'の値を使用しています – vasart

+0

大丈夫です。 previousRotを使用して回転を停止するにはどうすればよいですか?私が1回転後に1 'にあり、私のpreviousRot値が360'ならば?反時計回りの論理はどうでしょうか? – Maulik

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

r 2の副作用があります。 1.]左旋回部から時計方向に回転開始または2.]右半部から反時計回りに回転を開始する。これらの両方の場合、1.5回転の画像を回転させます。それは今までの私の論理の限界です。もし私がそれを更新するなら、私はあなたに知らせるでしょう。 –

+0

@Maulik、私のデモを試して問題がある場合は、ここでコメントしてお知らせください。 –

関連する問題