|
| 1 | +/** |
| 2 | + * Copyright (c) 2021 The Brave Authors. All rights reserved. |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 | + * You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | +package org.chromium.chrome.browser.custom_layout.popup_window_tooltip; |
| 8 | + |
| 9 | +import android.graphics.Canvas; |
| 10 | +import android.graphics.Color; |
| 11 | +import android.graphics.ColorFilter; |
| 12 | +import android.graphics.Paint; |
| 13 | +import android.graphics.Path; |
| 14 | +import android.graphics.PixelFormat; |
| 15 | +import android.graphics.Rect; |
| 16 | +import android.graphics.drawable.ColorDrawable; |
| 17 | + |
| 18 | +import androidx.annotation.ColorInt; |
| 19 | + |
| 20 | +public class ArrowColorDrawable extends ColorDrawable { |
| 21 | + public static final int LEFT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3, AUTO = 4; |
| 22 | + |
| 23 | + private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 24 | + private final int mBackgroundColor; |
| 25 | + private Path mPath; |
| 26 | + private final int mDirection; |
| 27 | + |
| 28 | + ArrowColorDrawable(@ColorInt int foregroundColor, int direction) { |
| 29 | + this.mBackgroundColor = Color.TRANSPARENT; |
| 30 | + this.mPaint.setColor(foregroundColor); |
| 31 | + this.mDirection = direction; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected void onBoundsChange(Rect bounds) { |
| 36 | + super.onBoundsChange(bounds); |
| 37 | + updatePath(bounds); |
| 38 | + } |
| 39 | + |
| 40 | + private synchronized void updatePath(Rect bounds) { |
| 41 | + mPath = new Path(); |
| 42 | + |
| 43 | + switch (mDirection) { |
| 44 | + case LEFT: |
| 45 | + mPath.moveTo(bounds.width(), bounds.height()); |
| 46 | + mPath.lineTo(0, (float) bounds.height() / 2); |
| 47 | + mPath.lineTo(bounds.width(), 0); |
| 48 | + mPath.lineTo(bounds.width(), bounds.height()); |
| 49 | + break; |
| 50 | + case TOP: |
| 51 | + mPath.moveTo(0, bounds.height()); |
| 52 | + mPath.lineTo((float) bounds.width() / 2, 0); |
| 53 | + mPath.lineTo(bounds.width(), bounds.height()); |
| 54 | + mPath.lineTo(0, bounds.height()); |
| 55 | + break; |
| 56 | + case RIGHT: |
| 57 | + mPath.moveTo(0, 0); |
| 58 | + mPath.lineTo(bounds.width(), (float) bounds.height() / 2); |
| 59 | + mPath.lineTo(0, bounds.height()); |
| 60 | + mPath.lineTo(0, 0); |
| 61 | + break; |
| 62 | + case BOTTOM: |
| 63 | + mPath.moveTo(0, 0); |
| 64 | + mPath.lineTo((float) bounds.width() / 2, bounds.height()); |
| 65 | + mPath.lineTo(bounds.width(), 0); |
| 66 | + mPath.lineTo(0, 0); |
| 67 | + break; |
| 68 | + } |
| 69 | + |
| 70 | + mPath.close(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void draw(Canvas canvas) { |
| 75 | + canvas.drawColor(mBackgroundColor); |
| 76 | + if (mPath == null) updatePath(getBounds()); |
| 77 | + canvas.drawPath(mPath, mPaint); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void setAlpha(int alpha) { |
| 82 | + mPaint.setAlpha(alpha); |
| 83 | + } |
| 84 | + |
| 85 | + public void setColor(@ColorInt int color) { |
| 86 | + mPaint.setColor(color); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void setColorFilter(ColorFilter colorFilter) { |
| 91 | + mPaint.setColorFilter(colorFilter); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public int getOpacity() { |
| 96 | + if (mPaint.getColorFilter() != null) { |
| 97 | + return PixelFormat.TRANSLUCENT; |
| 98 | + } |
| 99 | + |
| 100 | + switch (mPaint.getColor() >>> 24) { |
| 101 | + case 255: |
| 102 | + return PixelFormat.OPAQUE; |
| 103 | + case 0: |
| 104 | + return PixelFormat.TRANSPARENT; |
| 105 | + } |
| 106 | + return PixelFormat.TRANSLUCENT; |
| 107 | + } |
| 108 | +} |
0 commit comments