首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓系统中的连续旋转图片OnTouchEvent

安卓系统中的连续旋转图片OnTouchEvent
EN

Stack Overflow用户
提问于 2011-11-02 23:54:39
回答 3查看 2K关注 0票数 0

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

代码语言:javascript
复制
    @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);
        }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-11-03 00:26:55

据我所知,当用户将手指放在屏幕上时,您想要开始旋转图像,当用户将手指从屏幕上移开时,停止旋转。

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

可能是这样的:

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2011-11-02 23:59:22

使用:

代码语言:javascript
复制
degrees = (degrees + 10) % 360;

// ...

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

Stack Overflow用户

发布于 2012-12-31 21:03:36

使用以下代码

代码语言:javascript
复制
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();
    }
}

这是我的代码,用来旋转图像本身并慢慢降低速度

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7983347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档