2017-04-02 104 views
0

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

回答

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

非常感謝您的幫助!它工作完美。 – keikei38

+0

很高興知道:) – FAT

0

關於保存動畫作爲一個對象變量,其他的答案,也不會在你的代碼工作,因爲要創建爲每個標籤的獨立動畫。
話雖如此,您創建不可見標籤的動畫是一個巨大的資源浪費。 更改您的代碼,添加尋呼機偵聽器,並僅在當前頁面上設置動畫。通過創建單個動畫對象變量來實現,將其附加到當前視圖。當頁面發生變化時,取消它,將其銷燬併爲當前頁面創建一個新頁面(不確定是否可以重新使用舊頁面)。既然你有一個動畫變量,你也可以在onBackPressed中取消它。

+0

一次只有一個選項卡標題是動畫的(哪個選項卡是從服務器上的JSON數據中選擇的) – keikei38

+0

但是你說得對,如果多個選項卡是動畫的,那麼動畫不會被取消。 – keikei38