Statistics
| Branch: | Tag: | Revision:

securesmartcam / app / src / main / java / org / witness / obscuracam / video / InOutPlayheadSeekBar.java @ 41590feb

History | View | Annotate | Download (5.06 KB)

1
/*
2
 * Thanks to: http://www.quicknews4you.com/2011/05/08/android-seek-bar-with-two-thumb/
3
 * for the sample code, much of which as been incorporated
4
 */
5

    
6
package org.witness.obscuracam.video;
7

    
8

    
9
import org.witness.obscuracam.ObscuraApp;
10
import org.witness.sscphase1.R;
11

    
12
import android.content.Context;
13
import android.graphics.Bitmap;
14
import android.graphics.BitmapFactory;
15
import android.graphics.Canvas;
16
import android.graphics.Paint;
17
import android.util.AttributeSet;
18
import android.view.MotionEvent;
19
import android.widget.SeekBar;
20

    
21
public class InOutPlayheadSeekBar extends SeekBar {
22

    
23
        private String LOGTAG = ObscuraApp.TAG;
24
        
25
        public static final int NONE = 0;
26
        public static final int THUMBIN = 1;
27
        public static final int THUMBOUT = 2;
28
        
29
        private int selectedThumb = NONE;
30
        
31
        private Bitmap thumbIn = BitmapFactory.decodeResource(getResources(), R.drawable.leftthumb);
32
        private Bitmap thumbOut = BitmapFactory.decodeResource(getResources(), R.drawable.rightthumb);
33
        
34
        private int thumbInX, thumbOutX;
35
        private int thumbInY, thumbOutY;
36
        private int thumbInValue, thumbOutValue;
37
        
38
        private int thumbInHalfWidth;
39
        private int thumbOutHalfWidth;
40
        
41
        private Paint paint = new Paint();
42
        
43
        private InOutPlayheadSeekBarChangeListener changeListener;
44
        
45
        public boolean thumbsActive = false;
46
        
47
        public void setThumbsActive(int inThumbValue, int outThumbValue) {
48
                //Log.v(LOGTAG,"in value: " + inThumbValue + " out value: " + outThumbValue);
49
                thumbsActive = true;
50
                setThumbsValue(inThumbValue, outThumbValue);
51
                invalidate();
52
                
53
        }
54
        
55
        public void setThumbsInactive() {
56
                thumbsActive = false;
57
        }
58
        
59
        public InOutPlayheadSeekBar(Context context) {
60
                super(context);
61
        }
62
        
63
        public InOutPlayheadSeekBar(Context context, AttributeSet attrs) {
64
                super(context, attrs);
65
        }
66
        
67
        public InOutPlayheadSeekBar(Context context, AttributeSet attrs, int defStyle) {
68
                super(context, attrs, defStyle);
69
        }
70
        
71
        @Override
72
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
73
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
74
                if (getHeight() > 0) {
75
                        init();
76
                }
77
        }
78

    
79
        private void init() 
80
        {
81
                /*
82
                if (thumbIn.getHeight() > getHeight()) {
83
                        getLayoutParams().height = thumbIn.getHeight();                
84
                }
85

86
                if (thumbOut.getHeight() > getHeight()) {
87
                        getLayoutParams().height = thumbOut.getHeight();                
88
                }
89
                */
90
                
91
                thumbInY = (getHeight() / 2) - (thumbIn.getHeight() / 2);
92
                thumbOutY = (getHeight() / 2) - (thumbOut.getHeight() / 2);
93
                
94
                thumbInHalfWidth = thumbIn.getWidth()/2;
95
                thumbInX = 0;
96
                
97
                thumbOutHalfWidth = thumbOut.getWidth()/2;
98
                thumbOutX = getWidth()-thumbOutHalfWidth;
99
                
100
                invalidate();
101
        }
102
        
103
        public void setInOutPlayheadSeekBarChangeListener(InOutPlayheadSeekBarChangeListener changeListener){
104
                this.changeListener = changeListener;
105
        }
106

    
107
        @Override
108
        protected void onDraw(Canvas canvas) {
109
                super.onDraw(canvas);
110
                if (thumbsActive) {
111
                        canvas.drawBitmap(thumbIn, thumbInX - thumbInHalfWidth, thumbInY, paint);
112
                        canvas.drawBitmap(thumbOut, thumbOutX - thumbOutHalfWidth, thumbOutY, paint);
113
                }
114
        }
115

    
116
        @Override
117
        public boolean onTouchEvent(MotionEvent event) {
118
                boolean handled = false;
119
                
120
                int mx = (int) event.getX();
121
                switch (event.getAction()) {
122
                case MotionEvent.ACTION_DOWN:
123
                        if (mx >= thumbInX - thumbInHalfWidth && mx <= thumbInX + thumbInHalfWidth) {
124
                                selectedThumb = THUMBIN;
125
                        //        Log.i(LOGTAG,"Select thumbIn");
126
                                handled = true;
127
                        } else if (mx >= thumbOutX - thumbOutHalfWidth && mx <= thumbOutX + thumbOutHalfWidth) {
128
                                selectedThumb = THUMBOUT;
129
                        //        Log.i(LOGTAG,"Select thumbOut");
130
                                handled = true;
131
                        }
132
                        break;
133
                case MotionEvent.ACTION_MOVE:
134
                //        Log.i(LOGTAG,"Mouse Move : " + selectedThumb);
135

    
136
                        if (selectedThumb == THUMBIN) {
137
                                thumbInX = mx;
138
                        //        Log.i(LOGTAG,"Move thumbIn");
139
                                handled = true;
140
                        } else if (selectedThumb == THUMBOUT) {
141
                                thumbOutX = mx;
142
                //                Log.i(LOGTAG,"Move thumbOut");
143
                                handled = true;
144
                        }
145
                        break;
146
                case MotionEvent.ACTION_UP:
147
                        selectedThumb = NONE;
148
                        handled = true;
149
                        break;
150
                }
151

    
152
                // Some constraints
153
                if (thumbInX < 0) {
154
                        thumbInX = 0;
155
                }
156
                
157
                if (thumbOutX < thumbInX) {
158
                        thumbOutX = thumbInX;
159
                }
160
                        
161
                if (thumbOutX > getWidth()) {
162
                        thumbOutX = getWidth();
163
                }
164
                
165
                if (thumbInX > thumbOutX) {
166
                        thumbInX = thumbOutX;
167
                }
168
                
169
                invalidate();
170
                
171
                if (changeListener != null) {
172
                        calculateThumbsValue();
173
                        changeListener.inOutValuesChanged(thumbInValue,thumbOutValue);
174
                }
175
                
176
                if (!handled) {
177
                        super.onTouchEvent(event);
178
                }
179
                
180
                return true;
181
        }
182
        
183
        private void calculateThumbsValue(){
184
                thumbInValue = (int)((100*((float)thumbInX))/((float)getWidth()));
185
                thumbOutValue = (int)((100*((float)thumbOutX))/((float)getWidth()));
186
        //        Log.v(LOGTAG,"thumb in value: " + thumbInValue + " thumb out value: " + thumbOutValue);
187
        }
188
        
189
        private void setThumbsValue(int thumbInValue, int thumbOutValue) {
190
                thumbInX = (int)(((float)thumbInValue/(float)100)*(float)getWidth());
191
                thumbOutX = (int)(((float)thumbOutValue/(float)100)*(float)getWidth());
192
        //        Log.v(LOGTAG,"thumbInX: " + thumbInX + " thumbOutX: " + thumbOutX);
193
                calculateThumbsValue();
194
        }
195
                
196
        interface InOutPlayheadSeekBarChangeListener {
197
                void inOutValuesChanged(int thumbInValue,int thumbOutValue);
198
        }        
199
}