简体   繁体   English

在Android中连续旋转图片OnTouchEvent

[英]Continuously Rotate Image OnTouchEvent In Android

I'm new to Android development and am trying to figure out how to continuously rotate an image using the onTouchEvent. 我是Android开发的新手,正在尝试弄清楚如何使用onTouchEvent连续旋转图像。 I am using the following code to rotate the image 10 degrees when the onTouchEvent detects a screen touch and vice versa, but I want the image to keep rotating in 10 degree intervals as long as the onTouchEvent occurs. 当onTouchEvent检测到屏幕触摸时,我正在使用以下代码将图像旋转10度,反之亦然,但是我希望只要onTouchEvent发生,图像就以10度间隔旋转。 Thanks! 谢谢!

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

Use the below code 使用下面的代码

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

This is my code to rotate a image around itself and slowly decrease the speed 这是我的代码,用于围绕自身旋转图像并缓慢降低速度

Use: 采用:

degrees = (degrees + 10) % 360;

// ...

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

From what I understand, is you want to start rotating an image when the user puts his / her finger on the screen, and stop rotating when the user takes off the finger from the screen. 据我了解,您是想在用户将手指放在屏幕上时开始旋转图像,还是在用户将手指从屏幕上移开时停止旋转图像。

If that's correct, you'd need to start a thread or a handler on the background that would keep. 如果是正确的话,则需要在后台保留一个线程或一个处理程序。

May be something like: 可能是这样的:

// 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
    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM