Skip to content

Commit

Permalink
A few more touches
Browse files Browse the repository at this point in the history
  • Loading branch information
liutikas committed Oct 10, 2016
1 parent 8f341ef commit 7167f9c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 35 additions & 8 deletions stepperwidget/src/main/java/net/liutikas/widget/StepperWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
* A simple widget that will allow users to step up and step down a number and a number will animate
* as user presses the plus and minus buttons.
*/
public class StepperWidget extends LinearLayout {
public interface OnUpdateListener {
void onUpdate(int oldValue, int newValue);
}

private final int mLabelHeight;
private final ImageButton mPlusButton;
private final ImageButton mMinusButton;
private final TextView mLabelOne;
Expand All @@ -25,6 +34,7 @@ public class StepperWidget extends LinearLayout {
private int mCounter = 0;
private int mChange = 0;

private List<OnUpdateListener> mListeners;
private int mMin = Integer.MIN_VALUE;
private int mMax = Integer.MAX_VALUE;

Expand All @@ -47,6 +57,8 @@ public StepperWidget(Context context, AttributeSet attrs, int defStyleAttr) {
mMinusButton = (ImageButton) findViewById(R.id.minus);
mLabelOne = (TextView) findViewById(R.id.label1);
mLabelTwo = (TextView) findViewById(R.id.label2);
mLabelHeight =
context.getResources().getDimensionPixelSize(R.dimen.stepper_widget_label_height);
mPlusButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Expand All @@ -62,6 +74,7 @@ public void onClick(View view) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
initalizeAnimator();
}
mListeners = new ArrayList<>();
updateLabel(0);
}

Expand All @@ -85,6 +98,14 @@ public void setMin(int min) {
updateLabel(0);
}

public void addOnUpdateListener(OnUpdateListener listener) {
mListeners.add(listener);
}

public void removeOnUpdateListener(OnUpdateListener listener) {
mListeners.remove(listener);
}

/**
* @return The value that the user has currently chosen using this widget.
*/
Expand All @@ -104,11 +125,11 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
if (mChange == 0) {
// Do nothing
} else if (mChange < 0) {
getEnteringLabel().setTranslationY(100 - value);
getLeavingLabel().setTranslationY(-value);
getEnteringLabel().setTranslationY((100 - value) * mLabelHeight / 100f);
getLeavingLabel().setTranslationY(-value * mLabelHeight / 100f);
} else {
getEnteringLabel().setTranslationY(-100 + value);
getLeavingLabel().setTranslationY(value);
getEnteringLabel().setTranslationY((-100 + value) * mLabelHeight / 100f);
getLeavingLabel().setTranslationY(value * mLabelHeight / 100f);
}
}
});
Expand Down Expand Up @@ -141,9 +162,9 @@ public void onAnimationUpdate(ValueAnimator valueAnimator) {
if (mChange == 0) {
// Do nothing.
} else if (mChange < 0) {
getLeavingLabel().setTranslationY(-value);
getLeavingLabel().setTranslationY(-value * mLabelHeight / 100f);
} else {
getLeavingLabel().setTranslationY(value);
getLeavingLabel().setTranslationY(value * mLabelHeight / 100f);
}
}
});
Expand Down Expand Up @@ -175,11 +196,17 @@ private void updateLabel(int change) {
updateLabelVisibility(mLabelOneVisible);
bounceLabelAnimated(change);
} else {
for (OnUpdateListener listener : mListeners) {
listener.onUpdate(mCounter - change, mCounter);
}
switchLabelAnimated(change);
}
return;
} else {
updateLabelVisibility(!mLabelOneVisible);
for (OnUpdateListener listener : mListeners) {
listener.onUpdate(mCounter - change, mCounter);
}
}
updateLabelVisibility(!mLabelOneVisible);
}

private void updateLabelVisibility(boolean labelOneVisible) {
Expand Down
2 changes: 1 addition & 1 deletion stepperwidget/src/main/res/drawable/bg_add.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:topRightRadius="10dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp" />
<solid android:color="#8c008c" />
<solid android:color="#FF4081" />
</shape>
</item>
<item>
Expand Down
2 changes: 1 addition & 1 deletion stepperwidget/src/main/res/drawable/bg_remove.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
android:topRightRadius="0dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp" />
<solid android:color="#8c008c" />
<solid android:color="#FF4081" />
</shape>
</item>
<item>
Expand Down
2 changes: 1 addition & 1 deletion stepperwidget/src/main/res/layout/stepper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
android:src="@drawable/ic_add"
android:background="@drawable/bg_add"/>
<FrameLayout android:layout_width="56dp"
android:layout_height="44dp">
android:layout_height="@dimen/stepper_widget_label_height">
<TextView
android:id="@+id/label1"
android:layout_width="48dp"
Expand Down
4 changes: 4 additions & 0 deletions stepperwidget/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="stepper_widget_label_height">44dp</dimen>
</resources>

0 comments on commit 7167f9c

Please sign in to comment.