Skip to content
This repository was archived by the owner on Oct 23, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ Here are all the **FloatingActionButton**'s xml attributes with their **default
app:fab_progress_indeterminate="false"
app:fab_progress_max="100"
app:fab_progress="0"
app:fab_progress_showBackground="true"/>
app:fab_progress_showBackground="true"
fab:fab_icon_success="@drawable/your_drawable"
fab:fab_icon_failure="@drawable/your_drawable"
fab:fab_color_success="#84CA4B"
fab:fab_color_failure="#F8AE41"
fab:fab_success_failure_transition_animation_time="300"
fab:fab_success_failure_transition_animation_wait_time="2750"/>
```
All of these **FloatingActionButton**'s attributes has their corresponding getters and setters. So you can set them **programmatically**.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.github.clans.fab;

import android.animation.LayoutTransition;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
Expand All @@ -20,6 +23,7 @@
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Build;
Expand All @@ -32,7 +36,6 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewOutlineProvider;
import android.view.ViewParent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
Expand All @@ -54,20 +57,29 @@ public class FloatingActionButton extends ImageButton {
private static final long PAUSE_GROWING_TIME = 200;
private static final double BAR_SPIN_CYCLE_TIME = 500;
private static final int BAR_MAX_LENGTH = 270;
private static final int DEFAULT_TRANSITION_ANIMATION_TIME = 300;
private static final int DEFAULT_TRANSITION_ANIMATION_WAIT_TIME = 2750;

private int mColorNormal;
private int mColorPressed;
private int mColorDisabled;
private int mColorRipple;
private int mColorSuccess;
private int mColorFailure;
private Drawable mIcon;
private int mIconSize = Util.dpToPx(getContext(), 24f);
private Animation mShowAnimation;
private Animation mHideAnimation;
private AnimatorSet mSuccessFailureStateAnimatorSet;
private String mLabelText;
private OnClickListener mClickListener;
private Drawable mBackgroundDrawable;
private boolean mUsingElevation;
private boolean mUsingElevationCompat;
private Drawable mSuccessDrawable;
private Drawable mFailureDrawable;
private int mSuccessFailureAnimationTime;
private int mSuccessFailureAnimationWaitTime;

// Progress
private boolean mProgressBarEnabled;
Expand Down Expand Up @@ -135,6 +147,12 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
mProgressBackgroundColor = attr.getColor(R.styleable.FloatingActionButton_fab_progress_backgroundColor, 0x4D000000);
mProgressMax = attr.getInt(R.styleable.FloatingActionButton_fab_progress_max, mProgressMax);
mShowProgressBackground = attr.getBoolean(R.styleable.FloatingActionButton_fab_progress_showBackground, true);
mSuccessDrawable = attr.getDrawable(R.styleable.FloatingActionButton_fab_icon_success);
mFailureDrawable = attr.getDrawable(R.styleable.FloatingActionButton_fab_icon_failure);
mColorSuccess = attr.getColor(R.styleable.FloatingActionButton_fab_color_success, 0xFF84CA4B);
mColorFailure = attr.getColor(R.styleable.FloatingActionButton_fab_color_failure, 0xFFF8AE41);
mSuccessFailureAnimationTime = attr.getInt(R.styleable.FloatingActionButton_fab_success_failure_transition_animation_time, DEFAULT_TRANSITION_ANIMATION_TIME);
mSuccessFailureAnimationWaitTime = attr.getInt(R.styleable.FloatingActionButton_fab_success_failure_transition_animation_wait_time, DEFAULT_TRANSITION_ANIMATION_WAIT_TIME);

if (attr.hasValue(R.styleable.FloatingActionButton_fab_progress)) {
mProgress = attr.getInt(R.styleable.FloatingActionButton_fab_progress, 0);
Expand Down Expand Up @@ -1295,4 +1313,80 @@ public void showButtonInMenu(boolean animate) {
label.show(animate);
}
}

public void animateFailure() {
animateFromToFrom(mIcon, mFailureDrawable, mColorNormal, mColorFailure);
}

public void animateSuccess() {
animateFromToFrom(mIcon, mSuccessDrawable, mColorNormal, mColorSuccess);
}

public boolean isSuccessOrFailureAnimationRunning() {
if (mSuccessFailureStateAnimatorSet != null && mSuccessFailureStateAnimatorSet.isRunning()) {
return true;
}
return false;
}

public void animateFromToFrom(Drawable animateFromDrawable,
Drawable animateToDrawable,
Integer colorFrom,
Integer colorTo) {
if (isSuccessOrFailureAnimationRunning()) {
return;
}
Drawable drawableStates[] = new Drawable[2];
drawableStates[0] = animateFromDrawable;
drawableStates[1] = animateToDrawable;
final TransitionDrawable transitionDrawable = new TransitionDrawable(drawableStates);
transitionDrawable.setCrossFadeEnabled(true);
setImageDrawable(transitionDrawable);

transitionDrawable.startTransition(mSuccessFailureAnimationTime);

ValueAnimator changeColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
changeColorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
setColorNormal((Integer) animator.getAnimatedValue());
}
});
changeColorAnimation.setDuration(mSuccessFailureAnimationTime);

ValueAnimator doNothingAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
doNothingAnimation.setDuration(mSuccessFailureAnimationWaitTime);

ValueAnimator restoreColorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorTo, colorFrom);
restoreColorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
setColorNormal((Integer) animator.getAnimatedValue());
}
});
restoreColorAnimation.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
transitionDrawable.reverseTransition(mSuccessFailureAnimationTime);
}

@Override
public void onAnimationEnd(Animator animation) {
setImageDrawable(transitionDrawable.getDrawable(0));
}

@Override
public void onAnimationCancel(Animator animation) {
}

@Override
public void onAnimationRepeat(Animator animation) {
}
});
restoreColorAnimation.setDuration(mSuccessFailureAnimationTime);

mSuccessFailureStateAnimatorSet = new AnimatorSet();
mSuccessFailureStateAnimatorSet.playSequentially(changeColorAnimation, doNothingAnimation, restoreColorAnimation);
mSuccessFailureStateAnimatorSet.start();
}
}
6 changes: 6 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
<attr name="fab_progress_max" format="integer" />
<attr name="fab_progress" format="integer" />
<attr name="fab_progress_showBackground" format="boolean" />
<attr name="fab_icon_success" format="reference"/>
<attr name="fab_icon_failure" format="reference"/>
<attr name="fab_color_success" format="color"/>
<attr name="fab_color_failure" format="color"/>
<attr name="fab_success_failure_transition_animation_time" format="integer" />
<attr name="fab_success_failure_transition_animation_wait_time" format="integer" />
</declare-styleable>

<declare-styleable name="FloatingActionMenu">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
final FloatingActionButton loadingSuccessFab = (FloatingActionButton) findViewById(R.id.fab_loading_success);
fab.setMax(mMaxProgress);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
Expand Down Expand Up @@ -92,6 +93,20 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
}
}
});

loadingSuccessFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadingSuccessFab.setIndeterminate(true);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
loadingSuccessFab.animateSuccess();
loadingSuccessFab.setIndeterminate(false);
}
}, 1500);
}
});
}

private void increaseProgress(final FloatingActionButton fab, int i) {
Expand Down
Binary file added sample/src/main/res/drawable-hdpi/ic_done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/src/main/res/drawable-mdpi/ic_done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/src/main/res/drawable-xhdpi/ic_done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/src/main/res/drawable-xxhdpi/ic_done.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions sample/src/main/res/layout/recyclerview_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@
fab:fab_showAnimation="@anim/show_from_bottom"
fab:fab_hideAnimation="@anim/hide_to_bottom"/>

<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab_loading_success"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:src="@drawable/ic_progress"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
fab:fab_elevationCompat="4dp"
fab:fab_progress_backgroundColor="#9AC3FF"
fab:fab_progress_color="#264F8C"
fab:fab_colorNormal="#19345D"
fab:fab_colorPressed="#19345D"
fab:fab_colorRipple="#122645"
fab:fab_icon_success="@drawable/ic_done"
fab:fab_icon_failure="@drawable/ic_close"
fab:fab_color_success="#84CA4B"
fab:fab_color_failure="#F8AE41"
fab:fab_success_failure_transition_animation_time="300"
fab:fab_success_failure_transition_animation_wait_time="2750" />

</FrameLayout>