summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/views/SeekBarTicks.java
blob: 88e8e1642efbff36ff1308f6c17f503096deff08 (plain)
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
package de.blinkt.openvpn.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewConfiguration;
import android.widget.SeekBar;

public class SeekBarTicks extends SeekBar {
    private Paint mTickPaint;
    private float mTickHeight;

    private float tickHeightRatio = 0.6f;

    public SeekBarTicks(Context context, AttributeSet attrs) {
        super (context, attrs);

        initTicks (context, attrs, android.R.attr.seekBarStyle);
    }


    public SeekBarTicks(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        initTicks (context, attrs, defStyle);

        /*mTickHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                tickHeightDP,
                ctx.getResources().getDisplayMetrics()); */
    }

    private void initTicks(Context context, AttributeSet attrs, int defStyle) {
        TypedArray a = context.obtainStyledAttributes(attrs,
                new int[] { android.R.attr.secondaryProgress }, defStyle, 0);


        int tickColor = a.getColor(0, android.R.color.black);
        mTickPaint = new Paint();
        mTickPaint.setColor( context.getResources().getColor(tickColor));
        a.recycle();
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {
        drawTicks(canvas);
        super.onDraw(canvas);
    }

    private void drawTicks(Canvas canvas) {

        final int available = getWidth() - getPaddingLeft() - getPaddingRight();
        final int availableHeight = getHeight() - getPaddingBottom() - getPaddingTop();

        int extrapadding = (int) ((availableHeight- (availableHeight * tickHeightRatio))/2);

        int tickSpacing = available / (getMax() );

        for (int i = 1; i < getMax(); i++) {
            final float x = getPaddingLeft() + i * tickSpacing;

            canvas.drawLine(x, getPaddingTop()+extrapadding, x, getHeight()-getPaddingBottom()-extrapadding, mTickPaint);
        }
    }
}