本文介绍了FAB并不动画 - 测试code和附截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有prepared 一个简单的测试项目我的问题:

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

我想显示/隐藏 FloatingActionButton (又名FAB)每5秒由 MainActivity.java :

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

的Runnable 输出(切换动画)出现在亚行日志中,每5秒,但FAB始终是可见的:

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?

另外我很好奇,如果有可能确定在 activity_main.xml中而不是Java code。

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

推荐答案

我宁愿用 mFab.startAnimation(mFab.isShown()mOutAnimation:mInAnimation) mFab.setAnimation(mFab.isShown()mOutAnimation:mInAnimation); 。随着 setAnimation 你必须定义动画的开始时间(这可能是你错过了什么)

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)

这篇关于FAB并不动画 - 测试code和附截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 02:06