Skip to content

Commit efa7e68

Browse files
committed
[TimePicker] Update clock dial selector drawing logic
Resolves material-components#5042 Resolves material-components#5043 Resolves material-components#5044
1 parent f66c5df commit efa7e68

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

lib/java/com/google/android/material/timepicker/ClockHandView.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.android.material.timepicker;
1818

19+
import android.graphics.Color;
20+
import androidx.core.graphics.ColorUtils;
1921
import com.google.android.material.R;
2022

2123
import static com.google.android.material.timepicker.RadialViewGroup.LEVEL_1;
@@ -79,6 +81,7 @@ public interface OnActionUpListener {
7981

8082
private final int selectorRadius;
8183
private final float centerDotRadius;
84+
private final int clockHandColor;
8285
private final Paint paint = new Paint();
8386
// Since the selector moves, overlapping views may need information about
8487
// its current position
@@ -126,9 +129,8 @@ public ClockHandView(Context context, @Nullable AttributeSet attrs, int defStyle
126129
Resources res = getResources();
127130
selectorStrokeWidth = res.getDimensionPixelSize(R.dimen.material_clock_hand_stroke_width);
128131
centerDotRadius = res.getDimensionPixelSize(R.dimen.material_clock_hand_center_dot_radius);
129-
int selectorColor = a.getColor(R.styleable.ClockHandView_clockHandColor, 0);
132+
clockHandColor = a.getColor(R.styleable.ClockHandView_clockHandColor, 0);
130133
paint.setAntiAlias(true);
131-
paint.setColor(selectorColor);
132134
setHandRotation(0);
133135

134136
scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
@@ -251,6 +253,16 @@ protected void onDraw(Canvas canvas) {
251253
}
252254

253255
private void drawSelector(Canvas canvas) {
256+
int saveCount = 0;
257+
int clockHandColorAlpha = Color.alpha(clockHandColor);
258+
if (clockHandColorAlpha < 255) {
259+
saveCount = canvas.saveLayerAlpha(null, clockHandColorAlpha);
260+
int clockHandColorOpaque = ColorUtils.setAlphaComponent(clockHandColor, 255);
261+
paint.setColor(clockHandColorOpaque);
262+
} else {
263+
paint.setColor(clockHandColor);
264+
}
265+
254266
int yCenter = getHeight() / 2;
255267
int xCenter = getWidth() / 2;
256268

@@ -263,18 +275,14 @@ private void drawSelector(Canvas canvas) {
263275
paint.setStrokeWidth(0);
264276
canvas.drawCircle(selCenterX, selCenterY, selectorRadius, paint);
265277

266-
// Shorten the line to only go from the edge of the center dot to the
267-
// edge of the selection circle.
268-
double sin = Math.sin(degRad);
269-
double cos = Math.cos(degRad);
270-
float lineLength = leveledCircleRadius - selectorRadius;
271-
float linePointX = xCenter + (int) (lineLength * cos);
272-
float linePointY = yCenter + (int) (lineLength * sin);
273-
274278
// Draw the line.
275279
paint.setStrokeWidth(selectorStrokeWidth);
276-
canvas.drawLine(xCenter, yCenter, linePointX, linePointY, paint);
280+
canvas.drawLine(xCenter, yCenter, selCenterX, selCenterY, paint);
277281
canvas.drawCircle(xCenter, yCenter, centerDotRadius, paint);
282+
283+
if (clockHandColorAlpha < 255) {
284+
canvas.restoreToCount(saveCount);
285+
}
278286
}
279287

280288
/** Returns the current bounds of the selector, relative to the this view. */

0 commit comments

Comments
 (0)