2012-07-05 13 views
7

Estoy tratando de rotar la imagen desde su punto central, pero no puedo detenerme en la posición de deseo ya que puedo hacer la rotación pero quiero detener la rotación después de 360'(1 round).Detener la rotación de la imagen después de 360 ​​grados

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

método de rotación @

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

} 

Sus sugerencias son apreciables.

+0

¿En qué dirección desea girar? Me refiero a las agujas del reloj/antihorario? –

+0

Tanto en el sentido de las agujas del reloj como en el sentido contrario a las agujas del reloj. – Maulik

Respuesta

3

Debes guardar el valor anterior de rot. Y agregue cheque en el método updateRotation si previousRot está a la izquierda de 360 ​​'grados y rot está a la derecha de 360' grados luego hacemos 1 vuelta y necesitamos dejar de girar.

Código de ejemplo para el caso de las agujas del reloj

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

Para el caso de las agujas del reloj es casi la misma, pero los valores intercambió

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

Este código se detendrá la rotación. Desde el principio previousRot debe ser 0 para el caso de las agujas del reloj y en sentido antihorario para 359.99


Otro enfoque consiste en añadir una variable más para almacenar ángulo de recorrido total. Desde el principio traveledAngle tiene que ser igual a 0. Y si está girando en el sentido de las agujas del reloj, debe aumentarla por la diferencia entre rot y previousRot. Al girar en sentido contrario a las agujas del reloj, disminuya en el mismo valor.

traveledAngle += rot - previousRot; 

Cuando traveledAngle se hace superior a 360' que necesita para dejar de girar en sentido horario, y cuando llega a ser inferior a 0, tiene que dejar de girar en sentido contrario a las agujas del reloj.

+0

¿Cómo puedo obtener el ángulo más de 360 ​​'grados? Se incrementará de 1 'a 360'. después de 360 ​​'toma 1' grado. – Maulik

+0

¡Eso es! Para la dirección en el sentido de las agujas del reloj, 'previousRot' debe ser 350-360 ', y' rot' debe ser de aproximadamente 0-10 '. Pero estos números se dan solo por ejemplo, puede ser 300-360 'y 0-60'. La idea principal es usar el valor 'previousRot' – vasart

+0

Okay. pero ¿cómo puedo detener la rotación usando previousRot? si estoy a 1 'después de una rotación y mi valor anterior deRot es 360', entonces? ¿Y cuál debería ser la lógica para el sentido contrario a las agujas del reloj? – Maulik

2

he utilizado su demo y añade un poco de lógica, la demostración más reciente es la siguiente:

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

Hay r 2 efectos secundarios. 1.] Comenzando la rotación en sentido horario desde la parte de la parte izquierda O 2.] Comenzando la rotación en sentido antihorario desde la mitad derecha. En estos dos casos, Gira la imagen para 1.5 rotaciones.Esa es la limitación de mi lógica hasta este momento. Si voy a actualizarlo, te informaré. –

+0

@Maulik, si prueba mi demo y obtiene algún problema, entonces infórmeme comentando aquí. –

Cuestiones relacionadas