2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2018-03-05 12:54:24 -08:00
|
|
|
import static com.android.launcher3.anim.Interpolators.ACCEL;
|
2019-01-03 01:02:43 -08:00
|
|
|
import static com.android.launcher3.anim.Interpolators.DEACCEL;
|
2018-03-05 12:54:24 -08:00
|
|
|
|
2014-08-14 10:53:27 -07:00
|
|
|
import android.animation.ObjectAnimator;
|
2019-10-24 15:59:49 -07:00
|
|
|
import android.content.Context;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
2014-08-14 10:53:27 -07:00
|
|
|
import android.graphics.Color;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.graphics.ColorFilter;
|
2014-07-14 12:02:01 -07:00
|
|
|
import android.graphics.ColorMatrix;
|
|
|
|
|
import android.graphics.ColorMatrixColorFilter;
|
2010-11-09 17:19:49 -08:00
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.graphics.PixelFormat;
|
2018-03-05 12:54:24 -08:00
|
|
|
import android.graphics.Rect;
|
2010-11-09 17:19:49 -08:00
|
|
|
import android.graphics.drawable.Drawable;
|
2017-02-02 12:57:18 -08:00
|
|
|
import android.util.Property;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2019-10-24 15:59:49 -07:00
|
|
|
import com.android.launcher3.graphics.PlaceHolderIconDrawable;
|
2018-09-25 17:03:34 -07:00
|
|
|
import com.android.launcher3.icons.BitmapInfo;
|
2020-04-06 15:11:17 -07:00
|
|
|
import com.android.launcher3.model.data.ItemInfoWithIcon;
|
2020-06-05 12:17:16 -07:00
|
|
|
import com.android.launcher3.util.Themes;
|
2017-01-11 09:53:12 -08:00
|
|
|
|
2020-01-23 12:42:06 -08:00
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
public class FastBitmapDrawable extends Drawable {
|
2017-02-16 13:33:15 -08:00
|
|
|
|
2018-03-05 12:54:24 -08:00
|
|
|
private static final float PRESSED_SCALE = 1.1f;
|
|
|
|
|
|
2016-11-08 10:40:34 -08:00
|
|
|
private static final float DISABLED_DESATURATION = 1f;
|
|
|
|
|
private static final float DISABLED_BRIGHTNESS = 0.5f;
|
2014-07-14 12:02:01 -07:00
|
|
|
|
2018-03-05 12:54:24 -08:00
|
|
|
public static final int CLICK_FEEDBACK_DURATION = 200;
|
2014-08-14 10:53:27 -07:00
|
|
|
|
2020-01-23 12:42:06 -08:00
|
|
|
private static ColorFilter sDisabledFColorFilter;
|
2014-07-14 12:02:01 -07:00
|
|
|
|
2016-11-12 09:58:29 -08:00
|
|
|
protected final Paint mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
|
2018-03-02 17:25:59 -08:00
|
|
|
protected Bitmap mBitmap;
|
2017-12-19 16:49:24 -08:00
|
|
|
protected final int mIconColor;
|
2017-02-16 13:33:15 -08:00
|
|
|
|
|
|
|
|
private boolean mIsPressed;
|
2016-11-08 10:40:34 -08:00
|
|
|
private boolean mIsDisabled;
|
2020-06-05 12:17:16 -07:00
|
|
|
private float mDisabledAlpha = 1f;
|
2014-07-14 12:02:01 -07:00
|
|
|
|
2018-03-05 12:54:24 -08:00
|
|
|
// Animator and properties for the fast bitmap drawable's scale
|
|
|
|
|
private static final Property<FastBitmapDrawable, Float> SCALE
|
|
|
|
|
= new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") {
|
2017-02-16 13:33:15 -08:00
|
|
|
@Override
|
|
|
|
|
public Float get(FastBitmapDrawable fastBitmapDrawable) {
|
2018-03-05 12:54:24 -08:00
|
|
|
return fastBitmapDrawable.mScale;
|
2017-02-16 13:33:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
|
2018-03-05 12:54:24 -08:00
|
|
|
fastBitmapDrawable.mScale = value;
|
|
|
|
|
fastBitmapDrawable.invalidateSelf();
|
2017-02-16 13:33:15 -08:00
|
|
|
}
|
|
|
|
|
};
|
2018-03-05 12:54:24 -08:00
|
|
|
private ObjectAnimator mScaleAnimation;
|
|
|
|
|
private float mScale = 1;
|
|
|
|
|
|
2015-08-21 11:16:27 -07:00
|
|
|
private int mAlpha = 255;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2015-04-08 19:01:34 -07:00
|
|
|
public FastBitmapDrawable(Bitmap b) {
|
2017-12-19 16:49:24 -08:00
|
|
|
this(b, Color.TRANSPARENT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FastBitmapDrawable(BitmapInfo info) {
|
|
|
|
|
this(info.icon, info.color);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected FastBitmapDrawable(Bitmap b, int iconColor) {
|
2019-03-29 10:49:43 -07:00
|
|
|
this(b, iconColor, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected FastBitmapDrawable(Bitmap b, int iconColor, boolean isDisabled) {
|
2009-03-03 19:32:27 -08:00
|
|
|
mBitmap = b;
|
2017-12-19 16:49:24 -08:00
|
|
|
mIconColor = iconColor;
|
2017-02-02 16:37:21 -08:00
|
|
|
setFilterBitmap(true);
|
2019-03-29 10:49:43 -07:00
|
|
|
setIsDisabled(isDisabled);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2018-03-05 12:54:24 -08:00
|
|
|
public final void draw(Canvas canvas) {
|
2018-06-25 15:32:24 -07:00
|
|
|
if (mScale != 1f) {
|
2018-03-05 12:54:24 -08:00
|
|
|
int count = canvas.save();
|
|
|
|
|
Rect bounds = getBounds();
|
|
|
|
|
canvas.scale(mScale, mScale, bounds.exactCenterX(), bounds.exactCenterY());
|
|
|
|
|
drawInternal(canvas, bounds);
|
|
|
|
|
canvas.restoreToCount(count);
|
|
|
|
|
} else {
|
|
|
|
|
drawInternal(canvas, getBounds());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void drawInternal(Canvas canvas, Rect bounds) {
|
|
|
|
|
canvas.drawBitmap(mBitmap, null, bounds, mPaint);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2011-05-26 19:08:29 -07:00
|
|
|
@Override
|
|
|
|
|
public void setColorFilter(ColorFilter cf) {
|
2014-07-14 12:02:01 -07:00
|
|
|
// No op
|
2011-05-26 19:08:29 -07:00
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public int getOpacity() {
|
|
|
|
|
return PixelFormat.TRANSLUCENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setAlpha(int alpha) {
|
2019-08-30 18:42:01 -07:00
|
|
|
if (mAlpha != alpha) {
|
|
|
|
|
mAlpha = alpha;
|
|
|
|
|
mPaint.setAlpha(alpha);
|
|
|
|
|
invalidateSelf();
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
2014-07-14 12:02:01 -07:00
|
|
|
@Override
|
2011-06-17 13:26:23 -07:00
|
|
|
public void setFilterBitmap(boolean filterBitmap) {
|
|
|
|
|
mPaint.setFilterBitmap(filterBitmap);
|
2013-10-25 15:24:24 -07:00
|
|
|
mPaint.setAntiAlias(filterBitmap);
|
2011-06-17 13:26:23 -07:00
|
|
|
}
|
|
|
|
|
|
2010-12-01 15:47:31 -08:00
|
|
|
public int getAlpha() {
|
|
|
|
|
return mAlpha;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 15:32:24 -07:00
|
|
|
public void setScale(float scale) {
|
|
|
|
|
if (mScaleAnimation != null) {
|
|
|
|
|
mScaleAnimation.cancel();
|
|
|
|
|
mScaleAnimation = null;
|
|
|
|
|
}
|
|
|
|
|
mScale = scale;
|
|
|
|
|
invalidateSelf();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-05 12:54:24 -08:00
|
|
|
public float getAnimatedScale() {
|
|
|
|
|
return mScaleAnimation == null ? 1 : mScale;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-25 15:32:24 -07:00
|
|
|
public float getScale() {
|
|
|
|
|
return mScale;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
@Override
|
|
|
|
|
public int getIntrinsicWidth() {
|
2014-09-05 07:04:59 -07:00
|
|
|
return mBitmap.getWidth();
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getIntrinsicHeight() {
|
2014-09-05 07:04:59 -07:00
|
|
|
return mBitmap.getHeight();
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getMinimumWidth() {
|
2013-11-13 15:47:05 -08:00
|
|
|
return getBounds().width();
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getMinimumHeight() {
|
2013-11-13 15:47:05 -08:00
|
|
|
return getBounds().height();
|
2010-02-08 13:44:00 -08:00
|
|
|
}
|
|
|
|
|
|
2017-02-16 13:33:15 -08:00
|
|
|
@Override
|
|
|
|
|
public boolean isStateful() {
|
|
|
|
|
return true;
|
2014-07-14 12:02:01 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-21 17:28:23 -07:00
|
|
|
@Override
|
|
|
|
|
public ColorFilter getColorFilter() {
|
|
|
|
|
return mPaint.getColorFilter();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-16 13:33:15 -08:00
|
|
|
@Override
|
|
|
|
|
protected boolean onStateChange(int[] state) {
|
|
|
|
|
boolean isPressed = false;
|
|
|
|
|
for (int s : state) {
|
|
|
|
|
if (s == android.R.attr.state_pressed) {
|
|
|
|
|
isPressed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (mIsPressed != isPressed) {
|
|
|
|
|
mIsPressed = isPressed;
|
2015-08-21 11:16:27 -07:00
|
|
|
|
2018-03-05 12:54:24 -08:00
|
|
|
if (mScaleAnimation != null) {
|
|
|
|
|
mScaleAnimation.cancel();
|
|
|
|
|
mScaleAnimation = null;
|
2017-02-16 13:33:15 -08:00
|
|
|
}
|
2015-08-21 11:16:27 -07:00
|
|
|
|
2017-02-16 13:33:15 -08:00
|
|
|
if (mIsPressed) {
|
|
|
|
|
// Animate when going to pressed state
|
2018-03-05 12:54:24 -08:00
|
|
|
mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
|
|
|
|
|
mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
|
|
|
|
|
mScaleAnimation.setInterpolator(ACCEL);
|
|
|
|
|
mScaleAnimation.start();
|
2017-02-16 13:33:15 -08:00
|
|
|
} else {
|
2019-01-03 01:02:43 -08:00
|
|
|
if (isVisible()) {
|
|
|
|
|
mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, 1f);
|
|
|
|
|
mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
|
|
|
|
|
mScaleAnimation.setInterpolator(DEACCEL);
|
|
|
|
|
mScaleAnimation.start();
|
|
|
|
|
} else {
|
|
|
|
|
mScale = 1f;
|
|
|
|
|
invalidateSelf();
|
|
|
|
|
}
|
2017-02-16 13:33:15 -08:00
|
|
|
}
|
2015-08-21 11:16:27 -07:00
|
|
|
return true;
|
2014-08-14 10:53:27 -07:00
|
|
|
}
|
2015-08-21 11:16:27 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-08 10:40:34 -08:00
|
|
|
public void setIsDisabled(boolean isDisabled) {
|
|
|
|
|
if (mIsDisabled != isDisabled) {
|
|
|
|
|
mIsDisabled = isDisabled;
|
2020-01-23 12:42:06 -08:00
|
|
|
updateFilter();
|
2016-11-08 10:40:34 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-29 10:49:43 -07:00
|
|
|
protected boolean isDisabled() {
|
|
|
|
|
return mIsDisabled;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-23 12:42:06 -08:00
|
|
|
private ColorFilter getDisabledColorFilter() {
|
|
|
|
|
if (sDisabledFColorFilter == null) {
|
|
|
|
|
ColorMatrix tempBrightnessMatrix = new ColorMatrix();
|
|
|
|
|
ColorMatrix tempFilterMatrix = new ColorMatrix();
|
|
|
|
|
|
|
|
|
|
tempFilterMatrix.setSaturation(1f - DISABLED_DESATURATION);
|
|
|
|
|
float scale = 1 - DISABLED_BRIGHTNESS;
|
|
|
|
|
int brightnessI = (int) (255 * DISABLED_BRIGHTNESS);
|
|
|
|
|
float[] mat = tempBrightnessMatrix.getArray();
|
|
|
|
|
mat[0] = scale;
|
|
|
|
|
mat[6] = scale;
|
|
|
|
|
mat[12] = scale;
|
|
|
|
|
mat[4] = brightnessI;
|
|
|
|
|
mat[9] = brightnessI;
|
|
|
|
|
mat[14] = brightnessI;
|
2020-06-05 12:17:16 -07:00
|
|
|
mat[18] = mDisabledAlpha;
|
2020-01-23 12:42:06 -08:00
|
|
|
tempFilterMatrix.preConcat(tempBrightnessMatrix);
|
|
|
|
|
sDisabledFColorFilter = new ColorMatrixColorFilter(tempFilterMatrix);
|
2014-08-04 10:53:22 -07:00
|
|
|
}
|
2020-01-23 12:42:06 -08:00
|
|
|
return sDisabledFColorFilter;
|
2015-08-21 11:16:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates the paint to reflect the current brightness and saturation.
|
|
|
|
|
*/
|
2018-06-07 17:31:48 -07:00
|
|
|
protected void updateFilter() {
|
2020-01-23 12:42:06 -08:00
|
|
|
mPaint.setColorFilter(mIsDisabled ? getDisabledColorFilter() : null);
|
2015-08-21 11:16:27 -07:00
|
|
|
invalidateSelf();
|
2014-07-14 12:02:01 -07:00
|
|
|
}
|
2018-02-23 12:19:44 -08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ConstantState getConstantState() {
|
2019-03-29 10:49:43 -07:00
|
|
|
return new MyConstantState(mBitmap, mIconColor, mIsDisabled);
|
2018-02-23 12:19:44 -08:00
|
|
|
}
|
|
|
|
|
|
2018-03-02 17:25:59 -08:00
|
|
|
protected static class MyConstantState extends ConstantState {
|
|
|
|
|
protected final Bitmap mBitmap;
|
|
|
|
|
protected final int mIconColor;
|
2019-03-29 10:49:43 -07:00
|
|
|
protected final boolean mIsDisabled;
|
2018-02-23 12:19:44 -08:00
|
|
|
|
2019-03-29 10:49:43 -07:00
|
|
|
public MyConstantState(Bitmap bitmap, int color, boolean isDisabled) {
|
2018-02-23 12:19:44 -08:00
|
|
|
mBitmap = bitmap;
|
|
|
|
|
mIconColor = color;
|
2019-03-29 10:49:43 -07:00
|
|
|
mIsDisabled = isDisabled;
|
2018-02-23 12:19:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2019-10-24 15:59:49 -07:00
|
|
|
public FastBitmapDrawable newDrawable() {
|
2019-03-29 10:49:43 -07:00
|
|
|
return new FastBitmapDrawable(mBitmap, mIconColor, mIsDisabled);
|
2018-02-23 12:19:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int getChangingConfigurations() {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-24 15:59:49 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Interface to be implemented by custom {@link BitmapInfo} to handle drawable construction
|
|
|
|
|
*/
|
|
|
|
|
public interface Factory {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Called to create a new drawable
|
|
|
|
|
*/
|
|
|
|
|
FastBitmapDrawable newDrawable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a FastBitmapDrawable with the icon.
|
|
|
|
|
*/
|
|
|
|
|
public static FastBitmapDrawable newIcon(Context context, ItemInfoWithIcon info) {
|
|
|
|
|
FastBitmapDrawable drawable = newIcon(context, info.bitmap);
|
|
|
|
|
drawable.setIsDisabled(info.isDisabled());
|
|
|
|
|
return drawable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a drawable for the provided BitmapInfo
|
|
|
|
|
*/
|
|
|
|
|
public static FastBitmapDrawable newIcon(Context context, BitmapInfo info) {
|
2020-06-05 12:17:16 -07:00
|
|
|
final FastBitmapDrawable drawable;
|
2019-10-24 15:59:49 -07:00
|
|
|
if (info instanceof Factory) {
|
2020-06-05 12:17:16 -07:00
|
|
|
drawable = ((Factory) info).newDrawable();
|
2019-10-24 15:59:49 -07:00
|
|
|
} else if (info.isLowRes()) {
|
2020-06-05 12:17:16 -07:00
|
|
|
drawable = new PlaceHolderIconDrawable(info, context);
|
2019-10-24 15:59:49 -07:00
|
|
|
} else {
|
2020-06-05 12:17:16 -07:00
|
|
|
drawable = new FastBitmapDrawable(info);
|
2019-10-24 15:59:49 -07:00
|
|
|
}
|
2020-06-05 12:17:16 -07:00
|
|
|
drawable.mDisabledAlpha = Themes.getFloat(context, R.attr.disabledIconAlpha, 1f);
|
|
|
|
|
return drawable;
|
2019-10-24 15:59:49 -07:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|