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.quickstep.views; 17 18 import android.content.Context; 19 import android.graphics.Canvas; 20 import android.graphics.drawable.Drawable; 21 import android.util.AttributeSet; 22 import android.view.View; 23 24 import com.android.launcher3.Utilities; 25 26 /** 27 * A view which draws a drawable stretched to fit its size. Unlike ImageView, it avoids relayout 28 * when the drawable changes. 29 */ 30 public class IconView extends View { 31 32 private Drawable mDrawable; 33 IconView(Context context)34 public IconView(Context context) { 35 super(context); 36 } 37 IconView(Context context, AttributeSet attrs)38 public IconView(Context context, AttributeSet attrs) { 39 super(context, attrs); 40 } 41 IconView(Context context, AttributeSet attrs, int defStyleAttr)42 public IconView(Context context, AttributeSet attrs, int defStyleAttr) { 43 super(context, attrs, defStyleAttr); 44 } 45 setDrawable(Drawable d)46 public void setDrawable(Drawable d) { 47 if (mDrawable != null) { 48 mDrawable.setCallback(null); 49 } 50 mDrawable = d; 51 if (mDrawable != null) { 52 mDrawable.setCallback(this); 53 mDrawable.setBounds(0, 0, getWidth(), getHeight()); 54 } 55 invalidate(); 56 } 57 getDrawable()58 public Drawable getDrawable() { 59 return mDrawable; 60 } 61 62 @Override onSizeChanged(int w, int h, int oldw, int oldh)63 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 64 super.onSizeChanged(w, h, oldw, oldh); 65 if (mDrawable != null) { 66 mDrawable.setBounds(0, 0, w, h); 67 } 68 } 69 70 @Override verifyDrawable(Drawable who)71 protected boolean verifyDrawable(Drawable who) { 72 return super.verifyDrawable(who) || who == mDrawable; 73 } 74 75 @Override drawableStateChanged()76 protected void drawableStateChanged() { 77 super.drawableStateChanged(); 78 79 final Drawable drawable = mDrawable; 80 if (drawable != null && drawable.isStateful() 81 && drawable.setState(getDrawableState())) { 82 invalidateDrawable(drawable); 83 } 84 } 85 86 @Override onDraw(Canvas canvas)87 protected void onDraw(Canvas canvas) { 88 if (mDrawable != null) { 89 mDrawable.draw(canvas); 90 } 91 } 92 93 @Override hasOverlappingRendering()94 public boolean hasOverlappingRendering() { 95 return false; 96 } 97 98 @Override setAlpha(float alpha)99 public void setAlpha(float alpha) { 100 super.setAlpha(alpha); 101 if (alpha > 0) { 102 setVisibility(VISIBLE); 103 } else { 104 setVisibility(INVISIBLE); 105 } 106 } 107 108 /** 109 * Set the tint color of the icon, useful for scrimming or dimming. 110 * 111 * @param color to blend in. 112 * @param amount [0,1] 0 no tint, 1 full tint 113 */ setIconColorTint(int color, float amount)114 public void setIconColorTint(int color, float amount) { 115 if (mDrawable != null) { 116 mDrawable.setColorFilter(Utilities.makeColorTintingColorFilter(color, amount)); 117 } 118 } 119 } 120