简体   繁体   中英

Fade animation on Floating Action Button

To preface, there are many questions that ask about fade animations, but none that seem to have my exact problem. I have a Floating Action Button in my application that I would like to fade out with a certain duration and delay. The code for the animation (which even though it shouldn't matter, is inside the floating action action listener) looks like this. In this piece of code, b is the FAB.

AlphaAnimation animation1 = new AlphaAnimation(1, 0);
animation1.setDuration(1000);
animation1.setStartOffset(1000);
animation1.setFillAfter(true);
b.startAnimation(animation1);

The code looks fine, but it just doesn't work, and I'm all out of ideas. I have tried using b.setAnimation() in accordance with b.animate() , but to no avail. I do not want to use XML, for the value of the animation startOffSet relies on other components.

Thanks in advance.

* **UPDATE ***

I've found a fix. instead of b.startAnimation(animation) , apply the animation to the button: b.setAnimation(animation) . If you then try to make the button/view invisible like so

b.setVisibility(View.INVISIBILE) 

It will use the animation you specified.

Can you please specify which android support design library version you are using? I tested the below code on 23.2.0 and animation is being performed just fine using below code:

final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlphaAnimation animation1 = new AlphaAnimation(1, 0);
            animation1.setDuration(1000);
            animation1.setStartOffset(1000);
            animation1.setFillAfter(true);
            fab.startAnimation(animation1);
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM