1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package net.sourceforge.subsonic.androidapp.util; |
20 |
|
|
21 |
|
import android.content.Context; |
22 |
|
import android.graphics.Bitmap; |
23 |
|
import android.graphics.BitmapFactory; |
24 |
|
import android.graphics.Canvas; |
25 |
|
import android.util.AttributeSet; |
26 |
|
import android.view.MotionEvent; |
27 |
|
import android.view.View; |
28 |
|
import android.widget.ProgressBar; |
29 |
|
import net.sourceforge.subsonic.androidapp.R; |
30 |
|
|
31 |
|
|
32 |
|
@author |
33 |
|
@version |
34 |
|
|
|
|
| 87,8% |
Uncovered Elements: 9 (74) |
Complexity: 19 |
Complexity Density: 0,4 |
|
35 |
|
public class HorizontalSlider extends ProgressBar { |
36 |
|
|
37 |
|
private final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.slider_knob); |
38 |
|
private boolean slidingEnabled; |
39 |
|
private OnSliderChangeListener listener; |
40 |
|
private static final int PADDING = 2; |
41 |
|
private boolean sliding; |
42 |
|
private int sliderPosition; |
43 |
|
private int startPosition; |
44 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
45 |
|
public interface OnSliderChangeListener { |
46 |
|
void onSliderChanged(View view, int position, boolean inProgress); |
47 |
|
} |
48 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
49 |
0
|
public HorizontalSlider(Context context, AttributeSet attrs, int defStyle) {... |
50 |
0
|
super(context, attrs, defStyle); |
51 |
|
} |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
53 |
6
|
public HorizontalSlider(Context context, AttributeSet attrs) {... |
54 |
6
|
super(context, attrs, android.R.attr.progressBarStyleHorizontal); |
55 |
|
} |
56 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
57 |
0
|
public HorizontalSlider(Context context) {... |
58 |
0
|
super(context); |
59 |
|
} |
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0,67 |
|
61 |
216
|
public void setSlidingEnabled(boolean slidingEnabled) {... |
62 |
216
|
if (this.slidingEnabled != slidingEnabled) { |
63 |
10
|
this.slidingEnabled = slidingEnabled; |
64 |
10
|
invalidate(); |
65 |
|
} |
66 |
|
} |
67 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
68 |
0
|
public boolean isSlidingEnabled() {... |
69 |
0
|
return slidingEnabled; |
70 |
|
} |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
6
|
public void setOnSliderChangeListener(OnSliderChangeListener listener) {... |
73 |
6
|
this.listener = listener; |
74 |
|
} |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 4 |
Complexity Density: 0,22 |
|
76 |
204
|
@Override... |
77 |
|
protected void onDraw(Canvas canvas) { |
78 |
204
|
super.onDraw(canvas); |
79 |
|
|
80 |
204
|
int max = getMax(); |
81 |
204
|
if (!slidingEnabled || max == 0) { |
82 |
66
|
return; |
83 |
|
} |
84 |
|
|
85 |
138
|
int paddingLeft = getPaddingLeft(); |
86 |
138
|
int paddingRight = getPaddingRight(); |
87 |
138
|
int paddingTop = getPaddingTop(); |
88 |
138
|
int paddingBottom = getPaddingBottom(); |
89 |
|
|
90 |
138
|
int w = getWidth() - paddingLeft - paddingRight; |
91 |
138
|
int h = getHeight() - paddingTop - paddingBottom; |
92 |
138
|
int position = sliding ? sliderPosition : getProgress(); |
93 |
|
|
94 |
138
|
int bitmapWidth = bitmap.getWidth(); |
95 |
138
|
int bitmapHeight = bitmap.getWidth(); |
96 |
138
|
float x = paddingLeft + w * ((float) position / max) - bitmapWidth / 2.0F; |
97 |
138
|
x = Math.max(x, paddingLeft); |
98 |
138
|
x = Math.min(x, paddingLeft + w - bitmapWidth); |
99 |
138
|
float y = paddingTop + h / 2.0F - bitmapHeight / 2.0F; |
100 |
|
|
101 |
138
|
canvas.drawBitmap(bitmap, x, y, null); |
102 |
|
} |
103 |
|
|
|
|
| 91,2% |
Uncovered Elements: 3 (34) |
Complexity: 8 |
Complexity Density: 0,36 |
|
104 |
63
|
@Override... |
105 |
|
public boolean onTouchEvent(MotionEvent event) { |
106 |
63
|
if (!slidingEnabled) { |
107 |
3
|
return false; |
108 |
|
} |
109 |
|
|
110 |
60
|
int action = event.getAction(); |
111 |
|
|
112 |
60
|
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) { |
113 |
|
|
114 |
56
|
if (action == MotionEvent.ACTION_DOWN) { |
115 |
4
|
sliding = true; |
116 |
4
|
startPosition = getProgress(); |
117 |
|
} |
118 |
|
|
119 |
56
|
float x = event.getX() - PADDING; |
120 |
56
|
float width = getWidth() - 2 * PADDING; |
121 |
56
|
sliderPosition = Math.round((float) getMax() * (x / width)); |
122 |
56
|
sliderPosition = Math.max(sliderPosition, 0); |
123 |
|
|
124 |
56
|
setProgress(Math.min(startPosition, sliderPosition)); |
125 |
56
|
setSecondaryProgress(Math.max(startPosition, sliderPosition)); |
126 |
56
|
if (listener != null) { |
127 |
56
|
listener.onSliderChanged(this, sliderPosition, true); |
128 |
|
} |
129 |
|
|
130 |
4
|
} else if (action == MotionEvent.ACTION_UP) { |
131 |
4
|
sliding = false; |
132 |
4
|
setProgress(sliderPosition); |
133 |
4
|
setSecondaryProgress(0); |
134 |
4
|
if (listener != null) { |
135 |
4
|
listener.onSliderChanged(this, sliderPosition, false); |
136 |
|
} |
137 |
|
} |
138 |
|
|
139 |
60
|
return true; |
140 |
|
} |
141 |
|
} |