私のMainActivityには2つのアニメーション(FABとTABタイトル)があり、それらをonBackPressedで止めたいと思います。BackPressedでアニメーションを停止する方法は?
@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");
}
}
}
}
詳細な例をお手伝いしてくれてありがとう!それは完璧に働いた。 – keikei38
嬉しいです:) – FAT