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
17package com.android.launcher3.graphics;
18
19import android.graphics.Canvas;
20import android.graphics.ColorFilter;
21import android.graphics.Matrix;
22import android.graphics.Outline;
23import android.graphics.Paint;
24import android.graphics.Path;
25import android.graphics.PixelFormat;
26import android.graphics.Rect;
27import android.graphics.drawable.Drawable;
28
29public 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
37    public FastScrollThumbDrawable(Paint paint, boolean isRtl) {
38        mPaint = paint;
39        mIsRtl = isRtl;
40    }
41
42    @Override
43    public void getOutline(Outline outline) {
44        if (mPath.isConvex()) {
45            outline.setConvexPath(mPath);
46        }
47    }
48
49    @Override
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
71    public void draw(Canvas canvas) {
72        canvas.drawPath(mPath, mPaint);
73    }
74
75    @Override
76    public void setAlpha(int i) {
77        // Not supported
78    }
79
80    @Override
81    public void setColorFilter(ColorFilter colorFilter) {
82        // Not supported
83    }
84
85    @Override
86    public int getOpacity() {
87        return PixelFormat.TRANSLUCENT;
88    }
89}
90