1 /* 2 * Copyright (C) 2018 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 package com.android.launcher3.icons; 17 18 import android.content.Context; 19 import android.content.res.TypedArray; 20 import android.graphics.Bitmap; 21 import android.graphics.Rect; 22 import android.graphics.Region; 23 import android.graphics.RegionIterator; 24 import android.util.Log; 25 26 import androidx.annotation.ColorInt; 27 28 import java.io.ByteArrayOutputStream; 29 import java.io.IOException; 30 31 public class GraphicsUtils { 32 33 private static final String TAG = "GraphicsUtils"; 34 35 public static Runnable sOnNewBitmapRunnable = () -> { }; 36 37 /** 38 * Set the alpha component of {@code color} to be {@code alpha}. Unlike the support lib version, 39 * it bounds the alpha in valid range instead of throwing an exception to allow for safer 40 * interpolation of color animations 41 */ 42 @ColorInt setColorAlphaBound(int color, int alpha)43 public static int setColorAlphaBound(int color, int alpha) { 44 if (alpha < 0) { 45 alpha = 0; 46 } else if (alpha > 255) { 47 alpha = 255; 48 } 49 return (color & 0x00ffffff) | (alpha << 24); 50 } 51 52 /** 53 * Compresses the bitmap to a byte array for serialization. 54 */ flattenBitmap(Bitmap bitmap)55 public static byte[] flattenBitmap(Bitmap bitmap) { 56 ByteArrayOutputStream out = new ByteArrayOutputStream(getExpectedBitmapSize(bitmap)); 57 try { 58 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); 59 out.flush(); 60 out.close(); 61 return out.toByteArray(); 62 } catch (IOException e) { 63 Log.w(TAG, "Could not write bitmap"); 64 return null; 65 } 66 } 67 68 /** 69 * Try go guesstimate how much space the icon will take when serialized to avoid unnecessary 70 * allocations/copies during the write (4 bytes per pixel). 71 */ getExpectedBitmapSize(Bitmap bitmap)72 static int getExpectedBitmapSize(Bitmap bitmap) { 73 return bitmap.getWidth() * bitmap.getHeight() * 4; 74 } 75 getArea(Region r)76 public static int getArea(Region r) { 77 RegionIterator itr = new RegionIterator(r); 78 int area = 0; 79 Rect tempRect = new Rect(); 80 while (itr.next(tempRect)) { 81 area += tempRect.width() * tempRect.height(); 82 } 83 return area; 84 } 85 86 /** 87 * Utility method to track new bitmap creation 88 */ noteNewBitmapCreated()89 public static void noteNewBitmapCreated() { 90 sOnNewBitmapRunnable.run(); 91 } 92 93 /** 94 * Returns the color associated with the attribute 95 */ getAttrColor(Context context, int attr)96 public static int getAttrColor(Context context, int attr) { 97 TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); 98 int colorAccent = ta.getColor(0, 0); 99 ta.recycle(); 100 return colorAccent; 101 } 102 103 /** 104 * Returns the alpha corresponding to the theme attribute {@param attr} 105 */ getFloat(Context context, int attr, float defValue)106 public static float getFloat(Context context, int attr, float defValue) { 107 TypedArray ta = context.obtainStyledAttributes(new int[]{attr}); 108 float value = ta.getFloat(0, defValue); 109 ta.recycle(); 110 return value; 111 } 112 } 113