【问题标题】:How to stop animation in Android如何在Android中停止动画
【发布时间】:2012-03-31 04:07:15
【问题描述】:

要求:

我需要一个一个地运行多个动画。我正在使用 onAnimationEnd() 这样做。

在制作动画时,如果发生触摸事件,我需要停止动画并在该位置显示新图像。

问题:

  1. 我在 onTouch 的情况下使用 clearAnimation()。因此,完整的动画被删除了。我的意图是停止动画并在触摸部分显示新图像。我怎样才能做到这一点?

  2. 由于 clearAnimation(),onAnimationEnd() 被多次调用,我在一个接一个地运行动画时遇到问题。

  3. 是否有任何功能只是停止动画而不是完全清除它?我使用的是 Android 2.1。

@Override
public boolean onTouch(View v, MotionEvent event) {      
    switch(event.getAction()) {
        case MotionEvent.ACTION_UP:
            imageArray[g_animCount - 1].clearAnimation();          
            break;

        default:
            break;
    }

    return true; // indicate event was handled
}

@Override
public void onAnimationEnd(Animation animation) {
    layout.removeView(imageArray[g_animCount - 1]);
    if ( (g_animCount < count))
    {               
        startNextAnimation();
    }
    else
    {
        g_animCount = 0;
        isBegin = false;
    }
}

public void startNextAnimation()
{
     int j = random.nextInt(200);
     layoutParams.setMargins(j, -20, 30, 0);
     layout.addView(imageArray[g_animCount], layoutParams); 
     imageArray[g_animCount].startAnimation(movArray[g_animCount]);
     g_animCount++;
}

【问题讨论】:

    标签: android animation imageview


    【解决方案1】:

    动画只移动屏幕上的像素,而不是对象的位置。要设置你在结束时停留在哪里,设置你的

    animation.setFillAfter(true);
    

    要实际移动对象的位置,请使用以下代码 sn-p 的修改版本。

    MarginLayoutParams marginParams = new MarginLayoutParams(object.getLayoutParams());
    marginParams.setMargins(left, (top+hol2), left, 0);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
    object.setLayoutParams(layoutParams);
    

    关于 onAnimationEnd 被多次调用,我需要看一些代码。

    我知道手动停止动画的唯一两种方法是

    animation.cancel(); (may not work for 2.1, can't remember)
    

    object.clearAnimation();
    

    下面的示例代码:

        upmotionleft = new TranslateAnimation(0, 0, 0, 600);
        upmotionleft.setDuration(2000);
        upmotionleft.setFillEnabled(true);
        upmotionleft.setAnimationListener(new Animation.AnimationListener() 
        {
            @Override
            public void onAnimationStart(Animation animation) 
            {}
            @Override
            public void onAnimationEnd(Animation animation) 
            {
                //sets to wherever I want the final object to be
                MarginLayoutParams marginParams = new MarginLayoutParams(object.getLayoutParams());
                marginParams.setMargins(left, top-hol2, left, 0);
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
                object.setLayoutParams(layoutParams);
    
                //starts next animation
                object.startAnimation(nextAnimation);
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) 
            {}
        });
    
    object.startAnimation(upmotionleft);
    

    此代码是从我的一个项目中复制和粘贴的,但已有所更改,应该仍然可以运行。

    【讨论】:

    • @testingtester clearAnimation 有必要调用 onAnimationEnd() 吗?
    猜你喜欢
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    相关资源
    最近更新 更多