Scalified / fab

Floating Action Button Library for Android
Apache License 2.0
849 stars 164 forks source link

Animations not showing on default #25

Closed akuji1993 closed 9 years ago

akuji1993 commented 9 years ago

Hi, I'm using your library to do an app on a Moto G2. When I define the show and hide animations in the xml and through the actionButton.setHideAnimation(...) - methods, I can't see any animation. I honestly thought there's actually something really broken.

I decided to play around. I extracted the Animation object through actionButton.getShowAnimation(). I then called animation.setDuration(1000) and added them back with a actionButton.setShowAnimation(). NOW I can actually see all of the animations. This workaround is working, but I feel like this isn't the correct way done.

IS there something I might have done wrong or do I have to overwrite the duration to see the animations?

vbaidak commented 9 years ago

Hi akuji1993,

Animations are played when calling show(), hide(), dismiss() methods. Also there are additional methods for just playing the set animation: playShowAnimation(), playHideAnimation().

Note, that animations are played only if they are defined either in the XML resource or programmatically.

Please check the above and if no mistakes found describe in more detail your case when animation is not played (what method are you calling etc.)

akuji1993 commented 9 years ago

Wow, thanks for the super fast reply!

Neither show() nor playShowAnimation() work for making the button animate. The button appears, but without the animation.

My activity.xml for your Button looks like this: ... <com.software.shell.fab.ActionButton android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="@dimen/fab_margin" android:layout_marginBottom="@dimen/fab_margin" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" fab:type="DEFAULT" fab:button_color="@color/fab_material_blue_500" fab:button_colorPressed="@color/fab_material_blue_900" fab:image="@drawable/fab_plus_icon" fab:image_size="24dp" fab:shadow_color="#757575" fab:shadow_radius="5dp" fab:shadow_xOffset="5dp" fab:shadow_yOffset="5dp" fab:show_animation="@anim/fab_roll_from_down" fab:hide_animation="@anim/fab_roll_to_down" /> ...

Programmatically I use the button with this in onCreate(...):

    ActionButton actionButton = (ActionButton) findViewById(R.id.button);
    actionButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
        ....
        }
    });

    /** Reconfigure to actually see the animation
    Animation animation = actionButton.getShowAnimation();
    animation.setDuration(1000);
    actionButton.setShowAnimation(animation);
    animation = actionButton.getHideAnimation();
    animation.setDuration(1000);
    actionButton.setHideAnimation(animation); */

    actionButton.show();
    actionButton.playShowAnimation();

...

The code that's blocked out makes the animation work for me. Without it, the button just appears.

vbaidak commented 9 years ago

Hi,

As I understood, you want the animation to play when the ActionButton appears for the first time. In order to do this, the best way would be simply to call playShowAnimation(), you do not need to take such heavy actions.

Here are the sample code (it works, I tested it):

fab_activity_layout

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        style="@style/fab_layout_match_width_match_height_style"
        >
    <com.software.shell.fab.ActionButton
            android:id="@+id/fab_activity_action_button"
            style="@style/fab_action_button_style"
            fab:type="DEFAULT"
            fab:show_animation="@anim/fab_roll_from_down"
            fab:hide_animation="@anim/fab_roll_to_down"
            />
</RelativeLayout>

FABActivity

public class FABActivity extends Activity {

    private ActionButton actionButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fab_activity_layout);
        actionButton = (ActionButton) findViewById(R.id.fab_activity_action_button);
        actionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // button click action goes here
            }
        });
        actionButton.playShowAnimation();
    }

}

In this way, animation guaranteed will be played.

The other thing is that onCreate method used for Activity components initializations. This means that animation can be played so quickly, that your device won't be able to show it. In this case try to move:

    actionButton.playShowAnimation();

to onStart / onResume methods

Please let me know whether this helps Have a nice day

akuji1993 commented 9 years ago

Thanks for the detailed answer! Unfortunately, the suggestions you made don't work for me. In your xml-Sample there is a line

style="@style/fab_layout_match_width_match_height_style"

Android Studio marks this as error with "cannot resolve symbol '@style/fab_layout_match_width_match_height_style'". I'm definitely doing something wrong here..

The other suggestion to move the actionButton.playShowAnimation() to onStart() doesn't work either. I have overriden the method in my Activity and put actionButton.playShowAnimation() there. The result is the button appearing together with the Activity, without any animation.

I'm absolutely sure, it's something I do wrong. I don't want to waste your time, so if you don't have any immediate idea, what else I could try, I just use my workaround.

vbaidak commented 9 years ago

Hi,

Yup, sorry, it's just a style. Replace it in such a way:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        >
    <com.software.shell.fab.ActionButton
            android:id="@+id/fab_activity_action_button"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            fab:type="DEFAULT"
            fab:show_animation="@anim/fab_roll_from_down"
            fab:hide_animation="@anim/fab_roll_to_down"
            />
</RelativeLayout>

Okay, let's try to solve it as follows: I'll create a sample application and make a zip of a code + add the apk file for you, so that you'll have a chance to check it.

Please e-mail me, in order to send you the sample app

vbaidak commented 9 years ago

Issue resolved in private conversation. Suggested to use public void onWindowFocusChanged(boolean hasFocus)