在Android中停止ObjectAnimators的AnimatorSet [英] Stop AnimatorSet of ObjectAnimators in Android

查看:1615
本文介绍了在Android中停止ObjectAnimators的AnimatorSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮时停止 ImageView 的动画。我正在使用的动画是一个 AnimatorSet ,它由5个 ObjectAnimators 组成。问题是我无法弄清楚当单击按钮 btn.clearAnimation()时,如何停止并从 ImageView 清除此动画?

Im trying to stop the animation of an ImageView when a button is clicked. The animation I am using is an AnimatorSet consisting of 5 ObjectAnimators... The problem is that I can't figure how to stop and clear this animation from the ImageView when the button is clicked as btn.clearAnimation() obviously doesn't work.

谢谢您的帮助。

推荐答案

您应该可以调用 animatorSet.cancel()来取消动画。下面是一个在动画播放5秒后取消动画的示例:

You should be able to call animatorSet.cancel() to cancel the animation. Here's an example that cancels the animation 5 seconds after it starts:

package com.example.myapp2;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = (TextView) findViewById(R.id.hello_world);

        List<Animator> animations = new ArrayList<Animator>();

        animations.add(ObjectAnimator.ofInt(tv, "left", 100, 1000).setDuration(10000));
        animations.add(ObjectAnimator.ofFloat(tv, "textSize", 10, 50).setDuration(10000));

        final AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animations);
        animatorSet.start();

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                animatorSet.cancel();
            }
        }, 5000);
    }
}

这篇关于在Android中停止ObjectAnimators的AnimatorSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆