Skip to content

Commit

Permalink
Merge pull request #2 from OpenCraft/feature-regra
Browse files Browse the repository at this point in the history
Feature regra
  • Loading branch information
cleberhenriques authored Jan 10, 2018
2 parents bec3aed + a891c6d commit 5bbce6c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, valor = 0, step = 0, calculatedValue = 0;
int marginTop = 0;
int value = 0;
int maxValue = 0;
int step = 0;
int calculatedValue = 0;
final int DEFAULT_VALUE = 500;
final int DEFAULT_STEP = 25;

public VerticalSeekBar(Context context) {
super(context);
Expand Down Expand Up @@ -49,15 +57,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, DEFAULT_VALUE);
}

private void loadStepAttr(TypedArray attributes) {
step = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_step, 25);
step = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_step, DEFAULT_STEP);
}

private void loadMaxValueAttr(TypedArray attributes) {
maxValue = attributes.getInteger(R.styleable.VerticalSeekBar_seekbar_max_value, 0);
}

private void loadBackgroundAttr(TypedArray attributes) {
Expand All @@ -84,7 +97,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;

Expand All @@ -97,14 +110,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:
Expand All @@ -115,15 +134,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);
}
}

Expand All @@ -137,7 +155,7 @@ private void handleIfHitBottom(int heightArea, float wantedY) {
private void handleIfHitTop(float wantedY) {
if (wantedY < marginTop) {
setYPosition(marginTop, 0);
calculatedValue = valor;
calculatedValue = value;
}
}

Expand All @@ -146,4 +164,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;
}
}
1 change: 1 addition & 0 deletions verticalseekbarlib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<attr name="seekbar_thumb" format="reference" />
<attr name="seekbar_step" format="integer" />
<attr name="seekbar_value" format="integer" />
<attr name="seekbar_max_value" format="integer" />
</declare-styleable>

</resources>

0 comments on commit 5bbce6c

Please sign in to comment.