Skip to content

Commit

Permalink
new feature keepProgressBarEnabledSize will stop the fab from jumping…
Browse files Browse the repository at this point in the history
… position when using coordinator anchors
  • Loading branch information
martinseal committed Sep 12, 2019
1 parent 4e7de21 commit 22583bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class FloatingActionButton extends ImageButton {

// Progress
private boolean mProgressBarEnabled;
private boolean keepProgressBarEnabledSize;
private int mProgressWidth = Util.dpToPx(getContext(), 6f);
private int mProgressColor;
private int mProgressBackgroundColor;
Expand Down Expand Up @@ -137,6 +138,7 @@ 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);
keepProgressBarEnabledSize = attr.getBoolean(R.styleable.FloatingActionButton_fab_keep_progress_size, false);

if (attr.hasValue(R.styleable.FloatingActionButton_fab_progress)) {
mProgress = attr.getInt(R.styleable.FloatingActionButton_fab_progress, 0);
Expand Down Expand Up @@ -186,15 +188,15 @@ private int getCircleSize() {

private int calculateMeasuredWidth() {
int width = getCircleSize() + calculateShadowWidth();
if (mProgressBarEnabled) {
if (mProgressBarEnabled | keepProgressBarEnabledSize) {
width += mProgressWidth * 2;
}
return width;
}

private int calculateMeasuredHeight() {
int height = getCircleSize() + calculateShadowHeight();
if (mProgressBarEnabled) {
if (mProgressBarEnabled | keepProgressBarEnabledSize) {
height += mProgressWidth * 2;
}
return height;
Expand Down Expand Up @@ -235,13 +237,15 @@ protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if (mProgressBarEnabled) {

if (mShowProgressBackground) {
canvas.drawArc(mProgressCircleBounds, 360, 360, false, mBackgroundPaint);
}

boolean shouldInvalidate = false;

if (mProgressIndeterminate) {

shouldInvalidate = true;

long deltaTime = SystemClock.uptimeMillis() - mLastTimeAnimated;
Expand All @@ -264,6 +268,7 @@ protected void onDraw(Canvas canvas) {
}

canvas.drawArc(mProgressCircleBounds, from, to, false, mProgressPaint);

} else {
if (mCurrentProgress != mTargetProgress) {
shouldInvalidate = true;
Expand Down Expand Up @@ -314,6 +319,7 @@ private void updateProgressLength(long deltaTimeInMillis) {

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {

saveButtonOriginalPosition();

if (mShouldProgressIndeterminate) {
Expand Down Expand Up @@ -368,7 +374,7 @@ void updateBackground() {
int circleInsetHorizontal = hasShadow() ? mShadowRadius + Math.abs(mShadowXOffset) : 0;
int circleInsetVertical = hasShadow() ? mShadowRadius + Math.abs(mShadowYOffset) : 0;

if (mProgressBarEnabled) {
if (mProgressBarEnabled | keepProgressBarEnabledSize) {
circleInsetHorizontal += mProgressWidth;
circleInsetVertical += mProgressWidth;
}
Expand Down Expand Up @@ -614,6 +620,7 @@ public Parcelable onSaveInstanceState() {
ss.mProgress = this.mProgress;
ss.mAnimateProgress = this.mAnimateProgress;
ss.mShowProgressBackground = this.mShowProgressBackground;
ss.keepProgressEnabledSize = this.keepProgressBarEnabledSize;

return ss;
}
Expand All @@ -635,6 +642,7 @@ public void onRestoreInstanceState(Parcelable state) {
this.mProgressColor = ss.mProgressColor;
this.mProgressBackgroundColor = ss.mProgressBackgroundColor;
this.mShouldProgressIndeterminate = ss.mShouldProgressIndeterminate;
this.keepProgressBarEnabledSize = ss.keepProgressEnabledSize;
this.mShouldSetProgress = ss.mShouldSetProgress;
this.mProgress = ss.mProgress;
this.mAnimateProgress = ss.mAnimateProgress;
Expand All @@ -656,7 +664,7 @@ private CircleDrawable(Shape s) {
circleInsetHorizontal = hasShadow() ? mShadowRadius + Math.abs(mShadowXOffset) : 0;
circleInsetVertical = hasShadow() ? mShadowRadius + Math.abs(mShadowYOffset) : 0;

if (mProgressBarEnabled) {
if (mProgressBarEnabled | keepProgressBarEnabledSize) {
circleInsetHorizontal += mProgressWidth;
circleInsetVertical += mProgressWidth;
}
Expand Down Expand Up @@ -736,6 +744,7 @@ static class ProgressSavedState extends BaseSavedState {
boolean mShouldSetProgress;
boolean mAnimateProgress;
boolean mShowProgressBackground;
boolean keepProgressEnabledSize;

ProgressSavedState(Parcelable superState) {
super(superState);
Expand All @@ -757,6 +766,7 @@ private ProgressSavedState(Parcel in) {
this.mShouldSetProgress = in.readInt() != 0;
this.mAnimateProgress = in.readInt() != 0;
this.mShowProgressBackground = in.readInt() != 0;
this.keepProgressEnabledSize = in.readInt() != 0;
}

@Override
Expand All @@ -776,6 +786,7 @@ public void writeToParcel(Parcel out, int flags) {
out.writeInt(this.mShouldSetProgress ? 1 : 0);
out.writeInt(this.mAnimateProgress ? 1 : 0);
out.writeInt(this.mShowProgressBackground ? 1 : 0);
out.writeInt(this.keepProgressEnabledSize ? 1 : 0);
}

public static final Parcelable.Creator<ProgressSavedState> CREATOR =
Expand Down Expand Up @@ -1173,6 +1184,7 @@ public synchronized void setIndeterminate(boolean indeterminate) {
}

mProgressBarEnabled = indeterminate;

mShouldUpdateButtonPosition = true;
mProgressIndeterminate = indeterminate;
mLastTimeAnimated = SystemClock.uptimeMillis();
Expand Down
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<attr name="fab_progress_max" format="integer" />
<attr name="fab_progress" format="integer" />
<attr name="fab_progress_showBackground" format="boolean" />
<attr name="fab_keep_progress_size" format="boolean" />
</declare-styleable>

<declare-styleable name="FloatingActionMenu">
Expand Down
6 changes: 4 additions & 2 deletions sample/src/main/res/layout/progress_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
android:layout_height="wrap_content"
fab:layout_anchor="@id/stage"
fab:layout_anchorGravity="top|end"
android:layout_margin="16dp"
android:layout_marginEnd="16dp"
android:src="@drawable/ic_progress"
fab:fab_keep_progress_size="true"
fab:fab_showAnimation="@anim/show_from_bottom"
fab:fab_hideAnimation="@anim/hide_to_bottom"/>
fab:fab_hideAnimation="@anim/hide_to_bottom"
android:layout_marginRight="16dp"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

0 comments on commit 22583bf

Please sign in to comment.