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

Adding custom drawable to "AddFloatingActionButton" #353

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'com.android.tools.build:gradle:3.0.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Nov 27 11:18:21 CET 2014
#Thu Feb 01 15:58:50 CET 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
1 change: 0 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apply plugin: 'com.android.library'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
minSdkVersion 14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

public class AddFloatingActionButton extends FloatingActionButton {
int mPlusColor;
Drawable mCustomDrawable;

public AddFloatingActionButton(Context context) {
this(context, null);
Expand Down Expand Up @@ -61,28 +62,33 @@ public void setIcon(@DrawableRes int icon) {

@Override
Drawable getIconDrawable() {
final float iconSize = getDimension(R.dimen.fab_icon_size);
final float iconHalfSize = iconSize / 2f;

final float plusSize = getDimension(R.dimen.fab_plus_icon_size);
final float plusHalfStroke = getDimension(R.dimen.fab_plus_icon_stroke) / 2f;
final float plusOffset = (iconSize - plusSize) / 2f;
if (mCustomDrawable == null) {
final float iconSize = getDimension(R.dimen.fab_icon_size);
final float iconHalfSize = iconSize / 2f;

final Shape shape = new Shape() {
@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawRect(plusOffset, iconHalfSize - plusHalfStroke, iconSize - plusOffset, iconHalfSize + plusHalfStroke, paint);
canvas.drawRect(iconHalfSize - plusHalfStroke, plusOffset, iconHalfSize + plusHalfStroke, iconSize - plusOffset, paint);
}
};
final float plusSize = getDimension(R.dimen.fab_plus_icon_size);
final float plusHalfStroke = getDimension(R.dimen.fab_plus_icon_stroke) / 2f;
final float plusOffset = (iconSize - plusSize) / 2f;

ShapeDrawable drawable = new ShapeDrawable(shape);
final Shape shape = new Shape() {
@Override
public void draw(Canvas canvas, Paint paint) {
canvas.drawRect(plusOffset, iconHalfSize - plusHalfStroke, iconSize - plusOffset, iconHalfSize + plusHalfStroke, paint);
canvas.drawRect(iconHalfSize - plusHalfStroke, plusOffset, iconHalfSize + plusHalfStroke, iconSize - plusOffset, paint);
}
};

final Paint paint = drawable.getPaint();
paint.setColor(mPlusColor);
paint.setStyle(Style.FILL);
paint.setAntiAlias(true);
ShapeDrawable drawable = new ShapeDrawable(shape);

return drawable;
final Paint paint = drawable.getPaint();
paint.setColor(mPlusColor);
paint.setStyle(Style.FILL);
paint.setAntiAlias(true);

return drawable;
} else {
return mCustomDrawable;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ public class FloatingActionButton extends ImageButton {
private float mShadowRadius;
private float mShadowOffset;
private int mDrawableSize;
boolean mIconVisible;
boolean mStrokeVisible;

public boolean ismIconVisible() {
return mIconVisible;
}

public FloatingActionButton(Context context) {
this(context, null);
}
Expand All @@ -80,6 +85,7 @@ void init(Context context, AttributeSet attributeSet) {
mIcon = attr.getResourceId(R.styleable.FloatingActionButton_fab_icon, 0);
mTitle = attr.getString(R.styleable.FloatingActionButton_fab_title);
mStrokeVisible = attr.getBoolean(R.styleable.FloatingActionButton_fab_stroke_visible, true);
mIconVisible = attr.getBoolean(R.styleable.FloatingActionButton_fab_icon_visible, true);
attr.recycle();

updateCircleSize();
Expand All @@ -91,7 +97,7 @@ void init(Context context, AttributeSet attributeSet) {
}

private void updateDrawableSize() {
mDrawableSize = (int) (mCircleSize + 2 * mShadowRadius);
mDrawableSize = (int) (mCircleSize + 2 * mShadowRadius);
}

private void updateCircleSize() {
Expand Down Expand Up @@ -231,39 +237,43 @@ void updateBackground() {
final float strokeWidth = getDimension(R.dimen.fab_stroke_width);
final float halfStrokeWidth = strokeWidth / 2f;

LayerDrawable layerDrawable = new LayerDrawable(
new Drawable[] {
getResources().getDrawable(mSize == SIZE_NORMAL ? R.drawable.fab_bg_normal : R.drawable.fab_bg_mini),
createFillDrawable(strokeWidth),
createOuterStrokeDrawable(strokeWidth),
getIconDrawable()
});

int iconOffset = (int) (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2;

int circleInsetHorizontal = (int) (mShadowRadius);
int circleInsetTop = (int) (mShadowRadius - mShadowOffset);
int circleInsetBottom = (int) (mShadowRadius + mShadowOffset);

layerDrawable.setLayerInset(1,
circleInsetHorizontal,
circleInsetTop,
circleInsetHorizontal,
circleInsetBottom);

layerDrawable.setLayerInset(2,
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetTop - halfStrokeWidth),
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetBottom - halfStrokeWidth));

layerDrawable.setLayerInset(3,
circleInsetHorizontal + iconOffset,
circleInsetTop + iconOffset,
circleInsetHorizontal + iconOffset,
circleInsetBottom + iconOffset);

setBackgroundCompat(layerDrawable);
if (mIconVisible) {
LayerDrawable layerDrawable = new LayerDrawable(
new Drawable[]{
getResources().getDrawable(mSize == SIZE_NORMAL ? R.drawable.fab_bg_normal : R.drawable.fab_bg_mini),
createFillDrawable(strokeWidth),
createOuterStrokeDrawable(strokeWidth),
getIconDrawable()
});

int iconOffset = (int) (mCircleSize - getDimension(R.dimen.fab_icon_size)) / 2;

int circleInsetHorizontal = (int) (mShadowRadius);
int circleInsetTop = (int) (mShadowRadius - mShadowOffset);
int circleInsetBottom = (int) (mShadowRadius + mShadowOffset);

layerDrawable.setLayerInset(1,
circleInsetHorizontal,
circleInsetTop,
circleInsetHorizontal,
circleInsetBottom);

layerDrawable.setLayerInset(2,
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetTop - halfStrokeWidth),
(int) (circleInsetHorizontal - halfStrokeWidth),
(int) (circleInsetBottom - halfStrokeWidth));

layerDrawable.setLayerInset(3,
circleInsetHorizontal + iconOffset,
circleInsetTop + iconOffset,
circleInsetHorizontal + iconOffset,
circleInsetBottom + iconOffset);

setBackgroundCompat(layerDrawable);
} else {
setBackgroundCompat(new LayerDrawable(new Drawable[]{}));
}
}

Drawable getIconDrawable() {
Expand Down Expand Up @@ -306,6 +316,7 @@ private Drawable createCircleDrawable(int color, float strokeWidth) {
int halfStrokeWidth = (int) (strokeWidth / 2f);
drawable.setLayerInset(1, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth);


return drawable;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class FloatingActionsMenu extends ViewGroup {
private int mLabelsPosition;
private int mButtonsCount;

private Drawable customFloatingButtonIcon;
private boolean disableDrawableRotation;

private TouchDelegateGroup mTouchDelegateGroup;

private OnFloatingActionsMenuUpdateListener mListener;
Expand Down Expand Up @@ -100,6 +103,8 @@ private void init(Context context, AttributeSet attributeSet) {
mExpandDirection = attr.getInt(R.styleable.FloatingActionsMenu_fab_expandDirection, EXPAND_UP);
mLabelsStyle = attr.getResourceId(R.styleable.FloatingActionsMenu_fab_labelStyle, 0);
mLabelsPosition = attr.getInt(R.styleable.FloatingActionsMenu_fab_labelsPosition, LABELS_ON_LEFT_SIDE);
customFloatingButtonIcon = attr.getDrawable(R.styleable.FloatingActionsMenu_fab_addButtonCustomDrawableIcon);
disableDrawableRotation = attr.getBoolean(R.styleable.FloatingActionsMenu_fab_buttonDrawableDisableRotation, false);
attr.recycle();

if (mLabelsStyle != 0 && expandsHorizontally()) {
Expand Down Expand Up @@ -152,26 +157,31 @@ void updateBackground() {
mColorNormal = mAddButtonColorNormal;
mColorPressed = mAddButtonColorPressed;
mStrokeVisible = mAddButtonStrokeVisible;
mCustomDrawable = customFloatingButtonIcon;
super.updateBackground();
}

@Override
Drawable getIconDrawable() {
final RotatingDrawable rotatingDrawable = new RotatingDrawable(super.getIconDrawable());
mRotatingDrawable = rotatingDrawable;
if (disableDrawableRotation) {
return super.getIconDrawable();
} else {
final RotatingDrawable rotatingDrawable = new RotatingDrawable(super.getIconDrawable());
mRotatingDrawable = rotatingDrawable;

final OvershootInterpolator interpolator = new OvershootInterpolator();
final OvershootInterpolator interpolator = new OvershootInterpolator();

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 collapseAnimator = ObjectAnimator.ofFloat(rotatingDrawable, "rotation", EXPANDED_PLUS_ROTATION, COLLAPSED_PLUS_ROTATION);
final ObjectAnimator expandAnimator = ObjectAnimator.ofFloat(rotatingDrawable, "rotation", COLLAPSED_PLUS_ROTATION, EXPANDED_PLUS_ROTATION);

collapseAnimator.setInterpolator(interpolator);
expandAnimator.setInterpolator(interpolator);
collapseAnimator.setInterpolator(interpolator);
expandAnimator.setInterpolator(interpolator);

mExpandAnimation.play(expandAnimator);
mCollapseAnimation.play(collapseAnimator);
mExpandAnimation.play(expandAnimator);
mCollapseAnimation.play(collapseAnimator);

return rotatingDrawable;
return rotatingDrawable;
}
}
};

Expand Down
3 changes: 3 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</attr>
<attr name="fab_title" format="string"/>
<attr name="fab_stroke_visible" format="boolean"/>
<attr name="fab_icon_visible" format="boolean"/>
</declare-styleable>
<declare-styleable name="AddFloatingActionButton">
<attr name="fab_plusIconColor" format="color"/>
Expand All @@ -22,6 +23,8 @@
<enum name="normal" value="0"/>
<enum name="mini" value="1"/>
</attr>
<attr name="fab_addButtonCustomDrawableIcon" format="integer" />
<attr name="fab_buttonDrawableDisableRotation" format="boolean" />
<attr name="fab_addButtonPlusIconColor" format="color"/>
<attr name="fab_addButtonStrokeVisible" format="boolean"/>
<attr name="fab_labelStyle" format="reference"/>
Expand Down
1 change: 0 additions & 1 deletion sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
applicationId "com.getbase.floatingactionbutton.sample"
Expand Down
11 changes: 11 additions & 0 deletions sample/src/main/res/drawable/ic_search.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector android:height="24dp"
android:viewportHeight="22.0"
android:viewportWidth="22.0"
android:width="24dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<path
android:fillColor="#FFFFFF"
android:pathData="M9.41,15.435C6.181,15.435 3.551,12.815 3.551,9.584C3.551,6.355 6.181,3.725 9.41,3.725C12.64,3.725 15.27,6.355 15.27,9.584C15.27,12.815 12.64,15.435 9.41,15.435M21.051,18.725L16.83,14.505C17.801,13.055 18.32,11.355 18.32,9.584C18.32,4.664 14.32,0.675 9.41,0.675C4.5,0.675 0.5,4.664 0.5,9.584C0.5,14.495 4.5,18.495 9.41,18.495C11.36,18.495 13.21,17.865 14.74,16.725L18.89,20.885C19.181,21.175 19.57,21.325 19.971,21.325C20.38,21.325 20.761,21.175 21.051,20.885C21.65,20.285 21.65,19.315 21.051,18.725"
android:strokeColor="#00000000"
android:strokeWidth="1"/>
</vector>
11 changes: 7 additions & 4 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
fab:fab_addButtonColorNormal="@color/white"
fab:fab_addButtonColorNormal="@color/headerColor"
fab:fab_addButtonColorPressed="@color/white_pressed"
fab:fab_addButtonPlusIconColor="@color/half_black"
fab:fab_labelStyle="@style/menu_labels_style"
fab:fab_addButtonCustomDrawableIcon="@drawable/ic_search"
fab:fab_buttonDrawableDisableRotation="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
Expand All @@ -109,15 +110,17 @@
android:id="@+id/action_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_stroke_visible="false"
fab:fab_icon_visible="false"
fab:fab_title="Action A"
fab:fab_colorPressed="@color/white_pressed"/>

<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/action_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_colorNormal="@color/white"
fab:fab_stroke_visible="false"
fab:fab_icon_visible="false"
fab:fab_title="Action with a very long name that won\'t fit on the screen"
fab:fab_colorPressed="@color/white_pressed"/>

Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
<color name="pink_pressed">#ec407a</color>
<color name="blue_semi_transparent">#805677fc</color>
<color name="blue_semi_transparent_pressed">#80738ffe</color>

<color name="headerColor">#00B6AF</color>
</resources>