2011-11-02 108 views
0

我是Android開發新手,想弄清楚如何使用onTouchEvent連續旋轉圖像。當onTouchEvent檢測到屏幕觸摸時,我使用以下代碼將圖像旋轉10度,反之亦然,但我希望圖像在發生onTouchEvent時保持10度旋轉。謝謝!連續旋轉圖像OnTouchEvent在Android中

@Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if(event.getAction()==MotionEvent.ACTION_DOWN) { 
      degrees=degrees+10; 
      make(degrees); 
      } 
     else if(event.getAction()==MotionEvent.ACTION_UP) { 
      degrees=degrees-10; 
      make(degrees); 
      } 
     return super.onTouchEvent(event); 
     } 
} 

回答

0

從我個人理解,就是你要當用戶將他/她的手指在屏幕上,開始旋轉圖像,並停止轉動,當用戶取下從屏幕上的手指。

如果這是正確的,你需要在後臺啓動一個線程或處理程序。

可能是這樣的:

// flag to tell if the rotation should continue 
private boolean keepRotating = false; 
// instance variable to keep the current rotation degrees 
private int degrees = 0; 
// rotation interval in milliseconds 
private static final int INTERVAL = 100; 

@Override 
public boolean onTouchEvent(MotionEvent event) 
{ 
    switch (event.getAction()) 
    { 
     case MotionEvent.ACTION_DOWN: 
      startRotating(); 
      break; 
     case MotionEvent.ACTION_UP: 
      stopRotating(); 
      break; 
    } 

    return super.onTouchEvent(event); 
} 

public void startRotating() 
{ 
    if (!keepRotating) 
    { 
     keepRotating = true; 

     final Handler handler = new Handler(); 

     handler.postDelayed(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       if (keepRotating) 
       { 
        degrees = (degrees + 10) % 360; 
        make(degrees); 

        handler.postDelayed(this, INTERVAL); 
       } 
      } 
     }, INTERVAL); 
    } 
} 

public void stopRotating() 
{ 
    keepRotating = false; 
} 

public void make(int degrees) 
{ 
    Log.i(this.getClass().toString(), "Rotation : " + degrees); 

    // Your rotation logic here based on the degrees parameter 
    // ... 
} 
+0

這是正確的,我當觸摸屏幕時圖像不斷旋轉,並在觸摸事件結束時停止旋轉。 – n00bdev

+0

我不確定這是否是正確的方式來反轉旋轉,但我向startRotating()和StopRotating()部分添加了一個'returnRotating'變量,以使針返回到原始位置,如下所示: public void stopRotating() { \t keepRotating = false; if(!returnRotating) // ... – n00bdev

+0

嗯,你的意思是你想讓針在開始下一圈之前回到原來的位置?就像我開始,然後停止針。然後再次啓動,它應該從0開始,而不是從停止的地方繼續。正確?對於這個使用returnRotating布爾值應該沒問題。但這一切都取決於你的要求和邏輯:) –

0

用途:

degrees = (degrees + 10) % 360; 

// ... 

if(degrees < 10) degrees += 360; 
degrees = (degrees - 10) % 360; 
0

使用下面的代碼

public void startMoving() { 
    rotateAnimation1 = null; 
    try { 
     if (duration < 1500) { 
      rotateAnimation1 = new RotateAnimation(0, 360, 
        Animation.RELATIVE_TO_SELF, 0.5f, 
        Animation.RELATIVE_TO_SELF, 0.5f); 
      rotateAnimation1.setInterpolator(new LinearInterpolator()); 
      rotateAnimation1.setDuration(duration); 
      rotateAnimation1.setRepeatCount(0); 
      imgBottle.startAnimation(rotateAnimation1); 

      flagSpinAvailable = false; 

      rotateAnimation1.setAnimationListener(new AnimationListener() { 
       public void onAnimationStart(Animation anim) { 
       }; 

       public void onAnimationRepeat(Animation anim) { 
       }; 

       public void onAnimationEnd(Animation anim) { 
        duration = duration + 70; 
        startMoving(); 
       }; 
      }); 

     } else { 
      duration = duration + 100; 
      final float degree = (float) (Math.random() * 360); 
      degreeBack = 360 - degree; 
      rotateAnimation2 = new RotateAnimation(0, degree, 
        Animation.RELATIVE_TO_SELF, 0.5f, 
        Animation.RELATIVE_TO_SELF, 0.5f); 
      rotateAnimation2.setInterpolator(new LinearInterpolator()); 
      rotateAnimation2.setDuration(duration); 
      rotateAnimation2.setRepeatCount(0); 
      rotateAnimation2.setFillAfter(true); 
      imgBottle.startAnimation(rotateAnimation2); 

      rotateAnimation2.setAnimationListener(new AnimationListener() { 
       public void onAnimationStart(Animation anim) { 
       }; 

       public void onAnimationRepeat(Animation anim) { 
       }; 

       public void onAnimationEnd(Animation anim) { 
        afterSpinEnd(); 
       }; 
      }); 

     } 
    } catch (Exception e) { 
     flagSpinAvailable = true; 
     e.printStackTrace(); 
    } 
} 

這是我的代碼自己周圍旋轉圖像,慢慢降低速度