From d31a2ecb959be08cca9f65408c993a0308094f91 Mon Sep 17 00:00:00 2001 From: Vinicius Sossella Date: Wed, 10 Jan 2018 16:02:44 -0200 Subject: [PATCH 1/3] handle regra max value --- .../verticalseekbarlib/VerticalSeekBar.java | 59 +++++++++++++------ .../src/main/res/values/attrs.xml | 1 + 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java b/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java index 6040e7d..a9e14e5 100644 --- a/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java +++ b/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java @@ -20,7 +20,7 @@ public class VerticalSeekBar extends RelativeLayout { View layoutView, verticalSeekBarBackground, verticalSeekBarThumb; VerticalSeekBarListener listener; float dY = 0; - int marginTop = 0, valor = 0, step = 0, calculatedValue = 0; + int marginTop = 0, value = 0, defaultValue = 500, defaultStep = 25, maxValue = 0, step = 0, calculatedValue = 0; public VerticalSeekBar(Context context) { super(context); @@ -49,15 +49,20 @@ private void loadCustomAttrs(Context context, AttributeSet attrs) { loadThumbAttr(attributes); loadValueAttr(attributes); loadStepAttr(attributes); + loadMaxValueAttr(attributes); setupBackgroundMarginTopAttr(attributes); } private void loadValueAttr(TypedArray attributes) { - valor = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_value, 500); + value = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_value, defaultValue); } private void loadStepAttr(TypedArray attributes) { - step = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_step, 25); + step = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_step, defaultStep); + } + + private void loadMaxValueAttr(TypedArray attributes) { + maxValue = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_max_value, 0); } private void loadBackgroundAttr(TypedArray attributes) { @@ -84,7 +89,7 @@ private void addThumbTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { - int interations = valor / step; + int interations = value / step; int heightArea = getHeight() - marginTop; int pixelNumberToInteraction = heightArea / interations; @@ -97,14 +102,20 @@ public boolean onTouch(View view, MotionEvent event) { case MotionEvent.ACTION_MOVE: { float wantedY = event.getRawY() + dY; int multiplier = (int) wantedY / pixelNumberToInteraction; - calculatedValue = valor - (multiplier * step); - - handleIfHitTop(wantedY); - handleIfHitBottom(heightArea, wantedY); - handleIfIsBetweenTopAndBottom(heightArea, wantedY); - - if (listener != null) - listener.onValueChanged(calculatedValue); + calculatedValue = value - (multiplier * step); + int interationsToGetMaxValue = maxValue / step; + float maxValueY = interationsToGetMaxValue * pixelNumberToInteraction; + + if (wantedY <= maxValueY || maxValueY == 0) { + handleIfHitTop(wantedY); + handleIfHitBottom(heightArea, wantedY); + handleIfIsBetweenTopAndBottom(heightArea, wantedY); + + callOnValueChanged(calculatedValue); + } else { + callOnValueChanged(maxValue); + setYPosition(maxValueY, maxValueY - marginTop); + } break; } default: @@ -115,15 +126,14 @@ public boolean onTouch(View view, MotionEvent event) { }); } - public void setVerticalSeekBarListener(VerticalSeekBarListener verticalSeekBarListener) { - listener = verticalSeekBarListener; + private void callOnValueChanged(float value) { + if (listener != null) + listener.onValueChanged(value); } private void handleIfIsBetweenTopAndBottom(int heightArea, float wantedY) { if (wantedY > marginTop && wantedY <= heightArea) { setYPosition(wantedY, wantedY - marginTop); - verticalSeekBarBackground.setY(wantedY); - verticalSeekBarThumb.setY(wantedY - marginTop); } } @@ -137,7 +147,7 @@ private void handleIfHitBottom(int heightArea, float wantedY) { private void handleIfHitTop(float wantedY) { if (wantedY < marginTop) { setYPosition(marginTop, 0); - calculatedValue = valor; + calculatedValue = value; } } @@ -146,4 +156,19 @@ private void setYPosition(float yBackground, float yThumb) { verticalSeekBarThumb.setY(yThumb); } + public void setVerticalSeekBarListener(VerticalSeekBarListener verticalSeekBarListener) { + listener = verticalSeekBarListener; + } + + public void setValue(int value) { + this.value = value; + } + + public void setMaxValue(int maxValue) { + this.maxValue = maxValue; + } + + public void setStep(int step) { + this.step = step; + } } diff --git a/verticalseekbarlib/src/main/res/values/attrs.xml b/verticalseekbarlib/src/main/res/values/attrs.xml index 5327cb9..0f9a342 100644 --- a/verticalseekbarlib/src/main/res/values/attrs.xml +++ b/verticalseekbarlib/src/main/res/values/attrs.xml @@ -6,6 +6,7 @@ + \ No newline at end of file From 3b55539e7161c6761f427beac22cedb256ff94fe Mon Sep 17 00:00:00 2001 From: Vinicius Sossella Date: Wed, 10 Jan 2018 17:47:50 -0200 Subject: [PATCH 2/3] properties and consts --- .../com/verticalseekbarlib/VerticalSeekBar.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java b/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java index a9e14e5..da94945 100644 --- a/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java +++ b/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java @@ -17,10 +17,18 @@ public class VerticalSeekBar extends RelativeLayout { LayoutInflater inflater; - View layoutView, verticalSeekBarBackground, verticalSeekBarThumb; + View layoutView; + View verticalSeekBarBackground; + View verticalSeekBarThumb; VerticalSeekBarListener listener; float dY = 0; - int marginTop = 0, value = 0, defaultValue = 500, defaultStep = 25, maxValue = 0, step = 0, calculatedValue = 0; + int marginTop = 0; + int value = 0; + int maxValue = 0; + int step = 0; + int calculatedValue = 0; + final int defaultValue = 500; + final int defaultStep = 25; public VerticalSeekBar(Context context) { super(context); From a891c6dbc3f8b70607bf4856c44539a077e0cf40 Mon Sep 17 00:00:00 2001 From: Vinicius Sossella Date: Wed, 10 Jan 2018 17:49:26 -0200 Subject: [PATCH 3/3] CONST --- .../opencraft/com/verticalseekbarlib/VerticalSeekBar.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java b/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java index da94945..d30e1a1 100644 --- a/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java +++ b/verticalseekbarlib/src/main/java/opencraft/com/verticalseekbarlib/VerticalSeekBar.java @@ -27,8 +27,8 @@ public class VerticalSeekBar extends RelativeLayout { int maxValue = 0; int step = 0; int calculatedValue = 0; - final int defaultValue = 500; - final int defaultStep = 25; + final int DEFAULT_VALUE = 500; + final int DEFAULT_STEP = 25; public VerticalSeekBar(Context context) { super(context); @@ -62,11 +62,11 @@ private void loadCustomAttrs(Context context, AttributeSet attrs) { } private void loadValueAttr(TypedArray attributes) { - value = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_value, defaultValue); + value = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_value, DEFAULT_VALUE); } private void loadStepAttr(TypedArray attributes) { - step = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_step, defaultStep); + step = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_step, DEFAULT_STEP); } private void loadMaxValueAttr(TypedArray attributes) {