1 package com.google.android.DemoKit; 2 3 import android.content.Context; 4 import android.content.res.Resources; 5 import android.graphics.Canvas; 6 import android.graphics.Rect; 7 import android.graphics.drawable.Drawable; 8 import android.util.AttributeSet; 9 import android.view.MotionEvent; 10 import android.view.View; 11 12 public class Slider extends View { 13 14 interface SliderPositionListener { onPositionChange(double value)15 void onPositionChange(double value); 16 } 17 18 private Drawable mIndicator; 19 private Drawable mBackground; 20 private double mPosition; 21 private SliderPositionListener mListener; 22 private boolean mVertical; 23 Slider(Context context)24 public Slider(Context context) { 25 super(context); 26 initSliderView(context, false); 27 } 28 Slider(Context context, AttributeSet attrs)29 public Slider(Context context, AttributeSet attrs) { 30 super(context, attrs); 31 initSliderView(context, false); 32 } 33 setSliderBackground(Drawable background)34 public void setSliderBackground(Drawable background) { 35 mBackground = background; 36 invalidate(); 37 } 38 setPositionListener(SliderPositionListener listener)39 public void setPositionListener(SliderPositionListener listener) { 40 mListener = listener; 41 } 42 setPosition(double position)43 public void setPosition(double position) { 44 if (mPosition != position) { 45 invalidate(); 46 mPosition = position; 47 if (mListener != null) { 48 mListener.onPositionChange(mPosition); 49 } 50 } 51 } 52 53 private OnTouchListener mClickListener = new OnTouchListener() { 54 public boolean onTouch(View v, MotionEvent m) { 55 Rect r = new Rect(); 56 getDrawingRect(r); 57 58 double position; 59 if (mVertical) { 60 double y = m.getY(); 61 position = Math.max(0, (r.bottom - y) / r.height()); 62 } else { 63 double x = m.getX(); 64 position = Math.max(0, (x - r.left) / r.width()); 65 } 66 position = Math.min(1, position); 67 setPosition(position); 68 return true; 69 } 70 }; 71 initSliderView(Context context, boolean vertical)72 protected void initSliderView(Context context, boolean vertical) { 73 mPosition = 0; 74 mVertical = vertical; 75 Resources res = context.getResources(); 76 if (mVertical) { 77 mBackground = res 78 .getDrawable(R.drawable.scrubber_vertical_blue_holo_dark); 79 } else { 80 mBackground = res 81 .getDrawable(R.drawable.scrubber_horizontal_holo_dark); 82 } 83 mIndicator = res.getDrawable(R.drawable.scrubber_control_holo_dark); 84 this.setOnTouchListener(mClickListener); 85 } 86 onDraw(Canvas canvas)87 protected void onDraw(Canvas canvas) { 88 Rect r = new Rect(); 89 getDrawingRect(r); 90 if (mVertical) { 91 int lineX = r.centerX(); 92 int bgW = mBackground.getIntrinsicWidth() / 2; 93 if (bgW == 0) { 94 bgW = 5; 95 } 96 mBackground.setBounds(lineX - bgW, r.top + 10, lineX + bgW, 97 r.bottom - 10); 98 mBackground.draw(canvas); 99 final int kMargin = 48; 100 int indicatorY = (int) (r.bottom - (r.height() - kMargin) 101 * mPosition) 102 - kMargin / 2; 103 Utilities.centerAround(lineX, indicatorY, mIndicator); 104 mIndicator.draw(canvas); 105 } else { 106 int lineY = r.centerY(); 107 int bgH = mBackground.getIntrinsicHeight() / 2; 108 if (bgH == 0) { 109 bgH = 5; 110 } 111 mBackground.setBounds(r.left + 10, lineY - bgH, r.right - 10, lineY 112 + bgH); 113 mBackground.draw(canvas); 114 final int kMargin = 48; 115 int indicatorX = (int) ((r.width() - kMargin) * mPosition) + r.left 116 + kMargin / 2; 117 Utilities.centerAround(indicatorX, lineY, mIndicator); 118 mIndicator.draw(canvas); 119 } 120 } 121 onMeasure(int widthMeasureSpec, int heightMeasureSpec)122 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 123 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 124 if (mVertical) { 125 setMeasuredDimension(mIndicator.getIntrinsicWidth(), 126 getMeasuredHeight()); 127 } else { 128 setMeasuredDimension(getMeasuredWidth(), 129 mIndicator.getIntrinsicHeight()); 130 } 131 } 132 133 } 134