Skip to content

Commit

Permalink
added hide and show animation to floating action menu
Browse files Browse the repository at this point in the history
  • Loading branch information
martinseal committed Nov 3, 2019
1 parent 2ff9462 commit 42faef0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@

public class FloatingActionMenu extends ViewGroup {

private Animation mShowAnimation;
private Animation mHideAnimation;

private static final int ANIMATION_DURATION = 300;
private static final float CLOSED_PLUS_ROTATION = 0f;
private static final float OPENED_PLUS_ROTATION_LEFT = -90f - 45f;
Expand Down Expand Up @@ -211,6 +214,8 @@ private void init(Context context, AttributeSet attrs) {
createMenuButton();
initMenuButtonAnimations(attr);

initShowAnimation(attr);
initHideAnimation(attr);
attr.recycle();
}

Expand Down Expand Up @@ -1258,4 +1263,72 @@ public void setOnMenuButtonClickListener(OnClickListener clickListener) {
public void setOnMenuButtonLongClickListener(OnLongClickListener longClickListener) {
mMenuButton.setOnLongClickListener(longClickListener);
}








public boolean isHidden() {
return getVisibility() == INVISIBLE;
}

public void show(boolean animate) {
if (isHidden()) {
if (animate) {
playShowAnimation();
}
super.setVisibility(VISIBLE);
}
}

public void hide(boolean animate) {
if (!isHidden()) {
if (animate) {
playHideAnimation();
}
super.setVisibility(INVISIBLE);
}
}

private void initShowAnimation(TypedArray attr) {
int resourceId = attr.getResourceId(R.styleable.FloatingActionButton_fab_showAnimation, R.anim.fab_scale_up);
mShowAnimation = AnimationUtils.loadAnimation(getContext(), resourceId);
}

private void initHideAnimation(TypedArray attr) {
int resourceId = attr.getResourceId(R.styleable.FloatingActionButton_fab_hideAnimation, R.anim.fab_scale_down);
mHideAnimation = AnimationUtils.loadAnimation(getContext(), resourceId);
}

public void setShowAnimation(Animation showAnimation) {
mShowAnimation = showAnimation;
}

public void setHideAnimation(Animation hideAnimation) {
mHideAnimation = hideAnimation;
}


Animation getShowAnimation() {
return mShowAnimation;
}

Animation getHideAnimation() {
return mHideAnimation;
}

void playShowAnimation() {
mHideAnimation.cancel();
startAnimation(mShowAnimation);
}

void playHideAnimation() {
mShowAnimation.cancel();
startAnimation(mHideAnimation);
}


}
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
<attr name="menu_fab_label" format="string" />
<attr name="menu_fab_show_animation" format="reference" />
<attr name="menu_fab_hide_animation" format="reference" />
<attr name="menu_showAnimation" format="reference" />
<attr name="menu_hideAnimation" format="reference" />
</declare-styleable>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class MenusFragment extends Fragment {
private FloatingActionButton fab3;

private FloatingActionButton fabEdit;
private boolean isHidden = false;

private List<FloatingActionMenu> menus = new ArrayList<>();
private Handler mUiHandler = new Handler();
Expand Down Expand Up @@ -95,6 +96,17 @@ public void onClick(View v) {
menuLabelsRight.hideMenuButton(false);

fabEdit = (FloatingActionButton) view.findViewById(R.id.fab_edit);
fabEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isHidden = !isHidden;
if (isHidden){
menuRed.show(true);
} else {
menuRed.hide(true);
}
}
});
fabEdit.setShowAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.scale_up));
fabEdit.setHideAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.scale_down));
}
Expand Down
3 changes: 2 additions & 1 deletion sample/src/main/res/layout/menus_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
fab:menu_icon="@drawable/ic_star"
fab:menu_animationDelayPerItem="0"
fab:menu_colorNormal="#43A047"
fab:menu_colorPressed="#2E7D32"
Expand Down Expand Up @@ -222,6 +221,8 @@
android:paddingLeft="10dp"
fab:menu_labels_ellipsize="end"
fab:menu_labels_singleLine="true"
fab:fab_showAnimation="@anim/show_from_bottom"
fab:fab_hideAnimation="@anim/hide_to_bottom"
fab:menu_fab_label="Menu label">

<com.github.sealstudios.fab.FloatingActionButton
Expand Down

0 comments on commit 42faef0

Please sign in to comment.