keikei38
keikei38

Reputation: 63

How to stop an animation onBackPressed?

In my MainActivity I have 2 animations (FAB and a TAB title) and I would like to stop them 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);

About the FAB it's easy.

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

But how do I stop the other animation defined like this? I don't know how to access the animation of the TAB below.

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");
            }
        }
    }
}

Upvotes: 1

Views: 1265

Answers (3)

lionscribe
lionscribe

Reputation: 3513

The other answers about saving the animation as an object variable, will not work in your code, as you are creating a separate animation for each tab.
Having said that, the fact that you are creating animations for tabs that are not visible, is a huge waste of resources. Change your code, add a pager listener, and set animation only on current page. Do that by creating single Animation object variable, attach it to current view. When page changes, cancel it, destroy it, and create new one for current page (not sure if you can reuse the old one). Now that you have a single Animation variable, you can also cancel it in onBackPressed.

Upvotes: 0

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

Declare an Animation object as a global variable. Use cancel() method to cancel the animation.

Try this:

// 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();
}

Upvotes: 1

ianhanniballake
ianhanniballake

Reputation: 199880

Store a reference to your Animation, then call cancel() on the Animation.

Upvotes: 0

Related Questions