From 702f658165b9141d6624fef59b5a51a9deaf8b0a Mon Sep 17 00:00:00 2001 From: Vinicius Sossella Date: Fri, 12 Jan 2018 08:03:34 -0200 Subject: [PATCH] fixes --- app/src/main/res/layout/activity_main.xml | 5 +-- .../verticalseekbarlib/VerticalSeekBar.java | 31 +++++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0ff983a..db2b54b 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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" /> diff --git a/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java b/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java index bd77739..2faa369 100644 --- a/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java +++ b/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java @@ -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); @@ -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: @@ -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 { @@ -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; }