在Android中连续旋转图像OnTouchEvent

时间:2011-11-02 15:54:39

标签: java android android-layout

我是Android开发的新手,我正在尝试弄清楚如何使用onTouchEvent连续旋转图像。当onTouchEvent检测到屏幕触摸时,我使用以下代码将图像旋转10度,反之亦然,但我希望只要onTouchEvent发生,图像就会以10度的间隔保持旋转。谢谢!

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

3 个答案:

答案 0 :(得分:0)

使用:

degrees = (degrees + 10) % 360;

// ...

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

答案 1 :(得分: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
    // ...
}

答案 2 :(得分: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();
    }
}

这是我的代码,用于围绕自身旋转图像并慢慢降低速度

相关问题