| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.graphics.Canvas; |
| 20 |
import android.graphics.Color; |
| 21 |
import android.graphics.Paint; |
| 22 |
import android.graphics.Rect; |
| 23 |
import android.graphics.RectF; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class RoundScrollbarRenderer { |
| 29 |
|
| 30 |
private static final int SCROLLBAR_ANGLE_RANGE = 90; |
| 31 |
private static final int MAX_SCROLLBAR_ANGLE_SWIPE = 16; |
| 32 |
private static final int MIN_SCROLLBAR_ANGLE_SWIPE = 6; |
| 33 |
private static final float WIDTH_PERCENTAGE = 0.02f; |
| 34 |
private static final int DEFAULT_THUMB_COLOR = 0xFFE8EAED; |
| 35 |
private static final int DEFAULT_TRACK_COLOR = 0x4CFFFFFF; |
| 36 |
|
| 37 |
private final Paint mThumbPaint = new Paint(); |
| 38 |
private final Paint mTrackPaint = new Paint(); |
| 39 |
private final RectF mRect = new RectF(); |
| 40 |
private final View mParent; |
| 41 |
private final int mMaskThickness; |
| 42 |
|
| 43 |
public RoundScrollbarRenderer(View parent) { |
| 44 |
|
| 45 |
|
| 46 |
mThumbPaint.setAntiAlias(true); |
| 47 |
mThumbPaint.setStrokeCap(Paint.Cap.ROUND); |
| 48 |
mThumbPaint.setStyle(Paint.Style.STROKE); |
| 49 |
|
| 50 |
|
| 51 |
mTrackPaint.setAntiAlias(true); |
| 52 |
mTrackPaint.setStrokeCap(Paint.Cap.ROUND); |
| 53 |
mTrackPaint.setStyle(Paint.Style.STROKE); |
| 54 |
|
| 55 |
mParent = parent; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
mMaskThickness = parent.getContext().getResources().getDimensionPixelSize( |
| 61 |
com.android.internal.R.dimen.circular_display_mask_thickness); |
| 62 |
} |
| 63 |
|
| 64 |
public void drawRoundScrollbars(Canvas canvas, float alpha, Rect bounds) { |
| 65 |
if (alpha == 0) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
float maxScroll = mParent.computeVerticalScrollRange(); |
| 70 |
float scrollExtent = mParent.computeVerticalScrollExtent(); |
| 71 |
if (scrollExtent <= 0 || maxScroll <= scrollExtent) { |
| 72 |
return; |
| 73 |
} |
| 74 |
float currentScroll = Math.max(0, mParent.computeVerticalScrollOffset()); |
| 75 |
float linearThumbLength = mParent.computeVerticalScrollExtent(); |
| 76 |
float thumbWidth = mParent.getWidth() * WIDTH_PERCENTAGE; |
| 77 |
mThumbPaint.setStrokeWidth(thumbWidth); |
| 78 |
mTrackPaint.setStrokeWidth(thumbWidth); |
| 79 |
|
| 80 |
setThumbColor(applyAlpha(DEFAULT_THUMB_COLOR, alpha)); |
| 81 |
setTrackColor(applyAlpha(DEFAULT_TRACK_COLOR, alpha)); |
| 82 |
|
| 83 |
|
| 84 |
float sweepAngle = (linearThumbLength / maxScroll) * SCROLLBAR_ANGLE_RANGE; |
| 85 |
sweepAngle = clamp(sweepAngle, MIN_SCROLLBAR_ANGLE_SWIPE, MAX_SCROLLBAR_ANGLE_SWIPE); |
| 86 |
|
| 87 |
float startAngle = (currentScroll * (SCROLLBAR_ANGLE_RANGE - sweepAngle)) |
| 88 |
/ (maxScroll - linearThumbLength) - SCROLLBAR_ANGLE_RANGE / 2; |
| 89 |
startAngle = clamp(startAngle, -SCROLLBAR_ANGLE_RANGE / 2, |
| 90 |
SCROLLBAR_ANGLE_RANGE / 2 - sweepAngle); |
| 91 |
|
| 92 |
|
| 93 |
float inset = thumbWidth / 2 + mMaskThickness; |
| 94 |
mRect.set( |
| 95 |
bounds.left + inset, |
| 96 |
bounds.top + inset, |
| 97 |
bounds.right - inset, |
| 98 |
bounds.bottom - inset); |
| 99 |
canvas.drawArc(mRect, -SCROLLBAR_ANGLE_RANGE / 2, SCROLLBAR_ANGLE_RANGE, false, |
| 100 |
mTrackPaint); |
| 101 |
canvas.drawArc(mRect, startAngle, sweepAngle, false, mThumbPaint); |
| 102 |
} |
| 103 |
|
| 104 |
private static float clamp(float val, float min, float max) { |
| 105 |
if (val < min) { |
| 106 |
return min; |
| 107 |
} else if (val > max) { |
| 108 |
return max; |
| 109 |
} else { |
| 110 |
return val; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
private static int applyAlpha(int color, float alpha) { |
| 115 |
int alphaByte = (int) (Color.alpha(color) * alpha); |
| 116 |
return Color.argb(alphaByte, Color.red(color), Color.green(color), Color.blue(color)); |
| 117 |
} |
| 118 |
|
| 119 |
private void setThumbColor(int thumbColor) { |
| 120 |
if (mThumbPaint.getColor() != thumbColor) { |
| 121 |
mThumbPaint.setColor(thumbColor); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
private void setTrackColor(int trackColor) { |
| 126 |
if (mTrackPaint.getColor() != trackColor) { |
| 127 |
mTrackPaint.setColor(trackColor); |
| 128 |
} |
| 129 |
} |
| 130 |
} |