Juan Ezquerro LLanes | 92d144f | 2018-05-23 21:19:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Daniel Nilsson |
| 3 | * Copyright (C) 2012 The CyanogenMod Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
micky387 | 9850641 | 2023-10-23 16:33:47 +0200 | [diff] [blame] | 18 | package omnirom.ui; |
Juan Ezquerro LLanes | 92d144f | 2018-05-23 21:19:00 +0200 | [diff] [blame] | 19 | |
| 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.ColorFilter; |
| 23 | import android.graphics.Paint; |
| 24 | import android.graphics.Rect; |
| 25 | import android.graphics.Bitmap.Config; |
| 26 | import android.graphics.drawable.Drawable; |
| 27 | |
| 28 | /** |
| 29 | * This drawable that draws a simple white and gray chess board pattern. It's |
| 30 | * pattern you will often see as a background behind a partly transparent image |
| 31 | * in many applications. |
| 32 | * |
| 33 | * @author Daniel Nilsson |
| 34 | */ |
| 35 | public class AlphaPatternDrawable extends Drawable { |
| 36 | |
| 37 | private int mRectangleSize = 10; |
| 38 | |
| 39 | private Paint mPaint = new Paint(); |
| 40 | private Paint mPaintWhite = new Paint(); |
| 41 | private Paint mPaintGray = new Paint(); |
| 42 | |
| 43 | private int numRectanglesHorizontal; |
| 44 | private int numRectanglesVertical; |
| 45 | |
| 46 | /** |
| 47 | * Bitmap in which the pattern will be cached. |
| 48 | */ |
| 49 | private Bitmap mBitmap; |
| 50 | |
| 51 | public AlphaPatternDrawable(int rectangleSize) { |
| 52 | mRectangleSize = rectangleSize; |
| 53 | mPaintWhite.setColor(0xffffffff); |
| 54 | mPaintGray.setColor(0xffcbcbcb); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void draw(Canvas canvas) { |
| 59 | if (mBitmap != null) { |
| 60 | canvas.drawBitmap(mBitmap, null, getBounds(), mPaint); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public int getOpacity() { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void setAlpha(int alpha) { |
| 71 | throw new UnsupportedOperationException("Alpha is not supported by this drawwable."); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void setColorFilter(ColorFilter cf) { |
| 76 | throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable."); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected void onBoundsChange(Rect bounds) { |
| 81 | super.onBoundsChange(bounds); |
| 82 | |
| 83 | int height = bounds.height(); |
| 84 | int width = bounds.width(); |
| 85 | |
| 86 | numRectanglesHorizontal = (int) Math.ceil((width / mRectangleSize)); |
| 87 | numRectanglesVertical = (int) Math.ceil(height / mRectangleSize); |
| 88 | |
| 89 | generatePatternBitmap(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * This will generate a bitmap with the pattern as big as the rectangle we |
| 94 | * were allow to draw on. We do this to cache the bitmap so we don't need |
| 95 | * to recreate it each time draw() is called since it takes a few |
| 96 | * milliseconds. |
| 97 | */ |
| 98 | private void generatePatternBitmap() { |
| 99 | |
| 100 | if (getBounds().width() <= 0 || getBounds().height() <= 0) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888); |
| 105 | Canvas canvas = new Canvas(mBitmap); |
| 106 | |
| 107 | Rect r = new Rect(); |
| 108 | boolean verticalStartWhite = true; |
| 109 | for (int i = 0; i <= numRectanglesVertical; i++) { |
| 110 | boolean isWhite = verticalStartWhite; |
| 111 | for (int j = 0; j <= numRectanglesHorizontal; j++) { |
| 112 | r.top = i * mRectangleSize; |
| 113 | r.left = j * mRectangleSize; |
| 114 | r.bottom = r.top + mRectangleSize; |
| 115 | r.right = r.left + mRectangleSize; |
| 116 | |
| 117 | canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray); |
| 118 | |
| 119 | isWhite = !isWhite; |
| 120 | } |
| 121 | |
| 122 | verticalStartWhite = !verticalStartWhite; |
| 123 | } |
| 124 | } |
| 125 | } |