初始化一些參數
繪製刻度盤
繪製刻度盤下的圓弧
繪製標題與溫度標識
繪製旋轉按鈕
繪製溫度
處理滑動事件
提供一些接口方法
3.實現初始化一些參數public class TempControlView extends View {
// 控制項寬 private int width; // 控制項高 private int height; // 刻度盤半徑 private int dialRadius; // 圓弧半徑 private int arcRadius; // 刻度高 private int scaleHeight = dp2px(10); // 刻度盤畫筆 private Paint dialPaint; // 圓弧畫筆 private Paint arcPaint; // 標題畫筆 private Paint titlePaint; // 溫度標識畫筆 private Paint tempFlagPaint; // 旋轉按鈕畫筆 private Paint buttonPaint; // 溫度顯示畫筆 private Paint tempPaint; // 文本提示 private String title = "最高溫度設置"; // 溫度 private int temperature; // 最低溫度 private int minTemp = 15; // 最高溫度 private int maxTemp = 30; // 四格(每格4.5度,共18度)代表溫度1度 private int angleRate = 4; // 按鈕圖片 private Bitmap buttonImage = BitmapFactory.decodeResource(getResources(), R.mipmap.btn_rotate); // 按鈕圖片陰影 private Bitmap buttonImageShadow = BitmapFactory.decodeResource(getResources(), R.mipmap.btn_rotate_shadow); // 抗鋸齒 private PaintFlagsDrawFilter paintFlagsDrawFilter; // 溫度改變監聽 private OnTempChangeListener onTempChangeListener;
// 以下為旋轉按鈕相關
// 當前按鈕旋轉的角度 private float rotateAngle; // 當前的角度 private float currentAngle;
...
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // 控制項寬、高 width = height = Math.min(h, w); // 刻度盤半徑 dialRadius = width / 2 - dp2px(20); // 圓弧半徑 arcRadius = dialRadius - dp2px(20); }
...}繪製刻度盤以屏幕中心為畫布原點,圓弧角度為270°,繪製未選中與選中狀態的刻度盤。
旋轉方法中多減的2°是後期調整所得,不用在意。private void drawScale(Canvas canvas) { canvas.save(); canvas.translate(getWidth() / 2, getHeight() / 2); canvas.rotate(-133); dialPaint.setColor(Color.parseColor("#3CB7EA")); for (int i = 0; i < 60; i++) { canvas.drawLine(0, -dialRadius, 0, -dialRadius + scaleHeight, dialPaint); canvas.rotate(4.5f); }
canvas.rotate(90); dialPaint.setColor(Color.parseColor("#E37364")); for (int i = 0; i < (temperature - minTemp) * angleRate; i++) { canvas.drawLine(0, -dialRadius, 0, -dialRadius + scaleHeight, dialPaint); canvas.rotate(4.5f); } canvas.restore();}繪製刻度盤下的圓弧private void drawArc(Canvas canvas) { canvas.save(); canvas.translate(getWidth() / 2, getHeight() / 2); canvas.rotate(135 + 2); RectF rectF = new RectF(-arcRadius, -arcRadius, arcRadius, arcRadius); canvas.drawArc(rectF, 0, 265, false, arcPaint); canvas.restore();}繪製標題與溫度標識private void drawText(Canvas canvas) { canvas.save();
float titleWidth = titlePaint.measureText(title); canvas.drawText(title, (width - titleWidth) / 2, dialRadius * 2 + dp2px(15), titlePaint);
String minTempFlag = minTemp < 10 ? "0" + minTemp : minTemp + ""; float tempFlagWidth = titlePaint.measureText(maxTemp + ""); canvas.rotate(55, width / 2, height / 2); canvas.drawText(minTempFlag, (width - tempFlagWidth) / 2, height + dp2px(5), tempFlagPaint);
canvas.rotate(-105, width / 2, height / 2); canvas.drawText(maxTemp + "", (width - tempFlagWidth) / 2, height + dp2px(5), tempFlagPaint); canvas.restore(); }繪製旋轉按鈕private void drawButton(Canvas canvas) { int buttonWidth = buttonImage.getWidth(); int buttonHeight = buttonImage.getHeight(); int buttonShadowWidth = buttonImageShadow.getWidth(); int buttonShadowHeight = buttonImageShadow.getHeight();
canvas.drawBitmap(buttonImageShadow, (width - buttonShadowWidth) / 2, (height - buttonShadowHeight) / 2, buttonPaint);
Matrix matrix = new Matrix(); matrix.setTranslate(buttonWidth / 2, buttonHeight / 2); matrix.preRotate(45 + rotateAngle); matrix.preTranslate(-buttonWidth / 2, -buttonHeight / 2); matrix.postTranslate((width - buttonWidth) / 2, (height - buttonHeight) / 2);
canvas.setDrawFilter(paintFlagsDrawFilter); canvas.drawBitmap(buttonImage, matrix, buttonPaint);}繪製溫度private void drawTemp(Canvas canvas) { canvas.save(); canvas.translate(getWidth() / 2, getHeight() / 2);
float tempWidth = tempPaint.measureText(temperature + ""); float tempHeight = (tempPaint.ascent() + tempPaint.descent()) / 2; canvas.drawText(temperature + "°", -tempWidth / 2 - dp2px(5), -tempHeight, tempPaint); canvas.restore();}處理滑動事件private boolean isDown;private boolean isMove;
@Overridepublic boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: isDown = true; float downX = event.getX(); float downY = event.getY(); currentAngle = calcAngle(downX, downY); break;
case MotionEvent.ACTION_MOVE: isMove = true; float targetX; float targetY; downX = targetX = event.getX(); downY = targetY = event.getY(); float angle = calcAngle(targetX, targetY);
float angleIncreased = angle - currentAngle;
if (angleIncreased < -270) { angleIncreased = angleIncreased + 360; } else if (angleIncreased > 270) { angleIncreased = angleIncreased - 360; }
IncreaseAngle(angleIncreased); currentAngle = angle; invalidate(); break;
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: { if (isDown && isMove) { rotateAngle = (float) ((temperature - minTemp) * angleRate * 4.5); invalidate(); onTempChangeListener.change(temperature); isDown = false; isMove = false; } break; } } return true;}
private float calcAngle(float targetX, float targetY) { float x = targetX - width / 2; float y = targetY - height / 2; double radian;
if (x != 0) { float tan = Math.abs(y / x); if (x > 0) { if (y >= 0) { radian = Math.atan(tan); } else { radian = 2 * Math.PI - Math.atan(tan); } } else { if (y >= 0) { radian = Math.PI - Math.atan(tan); } else { radian = Math.PI + Math.atan(tan); } } } else { if (y > 0) { radian = Math.PI / 2; } else { radian = -Math.PI / 2; } } return (float) ((radian * 180) / Math.PI);}
private void IncreaseAngle(float angle) { rotateAngle += angle; if (rotateAngle < 0) { rotateAngle = 0; } else if (rotateAngle > 270) { rotateAngle = 270; } temperature = (int) (rotateAngle / 4.5) / angleRate + minTemp;}提供一些接口方法public void setTemp(int minTemp, int maxTemp, int temp) { this.minTemp = minTemp; this.maxTemp = maxTemp; this.temperature = temp; this.angleRate = 60 / (maxTemp - minTemp); rotateAngle = (float) ((temp - minTemp) * angleRate * 4.5); invalidate();}
public void setOnTempChangeListener(OnTempChangeListener onTempChangeListener) { this.onTempChangeListener = onTempChangeListener;}
public interface OnTempChangeListener { void change(int temp);}https://github.com/alidili/TempControlView