fab: cleanup

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2018-06-01 03:16:32 +02:00 committed by Jason A. Donenfeld
parent 8c32c32c2b
commit 3c84b48f08
3 changed files with 32 additions and 34 deletions

View File

@ -58,7 +58,7 @@ public class AddFloatingActionButton extends FloatingActionButton {
} }
} }
public void setPlusColorResId(@ColorRes int plusColor) { public void setPlusColorResId(@ColorRes final int plusColor) {
setPlusColor(getColor(plusColor)); setPlusColor(getColor(plusColor));
} }

View File

@ -51,7 +51,7 @@ public class FloatingActionButton extends AppCompatImageButton {
init(context, attrs); init(context, attrs);
} }
public FloatingActionButton(Context context, AttributeSet attrs, int defStyle) { public FloatingActionButton(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
init(context, attrs); init(context, attrs);
} }
@ -294,7 +294,7 @@ public class FloatingActionButton extends AppCompatImageButton {
return drawable; return drawable;
} }
private Drawable createOuterStrokeDrawable(final float strokeWidth) { private static Drawable createOuterStrokeDrawable(final float strokeWidth) {
final ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape()); final ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
final Paint paint = shapeDrawable.getPaint(); final Paint paint = shapeDrawable.getPaint();
@ -311,15 +311,15 @@ public class FloatingActionButton extends AppCompatImageButton {
return (int) (255f * opacity); return (int) (255f * opacity);
} }
private int darkenColor(final int argb) { private static int darkenColor(final int argb) {
return adjustColorBrightness(argb, 0.9f); return adjustColorBrightness(argb, 0.9f);
} }
private int lightenColor(final int argb) { private static int lightenColor(final int argb) {
return adjustColorBrightness(argb, 1.1f); return adjustColorBrightness(argb, 1.1f);
} }
private int adjustColorBrightness(final int argb, final float factor) { private static int adjustColorBrightness(final int argb, final float factor) {
final float[] hsv = new float[3]; final float[] hsv = new float[3];
Color.colorToHSV(argb, hsv); Color.colorToHSV(argb, hsv);
@ -328,7 +328,7 @@ public class FloatingActionButton extends AppCompatImageButton {
return Color.HSVToColor(Color.alpha(argb), hsv); return Color.HSVToColor(Color.alpha(argb), hsv);
} }
private int halfTransparent(final int argb) { private static int halfTransparent(final int argb) {
return Color.argb( return Color.argb(
Color.alpha(argb) / 2, Color.alpha(argb) / 2,
Color.red(argb), Color.red(argb),
@ -337,7 +337,7 @@ public class FloatingActionButton extends AppCompatImageButton {
); );
} }
private int opaque(final int argb) { private static int opaque(final int argb) {
return Color.rgb( return Color.rgb(
Color.red(argb), Color.red(argb),
Color.green(argb), Color.green(argb),

View File

@ -10,6 +10,7 @@ import android.animation.Animator;
import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet; import android.animation.AnimatorSet;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
@ -19,6 +20,7 @@ import android.graphics.drawable.LayerDrawable;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.support.annotation.ColorRes; import android.support.annotation.ColorRes;
import android.support.annotation.Keep;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.ContextThemeWrapper; import android.view.ContextThemeWrapper;
@ -26,12 +28,12 @@ import android.view.TouchDelegate;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator; import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.OvershootInterpolator; import android.view.animation.OvershootInterpolator;
import android.widget.TextView; import android.widget.TextView;
import com.wireguard.android.R; import com.wireguard.android.R;
@SuppressWarnings("ReturnOfInnerClass")
public class FloatingActionsMenu extends ViewGroup { public class FloatingActionsMenu extends ViewGroup {
public static final int EXPAND_UP = 0; public static final int EXPAND_UP = 0;
public static final int EXPAND_DOWN = 1; public static final int EXPAND_DOWN = 1;
@ -44,9 +46,9 @@ public class FloatingActionsMenu extends ViewGroup {
private static final int ANIMATION_DURATION = 300; private static final int ANIMATION_DURATION = 300;
private static final float COLLAPSED_PLUS_ROTATION = 0f; private static final float COLLAPSED_PLUS_ROTATION = 0f;
private static final float EXPANDED_PLUS_ROTATION = 90f + 45f; private static final float EXPANDED_PLUS_ROTATION = 90f + 45f;
private static final Interpolator sExpandInterpolator = new OvershootInterpolator(); private static final TimeInterpolator EXPAND_INTERPOLATOR = new OvershootInterpolator();
private static final Interpolator sCollapseInterpolator = new DecelerateInterpolator(3f); private static final TimeInterpolator COLLAPSE_INTERPOLATOR = new DecelerateInterpolator(3f);
private static final Interpolator sAlphaExpandInterpolator = new DecelerateInterpolator(); private static final TimeInterpolator ALPHA_EXPAND_INTERPOLATOR = new DecelerateInterpolator();
private int mAddButtonPlusColor; private int mAddButtonPlusColor;
private int mAddButtonColorNormal; private int mAddButtonColorNormal;
private int mAddButtonColorPressed; private int mAddButtonColorPressed;
@ -74,7 +76,7 @@ public class FloatingActionsMenu extends ViewGroup {
this(context, null); this(context, null);
} }
public FloatingActionsMenu(final Context context, AttributeSet attrs) { public FloatingActionsMenu(final Context context, final AttributeSet attrs) {
super(context, attrs); super(context, attrs);
init(context, attrs); init(context, attrs);
} }
@ -134,7 +136,7 @@ public class FloatingActionsMenu extends ViewGroup {
final RotatingDrawable rotatingDrawable = new RotatingDrawable(super.getIconDrawable()); final RotatingDrawable rotatingDrawable = new RotatingDrawable(super.getIconDrawable());
mRotatingDrawable = rotatingDrawable; mRotatingDrawable = rotatingDrawable;
final OvershootInterpolator interpolator = new OvershootInterpolator(); final TimeInterpolator interpolator = new OvershootInterpolator();
final ObjectAnimator collapseAnimator = ObjectAnimator.ofFloat(rotatingDrawable, "rotation", EXPANDED_PLUS_ROTATION, COLLAPSED_PLUS_ROTATION); final ObjectAnimator collapseAnimator = ObjectAnimator.ofFloat(rotatingDrawable, "rotation", EXPANDED_PLUS_ROTATION, COLLAPSED_PLUS_ROTATION);
final ObjectAnimator expandAnimator = ObjectAnimator.ofFloat(rotatingDrawable, "rotation", COLLAPSED_PLUS_ROTATION, EXPANDED_PLUS_ROTATION); final ObjectAnimator expandAnimator = ObjectAnimator.ofFloat(rotatingDrawable, "rotation", COLLAPSED_PLUS_ROTATION, EXPANDED_PLUS_ROTATION);
@ -151,12 +153,7 @@ public class FloatingActionsMenu extends ViewGroup {
mAddButton.setId(R.id.fab_expand_menu_button); mAddButton.setId(R.id.fab_expand_menu_button);
mAddButton.setSize(mAddButtonSize); mAddButton.setSize(mAddButtonSize);
mAddButton.setOnClickListener(new OnClickListener() { mAddButton.setOnClickListener(v -> toggle());
@Override
public void onClick(View v) {
toggle();
}
});
addView(mAddButton, super.generateDefaultLayoutParams()); addView(mAddButton, super.generateDefaultLayoutParams());
mButtonsCount++; mButtonsCount++;
@ -194,7 +191,7 @@ public class FloatingActionsMenu extends ViewGroup {
int maxLabelWidth = 0; int maxLabelWidth = 0;
for (int i = 0; i < mButtonsCount; i++) { for (int i = 0; i < mButtonsCount; i++) {
View child = getChildAt(i); final View child = getChildAt(i);
if (child.getVisibility() == GONE) { if (child.getVisibility() == GONE) {
continue; continue;
@ -214,17 +211,17 @@ public class FloatingActionsMenu extends ViewGroup {
} }
if (!expandsHorizontally()) { if (!expandsHorizontally()) {
TextView label = (TextView) child.getTag(R.id.fab_label); final TextView label = (TextView) child.getTag(R.id.fab_label);
if (label != null) { if (label != null) {
maxLabelWidth = Math.max(maxLabelWidth, label.getMeasuredWidth()); maxLabelWidth = Math.max(maxLabelWidth, label.getMeasuredWidth());
} }
} }
} }
if (!expandsHorizontally()) { if (expandsHorizontally()) {
width = mMaxButtonWidth + (maxLabelWidth > 0 ? maxLabelWidth + mLabelsMargin : 0);
} else {
height = mMaxButtonHeight; height = mMaxButtonHeight;
} else {
width = mMaxButtonWidth + (maxLabelWidth > 0 ? maxLabelWidth + mLabelsMargin : 0);
} }
switch (mExpandDirection) { switch (mExpandDirection) {
@ -243,7 +240,7 @@ public class FloatingActionsMenu extends ViewGroup {
setMeasuredDimension(width, height); setMeasuredDimension(width, height);
} }
private int adjustForOvershoot(final int dimension) { private static int adjustForOvershoot(final int dimension) {
return dimension * 12 / 10; return dimension * 12 / 10;
} }
@ -290,7 +287,7 @@ public class FloatingActionsMenu extends ViewGroup {
child.setTranslationY(mExpanded ? expandedTranslation : collapsedTranslation); child.setTranslationY(mExpanded ? expandedTranslation : collapsedTranslation);
child.setAlpha(mExpanded ? 1f : 0f); child.setAlpha(mExpanded ? 1f : 0f);
LayoutParams params = (LayoutParams) child.getLayoutParams(); final LayoutParams params = (LayoutParams) child.getLayoutParams();
params.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation); params.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation);
params.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation); params.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation);
params.setAnimationsTarget(child); params.setAnimationsTarget(child);
@ -322,7 +319,7 @@ public class FloatingActionsMenu extends ViewGroup {
label.setTranslationY(mExpanded ? expandedTranslation : collapsedTranslation); label.setTranslationY(mExpanded ? expandedTranslation : collapsedTranslation);
label.setAlpha(mExpanded ? 1f : 0f); label.setAlpha(mExpanded ? 1f : 0f);
LayoutParams labelParams = (LayoutParams) label.getLayoutParams(); final LayoutParams labelParams = (LayoutParams) label.getLayoutParams();
labelParams.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation); labelParams.mCollapseDir.setFloatValues(expandedTranslation, collapsedTranslation);
labelParams.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation); labelParams.mExpandDir.setFloatValues(collapsedTranslation, expandedTranslation);
labelParams.setAnimationsTarget(label); labelParams.setAnimationsTarget(label);
@ -525,6 +522,7 @@ public class FloatingActionsMenu extends ViewGroup {
return mRotation; return mRotation;
} }
@Keep
@SuppressWarnings("UnusedDeclaration") @SuppressWarnings("UnusedDeclaration")
public void setRotation(final float rotation) { public void setRotation(final float rotation) {
mRotation = rotation; mRotation = rotation;
@ -544,12 +542,12 @@ public class FloatingActionsMenu extends ViewGroup {
public static final Creator<SavedState> CREATOR = new Creator<SavedState>() { public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
@Override @Override
public SavedState createFromParcel(Parcel in) { public SavedState createFromParcel(final Parcel in) {
return new SavedState(in); return new SavedState(in);
} }
@Override @Override
public SavedState[] newArray(int size) { public SavedState[] newArray(final int size) {
return new SavedState[size]; return new SavedState[size];
} }
}; };
@ -582,10 +580,10 @@ public class FloatingActionsMenu extends ViewGroup {
LayoutParams(final ViewGroup.LayoutParams source) { LayoutParams(final ViewGroup.LayoutParams source) {
super(source); super(source);
mExpandDir.setInterpolator(sExpandInterpolator); mExpandDir.setInterpolator(EXPAND_INTERPOLATOR);
mExpandAlpha.setInterpolator(sAlphaExpandInterpolator); mExpandAlpha.setInterpolator(ALPHA_EXPAND_INTERPOLATOR);
mCollapseDir.setInterpolator(sCollapseInterpolator); mCollapseDir.setInterpolator(COLLAPSE_INTERPOLATOR);
mCollapseAlpha.setInterpolator(sCollapseInterpolator); mCollapseAlpha.setInterpolator(COLLAPSE_INTERPOLATOR);
mCollapseAlpha.setProperty(View.ALPHA); mCollapseAlpha.setProperty(View.ALPHA);
mCollapseAlpha.setFloatValues(1f, 0f); mCollapseAlpha.setFloatValues(1f, 0f);