android - 开始后无法在android中停止Animator

标签 android android-3.0-honeycomb android-animation viewanimator

我正在编写一个应用程序,其中我在 RelativeLayout 中有一些 View (在不同的位置,使用边距),我正在使用 Honeycomb 中的新动画 API 为它们制作动画。 动画会自行重复,但它们需要在每次重复之间等待一段时间,所以我无法使用重复模式。

一切顺利,但有一部分我想将它们移动到另一个地方并停止动画,但它拒绝停止。我正在移动它们,但它们没有出现,突然间我看到它们经过,因为它们仍在动画中。 我尝试了所有我能想到的方法,请帮助我。

代码:

if(!mMoving){
            mMoving = true;
            for(int i = 0; i < mImagesList.size(); i++){

                final LinearLayout f = mImagesList.get(i);
                if(mMoving){
                    ObjectAnimator anim = ObjectAnimator.ofFloat(f, "x", Math.round(mScreenWidth * 1.4));
                    mAnimators.add(anim);
                    anim.setDuration(mRandom.nextInt(10000) + 8000);
                    anim.setStartDelay((mRandom.nextInt(4000) + 3000) * (i / ITEMS_PER_SCREEN));
                    anim.addListener(new AnimatorListener() {


                        @Override
                        public void onAnimationEnd(Animator animation) {
                            if(mMoving){
                            mAnimators.remove(animation);

                                ImageView img = (ImageView)f.findViewById(R.id.stream_feed_item_pic);
                                int picWidth = img.getDrawable().getIntrinsicWidth();

                                Animator anim = ObjectAnimator.ofFloat(f, "x", -Math.round(picWidth * 1.4), Math.round(mScreenWidth * 1.2));

                                mAnimators.set(mAnimators.indexOf(animation), anim);

                                anim.setDuration(mRandom.nextInt(14000) + 8000);
                                anim.setStartDelay((mRandom.nextInt(6000) + 3000) * (mImagesList.size() / ITEMS_PER_SCREEN));
                                anim.addListener(this);
                                anim.start();
                            }
                        }
                    });
                    anim.start();
                }
            }
            mMoving = true;
            return true;
        }

正如您所看到的,我正在为每个图像创建一个具有监听器的 Animator,并且在每个动画结束时,都会调用监听器并创建一个新动画并开始延迟。我将所有动画存储在一个列表中。

这是我(绝望的)阻止他们的尝试:

if(mMoving){
                    mMoving = false;
                    for(Animator anim : mAnimators){
                        anim.setStartDelay(0);
                        anim.setDuration(0);
                        anim.start();
                        anim.cancel();
                        anim.removeAllListeners();
                        anim.setTarget(null);
                    }
                    mAnimators.clear();
                }

这就是我将它们移动到另一个布局的方式:

mContainer.removeAllViews();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(picWidth, picHeight);
            params.leftMargin = 0;
            params.topMargin = 0;
            if(size == SMALL_SIZE){
                if(mSmallCounter < mSmallIdList.length){
                    RelativeLayout frame = (RelativeLayout)findViewById(mSmallIdList[mSmallCounter++]);
                    frame.addView(f, params);
                }
            }

我很绝望,我尝试了大约一百种方法!

注意:这是 HONEYCOMB API,我使用的是 ANIMATOR,不是 3.0 之前的动画

最佳答案

还没有回复?可能为时已晚,但对于读者来说,这是解决方案:

anim.setRepeatCount(0);

关于android - 开始后无法在android中停止Animator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9623684/

相关文章:

menu - Android 3.0蜂窝: How to enable/disable Menu Items in Action Bar?

android - Android 3.0(Honeycomb)如何处理 "paste"事件?

android - 自定义 View 翻译动画在回收器 View 中不起作用

android - 如何从 Java 代码为 ImageView 制作图像变化动画

android - 从 Android 中的 RecyclerView 中删除焦点指示器

android - 深层链接不会打开新应用

android - 在服务中使用 GoogleApiClient

android - 在半透明 Activity 中显示警报对话框

Android Animate GONE 可见性

android - TrueType 无法在 Android 蜂窝中加载