1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.example.rscamera;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.util.AttributeSet;
22import android.view.MotionEvent;
23import android.widget.SeekBar;
24
25/**
26 * Class to create a vertical slider
27 */
28public class VerticalSeekBar extends SeekBar {
29
30    public VerticalSeekBar(Context context) {
31        super(context);
32    }
33
34    public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
35        super(context, attrs, defStyle);
36    }
37
38    public VerticalSeekBar(Context context, AttributeSet attrs) {
39        super(context, attrs);
40    }
41
42    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
43        super.onSizeChanged(h, w, oldh, oldw);
44    }
45
46    @Override
47    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
48        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
49        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
50    }
51
52    protected void onDraw(Canvas c) {
53        c.rotate(-90);
54        c.translate(-getHeight(), 0);
55
56        super.onDraw(c);
57    }
58
59    @Override
60    public boolean onTouchEvent(MotionEvent event) {
61        if (!isEnabled()) {
62            return false;
63        }
64
65        switch (event.getAction()) {
66            case MotionEvent.ACTION_DOWN:
67            case MotionEvent.ACTION_MOVE:
68            case MotionEvent.ACTION_UP:
69                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
70                onSizeChanged(getWidth(), getHeight(), 0, 0);
71                break;
72
73            case MotionEvent.ACTION_CANCEL:
74                break;
75        }
76        return true;
77    }
78}