FAB并不动画 - 测试code和附截图 [英] FAB does not animate - test code and screenshot attached

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

问题描述

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

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

应用截图

我想显示/隐藏 FloatingActionButton (又名FAB)每5秒由<一个下列code href=\"https://github.com/afarber/android-newbie/blob/q35/MyCoordinator/app/src/main/java/de/afarber/mycoordinator/MainActivity.java\"相对=nofollow> 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?

另外我很好奇,如果有可能确定在<一个以上的动画href=\"https://github.com/afarber/android-newbie/blob/q35/MyCoordinator/app/src/main/res/layout/activity_main.xml\"相对=nofollow> 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和附截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆