FAB does not animate - test code and screenshot attached

懵懂的女人 提交于 2019-12-11 12:13:08

问题


I have prepared a simple test project at GitHub for my question:

I am trying to show/hide a FloatingActionButton (aka FAB) every 5 seconds by the following code in MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mFab = (FloatingActionButton) findViewById(R.id.fab);

    mInAnimation = AnimationUtils.makeInAnimation(this, false);
    mInAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationEnd(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationStart(Animation animation) {
            mFab.setVisibility(View.VISIBLE);
        }
    });

    mOutAnimation = AnimationUtils.makeOutAnimation(this, true);
    mOutAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationEnd(Animation animation) {
            mFab.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationStart(Animation animation) {
        }
    });

    run();
}

The Runnable output ("Toggle animation") appears every 5 seconds in ADB logs, but the FAB is always visible:

@Override
public void run() {
    Log.d("MyCoordinator", "Toggle animation");
    mFab.setAnimation(mFab.isShown() ? mOutAnimation : mInAnimation);
    mHandler.postDelayed(this, 5000);
}

Does anybody please know, what is missing here?

Also I am curious, if it's possible to define the above animations in the activity_main.xml instead of Java code.


回答1:


I would rather use mFab.startAnimation(mFab.isShown() ? mOutAnimation : mInAnimation) than mFab.setAnimation(mFab.isShown() ? mOutAnimation : mInAnimation);. With setAnimation you have to define the start time of your animation (which is probably what you are missing)



来源:https://stackoverflow.com/questions/31773559/fab-does-not-animate-test-code-and-screenshot-attached

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!