From e5b58682670ead46ee41d76375b1d317886f76b5 Mon Sep 17 00:00:00 2001 From: Steffen Funke Date: Wed, 25 Feb 2015 08:21:09 +0100 Subject: [PATCH 1/4] added new fab_size property for custom size requirements --- .../com/melnykov/fab/FloatingActionButton.java | 14 +++++++++----- library/src/main/res/values/attrs.xml | 1 + 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/library/src/main/java/com/melnykov/fab/FloatingActionButton.java b/library/src/main/java/com/melnykov/fab/FloatingActionButton.java index 0393448f..dfb9c9d2 100644 --- a/library/src/main/java/com/melnykov/fab/FloatingActionButton.java +++ b/library/src/main/java/com/melnykov/fab/FloatingActionButton.java @@ -53,6 +53,7 @@ public class FloatingActionButton extends ImageButton { private int mColorRipple; private boolean mShadow; private int mType; + private int mSize; private int mShadowSize; @@ -79,8 +80,7 @@ public FloatingActionButton(Context context, AttributeSet attrs, int defStyle) { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); - int size = getDimension( - mType == TYPE_NORMAL ? R.dimen.fab_size_normal : R.dimen.fab_size_mini); + int size = mSize; if (mShadow && !hasLollipopApi()) { size += mShadowSize * 2; setMarginsWithoutShadow(); @@ -97,6 +97,7 @@ private void init(Context context, AttributeSet attributeSet) { mShadow = true; mScrollThreshold = getResources().getDimensionPixelOffset(R.dimen.fab_scroll_threshold); mShadowSize = getDimension(R.dimen.fab_shadow_size); + mSize = getDefaultSize(); if (attributeSet != null) { initAttributes(context, attributeSet); } @@ -115,12 +116,17 @@ private void initAttributes(Context context, AttributeSet attributeSet) { getColor(android.R.color.white)); mShadow = attr.getBoolean(R.styleable.FloatingActionButton_fab_shadow, true); mType = attr.getInt(R.styleable.FloatingActionButton_fab_type, TYPE_NORMAL); + mSize = attr.getDimensionPixelSize(R.styleable.FloatingActionButton_fab_size, mSize); } finally { attr.recycle(); } } } + private int getDefaultSize() { + return getDimension(mType == TYPE_NORMAL ? R.dimen.fab_size_normal : R.dimen.fab_size_mini); + } + private void updateBackground() { StateListDrawable drawable = new StateListDrawable(); drawable.addState(new int[]{android.R.attr.state_pressed}, createDrawable(mColorPressed)); @@ -189,9 +195,7 @@ private void setBackgroundCompat(Drawable drawable) { setOutlineProvider(new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { - int size = getDimension(mType == TYPE_NORMAL ? R.dimen.fab_size_normal - : R.dimen.fab_size_mini); - outline.setOval(0, 0, size, size); + outline.setOval(0, 0, mSize, mSize); } }); setClipToOutline(true); diff --git a/library/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml index e42a1f49..91381c0f 100644 --- a/library/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -6,6 +6,7 @@ + From 4289df1396fd3c2d532561291cffb578b4417c02 Mon Sep 17 00:00:00 2001 From: Steffen Funke Date: Wed, 25 Feb 2015 08:59:26 +0100 Subject: [PATCH 2/4] adjusted README for fab_size attribute --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a83906ed..273f56f0 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ dependencies { } ``` -**2)** Add the ``com.melnykov.fab.FloatingActionButton`` to your layout XML file. The button should be placed in the bottom right corner of the screen. The width and height of the floating action button are hardcoded to **56dp** for the normal and **40dp** for the mini button as specified in the [guidlines]. +**2)** Add the ``com.melnykov.fab.FloatingActionButton`` to your layout XML file. The button should be placed in the bottom right corner of the screen. The size (diameter) of the floating action button is hardcoded to **56dp** for the normal and **40dp** for the mini button as specified in the [guidlines], but can be also be overridden manually via ``fab_size`` dimension attribute, if this special case is required. ```xml Date: Wed, 25 Feb 2015 09:02:03 +0100 Subject: [PATCH 3/4] fixed typo in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 273f56f0..7970b361 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ dependencies { } ``` -**2)** Add the ``com.melnykov.fab.FloatingActionButton`` to your layout XML file. The button should be placed in the bottom right corner of the screen. The size (diameter) of the floating action button is hardcoded to **56dp** for the normal and **40dp** for the mini button as specified in the [guidlines], but can be also be overridden manually via ``fab_size`` dimension attribute, if this special case is required. +**2)** Add the ``com.melnykov.fab.FloatingActionButton`` to your layout XML file. The button should be placed in the bottom right corner of the screen. The size (diameter) of the floating action button is hardcoded to **56dp** for the normal and **40dp** for the mini button as specified in the [guidlines], but can also be overridden manually via the ``fab_size`` dimension attribute, if this special case is required. ```xml Date: Wed, 15 Apr 2015 09:52:22 +0200 Subject: [PATCH 4/4] Fix custom shadow size --- .../melnykov/fab/FloatingActionButton.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/melnykov/fab/FloatingActionButton.java b/library/src/main/java/com/melnykov/fab/FloatingActionButton.java index 7c34b055..34b45956 100644 --- a/library/src/main/java/com/melnykov/fab/FloatingActionButton.java +++ b/library/src/main/java/com/melnykov/fab/FloatingActionButton.java @@ -37,6 +37,7 @@ public class FloatingActionButton extends ImageButton { private static final int TRANSLATE_DURATION_MILLIS = 200; + private static final float SHADOW_SIZE_RELATIVE = 0.2f; // shadow padding relative to width / height, based on fab_shadow & fab_shadow_mini drawables. @Retention(RetentionPolicy.SOURCE) @IntDef({TYPE_NORMAL, TYPE_MINI}) @@ -97,12 +98,15 @@ private void init(Context context, AttributeSet attributeSet) { mColorDisabled = getColor(android.R.color.darker_gray); mType = TYPE_NORMAL; mShadow = true; + mSize = -1; mScrollThreshold = getResources().getDimensionPixelOffset(R.dimen.fab_scroll_threshold); - mShadowSize = getDimension(R.dimen.fab_shadow_size); - mSize = getDefaultSize(); + if (attributeSet != null) { initAttributes(context, attributeSet); } + + calculateSize(); + calculateShadowSize(); updateBackground(); } @@ -120,17 +124,30 @@ private void initAttributes(Context context, AttributeSet attributeSet) { mColorDisabled); mShadow = attr.getBoolean(R.styleable.FloatingActionButton_fab_shadow, true); mType = attr.getInt(R.styleable.FloatingActionButton_fab_type, TYPE_NORMAL); - mSize = attr.getDimensionPixelSize(R.styleable.FloatingActionButton_fab_size, mSize); + + // TYPE_MINI overrides any custom size + if(mType == TYPE_NORMAL) { + mSize = attr.getDimensionPixelSize(R.styleable.FloatingActionButton_fab_size, mSize); + } } finally { attr.recycle(); } } } + private void calculateSize() { + mSize = mSize == -1 ? getDefaultSize() : mSize; + } + private int getDefaultSize() { return getDimension(mType == TYPE_NORMAL ? R.dimen.fab_size_normal : R.dimen.fab_size_mini); } + private void calculateShadowSize() { + // custom shadow size for TYPE_NORMAL only, TYPE_MINI gets default shadow size + mShadowSize = (mType == TYPE_NORMAL) ? (int)(SHADOW_SIZE_RELATIVE * mSize) : getDimension(R.dimen.fab_shadow_size); + } + private void updateBackground() { StateListDrawable drawable = new StateListDrawable(); drawable.addState(new int[]{android.R.attr.state_pressed}, createDrawable(mColorPressed));