如何停止动画onBackPressed?

时间:2017-04-02 03:39:41

标签: android

在我的MainActivity中,我有2个动画(FAB和TAB标题),我想在onBackPressed上停止它们。

@Bind(R.id.fab_text) Button mFAB;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_drawer);

    Animation animation = loadAnimation(MainDrawerActivity.this, R.anim.fab_scale_up_down);
    animation.setRepeatCount(Animation.INFINITE);
    animation.setAnimationListener(new SimpleAnimationListener() {
        private long offset;
        private long startTime;

        @Override
        public void onAnimationStart(Animation animation) {
            startTime = System.currentTimeMillis();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            final long now = System.currentTimeMillis();
            Timber.i("onAnimationRepeatFAB: elapeed seconds: %d", (now - startTime) / 1000);
            if ((now - startTime > 7000) && (offset % 4 == 0)) { // stop animation after X seconds
                animation.setRepeatCount(0);
            } else {
                offset++;
                animation.setStartOffset(offset % 4 == 0 ? 700 : 0);
            }
        }
    });
    mFAB.startAnimation(animation);

关于FAB很容易。

public void onBackPressed() {
    mFAB.clearAnimation();

但是如何停止这样定义的其他动画呢?我不知道如何访问下面的TAB动画。

private void populateViewPager(List<Tab> tabs) {
    // clear all listeners before populating new tabs
    mTabLayout.setOnTabSelectedListener(null);
    mViewPager.clearOnPageChangeListeners();

    if (mPagerAdapter == null) {
        mPagerAdapter = new TabsPagerAdapter(this, getSupportFragmentManager());
        mViewPager.setAdapter(mPagerAdapter);
    }

    // populate tabs
    mPagerAdapter.setTabs(tabs);
    if (mPagerAdapter.getCount() > DEFAULT_TAB_POSITION)
        mViewPager.setCurrentItem(DEFAULT_TAB_POSITION);
    mTabLayout.setupWithViewPager(mViewPager);

    // set animation on corresponding tabs
    List<Tab> pagerTabs = mPagerAdapter.getTabs();
    for (int i = 0; i < pagerTabs.size(); i++) {
        Tab pagerTab = pagerTabs.get(i);
        if (pagerTab.isAnimated()) {
            Timber.i("Animating tab: %s", pagerTab.getId());
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            if (tab != null) {
                // set custom view in order to get it back then
                tab.setCustomView(R.layout.partial_tab_view);

                // set animation on the custom view
                Animation animation = loadAnimation(MainDrawerActivity.this, R.anim.tab_scale_up_down);
                animation.setRepeatCount(Animation.INFINITE);
                animation.setAnimationListener(new SimpleAnimationListener() {
                    private long offset;
                    private long startTime;

                    @Override
                    public void onAnimationStart(Animation animation) {
                        startTime = System.currentTimeMillis();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        final long now = System.currentTimeMillis();
                        Timber.i("onAnimationRepeat: elapeed seconds: %d", (now - startTime) / 1000);
                        if ((now - startTime > 7000) && (offset % 4 == 0)) { // stop animation after X seconds
                            animation.setRepeatCount(0);
                        } else {
                            offset++;
                            animation.setStartOffset(offset % 4 == 0 ? 700 : 0);
                        }
                    }
                });
                //noinspection ConstantConditions
                tab.getCustomView().setAnimation(animation);
            } else {
                Timber.w("tab!=null");
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

存储对Animation的引用,然后致电Animation上的cancel()

答案 1 :(得分:0)

Animation对象声明为global变量。使用cancel()方法取消动画。

试试这个:

// Animation
Animation animation;

............
..............

private void populateViewPager(List<Tab> tabs) {
    .................
    ..............................

    animation = AnimationUtils.loadAnimation(MainDrawerActivity.this, R.anim.tab_scale_up_down);

    ..............
    .....................
}

@Override
public void onBackPressed() {

    animation.cancel();
    super.onBackPressed();
}

答案 2 :(得分:0)

将动画保存为对象变量的其他答案在您的代码中无效,因为您要为每个标签创建单独的动画。
话虽如此,您正在为不可见的选项卡创建动画这一事实是对资源的巨大浪费。 更改代码,添加寻呼机侦听器,并仅在当前页面上设置动画。通过创建单个Animation对象变量来执行此操作,将其附加到当前视图。页面更改时,取消它,销毁它,并为当前页面创建一个新页面(不确定是否可以重复使用旧页面)。既然你有一个动画变量,你也可以在onBackPressed中取消它。