-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSliderView.java
More file actions
162 lines (128 loc) · 4.02 KB
/
SliderView.java
File metadata and controls
162 lines (128 loc) · 4.02 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package ru.firstpro.photoeditor;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PointF;
import android.provider.CalendarContract;
import android.support.annotation.Nullable;
import android.support.v4.view.MotionEventCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RelativeLayout;
import static ru.firstpro.photoeditor.SliderViewHorizontalIndicator.Anchor.left;
/**
Created by ysoftware on 13.04.17.
*/
public class SliderView extends RelativeLayout {
interface Delegate {
void sliderViewNewValue(SliderView sliderView, float newValue);
void sliderViewWillChangeValue(SliderView sliderView, float oldValue);
void sliderViewDidChangeValue(SliderView sliderView, float newValue);
}
public SliderView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
setBackgroundColor(Color.TRANSPARENT);
}
/* PROPERTIES */
@Nullable Delegate delegate;
@Nullable public SliderViewHorizontalIndicator getIndicator() {
return indicator;
}
public void setIndicator(@Nullable SliderViewHorizontalIndicator indicator) {
this.indicator = indicator;
if (indicator != null) {
indicator.setMinValue(minValue);
indicator.setMaxValue(maxValue);
indicator.setValue(value);
indicator.reloadViews();
}
}
/* VALUES */
public void setValue(float v) {
value = v;
if (value > maxValue) { value = maxValue; }
else if (value < minValue) { value = minValue; }
if (indicator != null) indicator.setValue(value);
}
public void setMinValue(float v) {
minValue = v;
if (minValue >= maxValue) { minValue = maxValue - 0.1f; }
if (indicator != null) indicator.setMinValue(minValue);
}
public void setMaxValue(float v) {
this.maxValue = v;
if (minValue >= maxValue) { maxValue = minValue + 0.1f; }
if (indicator != null) indicator.setMaxValue(maxValue);
}
public float getValue() {
return value;
}
public float getMaxValue() {
return maxValue;
}
public float getMinValue() {
return minValue;
}
public float getMiddleValue() {
return (minValue + maxValue) / 2;
}
public void setMiddleValue() { setValue(getMiddleValue()); }
/* SETTINGS */
public Boolean isEnabled = true;
public void setSensitivity(float s) {
this.sensitivity = s;
if (sensitivity <= 0) { sensitivity = 0.01f; }
}
public void setResolution(float r) {
this.resolution = r;
if (resolution > 0) { resolution = 0f; }
}
public float getResolution() {
return resolution;
}
public float getSensitivity() {
return sensitivity;
}
/* PRIVATE */
private float value = 0.5f;
private float minValue = 0f;
private float maxValue = 1f;
private float resolution = 0f;
private float sensitivity = 1.5f;
private Boolean isChangingValue = false;
private PointF touchDownPosition;
private @Nullable SliderViewHorizontalIndicator indicator;
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = MotionEventCompat.getActionMasked(event);
if (!isEnabled) { return false; }
switch(action) {
case MotionEvent.ACTION_DOWN:
if (!isChangingValue) {
if (delegate != null) delegate.sliderViewWillChangeValue(this, value);
touchDownPosition = new PointF(event.getX(), event.getY());
isChangingValue = true;
}
return true;
case MotionEvent.ACTION_MOVE:
float translation = (event.getX() - touchDownPosition.x) -
(event.getY() - touchDownPosition.y);
if (Math.abs(translation) >= resolution) {
float dif = maxValue - minValue;
float size = Math.max(getMeasuredWidth(), getMeasuredHeight());
float panSize = size / sensitivity;
float adaptation = panSize / dif;
setValue(value += translation / adaptation);
if (delegate != null) delegate.sliderViewNewValue(this, value);
touchDownPosition = new PointF(event.getX(), event.getY());
}
return true;
default:
if (isChangingValue) {
isChangingValue = false;
if (delegate != null) delegate.sliderViewDidChangeValue(this, value);
}
return true;
}
}
}