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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| package com.tw.customviews.yuanhuan;
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Shader; import android.graphics.SweepGradient; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import com.tw.customviews.R;
public class CircleSeekBar extends View {
private Paint mPaint; private int mProgress = 0; private int mMax = 80; private int mStartAngle = 270; private int mMaxAngle = 270;//默认360 private int mStrokeWidth = 10; private int mBackgroundColor = Color.GRAY; private int mProgressColor = Color.GREEN;
public CircleSeekBar(Context context) { this(context, null); }
public CircleSeekBar(Context context, AttributeSet attrs) { this(context, attrs, 0); }
public CircleSeekBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr);
// 初始化画笔 mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeCap(Paint.Cap.ROUND);
/** * 获取自定义属性 * max:最大进度值,默认为100。 * progress:当前进度值,默认为0。 * startAngle:起始角度,默认为270度(即12点钟方向)。 * strokeWidth:进度条宽度,默认为10dp。 * backgroundColor:背景颜色,默认为灰色。 * progressColor:进度条颜色,默认为蓝色。 **/ TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleSeekBar); mMax = ta.getInt(R.styleable.CircleSeekBar_max, 100); mProgress = ta.getInt(R.styleable.CircleSeekBar_progress, 0); mStartAngle = ta.getInt(R.styleable.CircleSeekBar_startAngle, 270); mStartAngle = ta.getInt(R.styleable.CircleSeekBar_maxAngle, 360); mStrokeWidth = ta.getDimensionPixelSize(R.styleable.CircleSeekBar_strokeWidth, 10); mBackgroundColor = ta.getColor(R.styleable.CircleSeekBar_backgroundColor, Color.GRAY); mProgressColor = ta.getColor(R.styleable.CircleSeekBar_progressColor, Color.GREEN); ta.recycle(); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);
// 绘制背景圆环 mPaint.setShader(null); mPaint.setColor(mBackgroundColor); mPaint.setStrokeWidth(mStrokeWidth); canvas.drawArc(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom(), mStartAngle, 360, false, mPaint);
// 绘制进度条 int[] colors = {Color.RED, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE, Color.MAGENTA, Color.RED}; float[] positions = {0, 0.15f, 0.3f, 0.45f, 0.6f, 0.75f, 1}; Shader shader = new SweepGradient(getWidth() / 2, getHeight() / 2, colors, positions); mPaint.setShader(shader); mPaint.setColor(mProgressColor); float sweepAngle = (float) mProgress / mMax * mMaxAngle; canvas.drawArc(getPaddingLeft(), getPaddingTop(), getWidth() - getPaddingRight(), getHeight() - getPaddingBottom(), mStartAngle, sweepAngle, false, mPaint);
}
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: // 计算当前进度值 float x = event.getX() - getWidth() / 2; float y = event.getY() - getHeight() / 2; double angle = Math.toDegrees(Math.atan2(y, x)); angle = angle < 0 ? angle + 360 : angle; mProgress = (int) (angle / 360 * mMax); invalidate(); return true; }
return super.onTouchEvent(event); }
public void setMax(int max) { mMax = max; invalidate(); }
public void setProgress(int progress) { mProgress = progress; invalidate(); }
public void setStartAngle(int startAngle) { mStartAngle = startAngle; invalidate(); } public void setMaxAngle(int maxAngle) { mMaxAngle = maxAngle; invalidate(); }
public void setStrokeWidth(int strokeWidth) { mStrokeWidth = strokeWidth; invalidate(); }
public void setBackgroundColor(int backgroundColor) { mBackgroundColor = backgroundColor; invalidate(); }
public void setProgressColor(int progressColor) { mProgressColor = progressColor; invalidate(); } }
|