连续旋转图像OnTouchEvent在Android中

问题描述:

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

从我个人理解,就是你要当用户将他/她的手指在屏幕上,开始旋转图像,并停止转动,当用户取下从屏幕上的手指。

如果这是正确的,你需要在后台启动一个线程或处理程序。

可能是这样的:

// 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布尔值应该没问题。但这一切都取决于你的要求和逻辑:) –

用途:

degrees = (degrees + 10) % 360; 

// ... 

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

使用下面的代码

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

这是我的代码自己周围旋转图像,慢慢降低速度