Stop image rotation after 360 degrees

I am trying to rotate the image of one round from its center point, but I cannot stop in the position of desire, because I can do the rotation, but I want to stop the rotation after 360'(1 round) .

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

Rotation method @

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

Your suggestions are noticeable.

+7
source share
2 answers

You need to keep the previous value of rot . And add the updateRotation check, if previousRot is to the left of 360 degrees, and rot is to the right of 360 degrees, then we did 1 round and you need to stop the rotation.

Sample code for a clockwise case

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

For the case counterclockwise this is almost the same, but the values ​​change

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

This code will stop rotation. From the beginning, previousRot should be 0 for the case clockwise and 359.99 for counterclockwise


Another approach is to add another variable to store the total angle traveled. From the beginning, traveledAngle should be 0. And if you rotate clockwise, you need to increase it by the difference between rot and previousRot . When rotating counterclockwise, decrease it by the same value.

 traveledAngle += rot - previousRot; 

When traveledAngle becomes more than 360 Β°, you need to stop the clockwise rotation, and when it becomes less than 0, you need to stop the counterclockwise rotation.

+3
source

I used your demo and added some logic, a newer demo looks like this:

 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); }*/ } } } 
+2
source

All Articles