1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.graphics; 18 19 import android.graphics.Canvas; 20 import android.graphics.ColorFilter; 21 import android.graphics.Matrix; 22 import android.graphics.Outline; 23 import android.graphics.Paint; 24 import android.graphics.Path; 25 import android.graphics.PixelFormat; 26 import android.graphics.Rect; 27 import android.graphics.drawable.Drawable; 28 29 public class FastScrollThumbDrawable extends Drawable { 30 31 private static final Matrix sMatrix = new Matrix(); 32 33 private final Path mPath = new Path(); 34 private final Paint mPaint; 35 private final boolean mIsRtl; 36 FastScrollThumbDrawable(Paint paint, boolean isRtl)37 public FastScrollThumbDrawable(Paint paint, boolean isRtl) { 38 mPaint = paint; 39 mIsRtl = isRtl; 40 } 41 42 @Override getOutline(Outline outline)43 public void getOutline(Outline outline) { 44 if (mPath.isConvex()) { 45 outline.setConvexPath(mPath); 46 } 47 } 48 49 @Override onBoundsChange(Rect bounds)50 protected void onBoundsChange(Rect bounds) { 51 mPath.reset(); 52 53 float r = bounds.height() * 0.5f; 54 // The path represents a rotate tear-drop shape, with radius of one corner is 1/5th of the 55 // other 3 corners. 56 float diameter = 2 * r; 57 float r2 = r / 5; 58 mPath.addRoundRect(bounds.left, bounds.top, bounds.left + diameter, bounds.top + diameter, 59 new float[] {r, r, r, r, r2, r2, r, r}, 60 Path.Direction.CCW); 61 62 sMatrix.setRotate(-45, bounds.left + r, bounds.top + r); 63 if (mIsRtl) { 64 sMatrix.postTranslate(bounds.width(), 0); 65 sMatrix.postScale(-1, 1, bounds.width(), 0); 66 } 67 mPath.transform(sMatrix); 68 } 69 70 @Override draw(Canvas canvas)71 public void draw(Canvas canvas) { 72 canvas.drawPath(mPath, mPaint); 73 } 74 75 @Override setAlpha(int i)76 public void setAlpha(int i) { 77 // Not supported 78 } 79 80 @Override setColorFilter(ColorFilter colorFilter)81 public void setColorFilter(ColorFilter colorFilter) { 82 // Not supported 83 } 84 85 @Override getOpacity()86 public int getOpacity() { 87 return PixelFormat.TRANSLUCENT; 88 } 89 } 90