Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinicius Sossella committed Jan 12, 2018
1 parent 9a0b9ad commit 702f658
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
5 changes: 3 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:background="@color/colorAccent" />
android:background="@color/colorPrimary" />

<TextView
android:id="@+id/text_top"
Expand All @@ -26,9 +26,10 @@
android:layout_height="match_parent"
android:layout_above="@+id/bottom"
android:layout_below="@+id/top"
app:seekbar_backgroundColor="@color/colorPrimary"
app:seekbar_backgroundColor="@color/colorPrimaryDark"
app:seekbar_background_margin_top="25dp"
app:seekbar_thumb="@drawable/check_on"
app:seekbar_max_value="250"
app:seekbar_value="500"
app:seekbar_step="25"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class VerticalSeekBar extends RelativeLayout {
int calculatedValue = 0;
final int DEFAULT_VALUE = 500;
final int DEFAULT_STEP = 25;
boolean hitBottom = false;
float hitBottomY = 0;

public VerticalSeekBar(Context context) {
super(context);
Expand Down Expand Up @@ -96,10 +98,9 @@ private void addThumbTouchListener() {
verticalSeekBarThumb.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {

int interations = value / step;
int heightArea = getHeight() - marginTop;
int pixelNumberToInteraction = heightArea / interations;
int heightAreaLessMarginTop = getHeight() - marginTop;
int pixelNumberToInteractionWithoutMargin = getHeight() / interations;

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Expand All @@ -109,15 +110,25 @@ public boolean onTouch(View view, MotionEvent event) {
}
case MotionEvent.ACTION_MOVE: {
float wantedY = event.getRawY() + dY + verticalSeekBarThumb.getHeight() / 2;
int multiplier = (int) wantedY / pixelNumberToInteraction;
float wantedYForBottom;
int multiplier;

if (hitBottom) {
wantedYForBottom = wantedY + marginTop;
multiplier = (int) wantedYForBottom / pixelNumberToInteractionWithoutMargin;
hitBottom = hitBottomY < wantedY;
} else {
multiplier = (int) wantedY / pixelNumberToInteractionWithoutMargin;
}

calculatedValue = value - (multiplier * step);
int interationsToGetMaxValue = maxValue / step;
float maxValueY = interationsToGetMaxValue * pixelNumberToInteraction;
float maxValueY = interationsToGetMaxValue * pixelNumberToInteractionWithoutMargin;

if (wantedY <= maxValueY || maxValueY == 0) {
handleIfHitTop(wantedY);
handleIfHitBottom(heightArea, wantedY);
handleIfIsBetweenTopAndBottom(heightArea, wantedY);
handleIfHitBottom(heightAreaLessMarginTop, wantedY);
handleIfIsBetweenTopAndBottom(heightAreaLessMarginTop, wantedY);

callOnValueChanged(calculatedValue);
} else {
Expand Down Expand Up @@ -146,14 +157,16 @@ private void handleIfIsBetweenTopAndBottom(int heightArea, float wantedY) {
}

private void handleIfHitBottom(int heightArea, float wantedY) {
if (wantedY > heightArea) {
if (wantedY - marginTop > heightArea) {
hitBottom = true;
hitBottomY = heightArea - marginTop;
setYPosition(heightArea, heightArea - marginTop);
calculatedValue = 0;
}
}

private void handleIfHitTop(float wantedY) {
if (wantedY < marginTop) {
if (wantedY + marginTop < marginTop) {
setYPosition(marginTop, 0);
calculatedValue = value;
}
Expand Down

0 comments on commit 702f658

Please sign in to comment.