-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSliderViewHorizontalIndicator.java
More file actions
139 lines (110 loc) · 3.29 KB
/
SliderViewHorizontalIndicator.java
File metadata and controls
139 lines (110 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package ru.firstpro.photoeditor;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import static ru.firstpro.photoeditor.SliderViewHorizontalIndicator.Anchor.left;
/**
* Created by ysoftware on 24.05.17.
*/
public class SliderViewHorizontalIndicator extends RelativeLayout {
public SliderViewHorizontalIndicator(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
foregroundView = new RelativeLayout(context);
addView(foregroundView);
}
/* VALUES */
public void setMaxValue(float v) {
maxValue = v;
if (minValue >= maxValue) { maxValue = minValue + 0.1f; }
reloadViews();
}
public void setMinValue(float v) {
minValue = v;
if (minValue >= maxValue) { minValue = maxValue - 0.1f; }
reloadViews();
}
public void setValue(float v) {
value = v;
if (value > maxValue) { value = maxValue; }
else if (value < minValue) { value = minValue; }
reloadViews();
}
public float getValue() {
return value;
}
public float getMinValue() {
return minValue;
}
public float getMaxValue() {
return maxValue;
}
public float getMiddleValue() {
return (minValue + maxValue) / 2;
}
/* SETTINGS */
enum Anchor { left, right, center }
public void setAnchor(Anchor a) {
anchor = a;
reloadViews();
}
public void setRelativePadding(float v) {
relativePadding = v;
if (relativePadding >= 1) { relativePadding = 0.99f; }
else if (relativePadding < 0) { relativePadding = 0f; }
reloadViews();
}
public void setForegroundColor(int c) {
foregroundColor = c;
foregroundView.setBackgroundColor(foregroundColor);
}
public Anchor getAnchor() {
return anchor;
}
public float getRelativePadding() {
return relativePadding;
}
public int getForegroundColor() {
return foregroundColor;
}
/* PRIVATE */
private float maxValue = 1f;
private float minValue = 0f;
private float value = 0.5f;
private float relativePadding = 0f;
private int foregroundColor = Color.parseColor("#FFF25F"); // a nice yellow color by default
private Anchor anchor = left;
private RelativeLayout foregroundView;
void reloadViews() {
foregroundView.setBackgroundColor(foregroundColor);
float dif = maxValue - minValue;
float width = getMeasuredWidth();
int height = getMeasuredHeight();
switch (anchor) {
case center: {
Boolean isPositive = getMiddleValue() - value < 0;
float relativeValue = getMiddleValue() - value;
float center = width / 2;
float indicatorWidth = center - (center * relativePadding);
float position = Math.abs(indicatorWidth / dif * 2 * relativeValue);
int _width = Math.round(position);
int _x = Math.round(isPositive ? center : center - position);
LayoutParams params = new LayoutParams(_width, height);
params.setMarginStart(_x);
foregroundView.setLayoutParams(params);
break;
}
case left:
case right: {
float indicatorWidth = width - (width * relativePadding);
float position = indicatorWidth * (value - minValue) / (maxValue - minValue);
int _width = Math.round(position);
int _x = anchor == left ? 0 : Math.round(width - position);
LayoutParams params = new LayoutParams(_width, height);
params.setMarginStart(_x);
foregroundView.setLayoutParams(params);
break;
}
}
}
}