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.animation.Animator; 19 import android.animation.AnimatorListenerAdapter; 20 import android.animation.ValueAnimator; 21 import android.content.Context; 22 import android.graphics.Canvas; 23 import android.graphics.Color; 24 import android.graphics.Path; 25 import android.graphics.PorterDuff; 26 import android.graphics.PorterDuffColorFilter; 27 import android.graphics.Rect; 28 import android.graphics.drawable.Drawable; 29 30 import androidx.core.graphics.ColorUtils; 31 32 /** 33 * Subclass which draws a placeholder icon when the actual icon is not yet loaded 34 */ 35 public class PlaceHolderIconDrawable extends FastBitmapDrawable { 36 37 // Path in [0, 100] bounds. 38 private final Path mProgressPath; 39 PlaceHolderIconDrawable(BitmapInfo info, Context context)40 public PlaceHolderIconDrawable(BitmapInfo info, Context context) { 41 super(info); 42 43 mProgressPath = GraphicsUtils.getShapePath(100); 44 mPaint.setColor(ColorUtils.compositeColors( 45 GraphicsUtils.getAttrColor(context, R.attr.loadingIconColor), info.color)); 46 } 47 48 @Override drawInternal(Canvas canvas, Rect bounds)49 protected void drawInternal(Canvas canvas, Rect bounds) { 50 int saveCount = canvas.save(); 51 canvas.translate(bounds.left, bounds.top); 52 canvas.scale(bounds.width() / 100f, bounds.height() / 100f); 53 canvas.drawPath(mProgressPath, mPaint); 54 canvas.restoreToCount(saveCount); 55 } 56 57 /** Updates this placeholder to {@code newIcon} with animation. */ animateIconUpdate(Drawable newIcon)58 public void animateIconUpdate(Drawable newIcon) { 59 int placeholderColor = mPaint.getColor(); 60 int originalAlpha = Color.alpha(placeholderColor); 61 62 ValueAnimator iconUpdateAnimation = ValueAnimator.ofInt(originalAlpha, 0); 63 iconUpdateAnimation.setDuration(375); 64 iconUpdateAnimation.addUpdateListener(valueAnimator -> { 65 int newAlpha = (int) valueAnimator.getAnimatedValue(); 66 int newColor = ColorUtils.setAlphaComponent(placeholderColor, newAlpha); 67 68 newIcon.setColorFilter(new PorterDuffColorFilter(newColor, PorterDuff.Mode.SRC_ATOP)); 69 }); 70 iconUpdateAnimation.addListener(new AnimatorListenerAdapter() { 71 @Override 72 public void onAnimationEnd(Animator animation) { 73 newIcon.setColorFilter(null); 74 } 75 }); 76 iconUpdateAnimation.start(); 77 } 78 79 } 80