Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting animation in a FAB lib. #23

Open
Informatheus opened this issue Aug 11, 2015 · 5 comments
Open

Setting animation in a FAB lib. #23

Informatheus opened this issue Aug 11, 2015 · 5 comments

Comments

@Informatheus
Copy link

Hello! Congratulations at first.
I want this amazing animation on the button of my radio player, but im already using a FAB lib (https://github.com/Clans/FloatingActionButton) and I was not able to understand how to put this effect on my button.
Any help is appreciated. Thanks in advance.

@PrathameshKesarkar
Copy link

have you used co-ordinate layout

@PureDark
Copy link

I had the same need and I figured it out.
Here's my example to start the animation when the FAB is clicked (I use FAB from android design library but it should be the same with the FAB lib you're using):

FloatingActionButton fab_add = (FloatingActionButton) findViewById(R.id.fab_add);
        final Animatable icon = (Animatable) ResourcesCompat.getDrawable(this, R.drawable.ic_arrow_to_drawer);
        fab_add.setImageDrawable((Drawable)icon);
        fab_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                icon.start();
            }
        });

It is pretty much the same case with any other view that supports drawable as icon or background.

@svenoaks
Copy link

svenoaks commented Nov 26, 2016

@PureDark, this works, but only once for each drawable. How do you reset the animation state, so you can run the animation over and over?

@PureDark
Copy link

@svenoaks
Store the drawable,

private Animatable icon;

and do as below every single time:

    fab.setImageDrawable((Drawable)icon);
    icon.start();

a more elegance way would be:

public class MyFloatingActionButton extends FloatingActionButton {
    private Animatable startIcon, endIcon;
    public MyFloatingActionButton(Context context) {
        super(context);
    }

    public MyFloatingActionButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setStartIcon(Animatable startIcon){
        this.startIcon = startIcon;
        this.setImageDrawable((Drawable)startIcon);
    }

    public void setEndIcon(Animatable endIcon){
        this.endIcon = endIcon;
    }

    public void start(){
        if(startIcon!=null){
            this.setImageDrawable((Drawable)startIcon);
            startIcon.start();
        }
    }

    public void reverse(){
        if(endIcon!=null){
            this.setImageDrawable((Drawable)endIcon);
            endIcon.start();
        }
    }

}

And set the initial icon in onCreate

        Animatable crossStartIcon = (Animatable) ResourcesCompat.getDrawable(this, R.drawable.vector_animated_cross_0_to_45);
        Animatable crossEndIcon = (Animatable) ResourcesCompat.getDrawable(this, R.drawable.vector_animated_cross_45_to_0);
        fabAdd = (MyFloatingActionButton) findViewById(R.id.fab_add);
        fabAdd.setStartIcon(crossStartIcon);
        fabAdd.setEndIcon(crossEndIcon);

@svenoaks
Copy link

Thanks but gets messed up in random ways when screen rotates and variables reassigned. Only consistent way was to use AnimatedVectorDrawable to get the variable instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants