2012-07-05 4 views
7

이미지를 중심점에서 회전 시키려고하지만 회전 할 수 있기 때문에 원하는 위치에서 멈출 수 없지만 회전을 멈추려는 경우는 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을해야합니다. 처음부터 traveledAngle은 0이어야합니다. 시계 방향으로 돌리면 rotpreviousRot의 차이만큼 키를 늘려야합니다. 시계 반대 방향으로 돌리면 같은 값만큼 시계 반대 방향으로 돌리십시오. traveledAngle보다 큰 360 '이되면

traveledAngle += rot - previousRot; 

당신은 시계 방향으로 회전 중지해야하고, 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, 내 데모를 시도하고 문제가 발생하면 여기에 의견을 보내 알려주십시오. –