Removing click-feedback shadow generation logic in BubbleTextView

Instead of animating the shadow, animating the icon scale. This avoids
unnecessary bitmap creating at app-launch and also plays nice with the
app-launch transition

Change-Id: I1d3d24bc7212a6d659855ff1002a45388e269e52
This commit is contained in:
Sunny Goyal
2018-03-05 12:54:24 -08:00
parent 85f1eed52d
commit 726bee7d5d
13 changed files with 68 additions and 522 deletions

View File

@@ -16,8 +16,9 @@
package com.android.launcher3;
import static com.android.launcher3.anim.Interpolators.ACCEL;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
@@ -28,9 +29,8 @@ import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Property;
import android.util.SparseArray;
@@ -38,14 +38,12 @@ import com.android.launcher3.graphics.BitmapInfo;
public class FastBitmapDrawable extends Drawable {
private static final float PRESSED_BRIGHTNESS = 100f / 255f;
private static final float PRESSED_SCALE = 1.1f;
private static final float DISABLED_DESATURATION = 1f;
private static final float DISABLED_BRIGHTNESS = 0.5f;
public static final TimeInterpolator CLICK_FEEDBACK_INTERPOLATOR = (input) ->
(input < 0.05f) ? (input / 0.05f) : ((input < 0.3f) ? 1 : (1 - input) / 0.7f);
public static final int CLICK_FEEDBACK_DURATION = 2000;
public static final int CLICK_FEEDBACK_DURATION = 200;
// Since we don't need 256^2 values for combinations of both the brightness and saturation, we
// reduce the value space to a smaller value V, which reduces the number of cached
@@ -66,18 +64,23 @@ public class FastBitmapDrawable extends Drawable {
private boolean mIsPressed;
private boolean mIsDisabled;
private static final Property<FastBitmapDrawable, Float> BRIGHTNESS
= new Property<FastBitmapDrawable, Float>(Float.TYPE, "brightness") {
// Animator and properties for the fast bitmap drawable's scale
private static final Property<FastBitmapDrawable, Float> SCALE
= new Property<FastBitmapDrawable, Float>(Float.TYPE, "scale") {
@Override
public Float get(FastBitmapDrawable fastBitmapDrawable) {
return fastBitmapDrawable.getBrightness();
return fastBitmapDrawable.mScale;
}
@Override
public void set(FastBitmapDrawable fastBitmapDrawable, Float value) {
fastBitmapDrawable.setBrightness(value);
fastBitmapDrawable.mScale = value;
fastBitmapDrawable.invalidateSelf();
}
};
private ObjectAnimator mScaleAnimation;
private float mScale = 1;
// The saturation and brightness are values that are mapped to REDUCED_FILTER_VALUE_SPACE and
// as a result, can be used to compose the key for the cached ColorMatrixColorFilters
@@ -86,9 +89,6 @@ public class FastBitmapDrawable extends Drawable {
private int mAlpha = 255;
private int mPrevUpdateKey = Integer.MAX_VALUE;
// Animators for the fast bitmap drawable's brightness
private ObjectAnimator mBrightnessAnimator;
public FastBitmapDrawable(Bitmap b) {
this(b, Color.TRANSPARENT);
}
@@ -108,8 +108,20 @@ public class FastBitmapDrawable extends Drawable {
}
@Override
public void draw(Canvas canvas) {
canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
public final void draw(Canvas canvas) {
if (mScaleAnimation != null) {
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);
}
@Override
@@ -138,6 +150,10 @@ public class FastBitmapDrawable extends Drawable {
return mAlpha;
}
public float getAnimatedScale() {
return mScaleAnimation == null ? 1 : mScale;
}
@Override
public int getIntrinsicWidth() {
return mBitmap.getWidth();
@@ -184,19 +200,20 @@ public class FastBitmapDrawable extends Drawable {
if (mIsPressed != isPressed) {
mIsPressed = isPressed;
if (mBrightnessAnimator != null) {
mBrightnessAnimator.cancel();
if (mScaleAnimation != null) {
mScaleAnimation.cancel();
mScaleAnimation = null;
}
if (mIsPressed) {
// Animate when going to pressed state
mBrightnessAnimator = ObjectAnimator.ofFloat(
this, BRIGHTNESS, getExpectedBrightness());
mBrightnessAnimator.setDuration(CLICK_FEEDBACK_DURATION);
mBrightnessAnimator.setInterpolator(CLICK_FEEDBACK_INTERPOLATOR);
mBrightnessAnimator.start();
mScaleAnimation = ObjectAnimator.ofFloat(this, SCALE, PRESSED_SCALE);
mScaleAnimation.setDuration(CLICK_FEEDBACK_DURATION);
mScaleAnimation.setInterpolator(ACCEL);
mScaleAnimation.start();
} else {
setBrightness(getExpectedBrightness());
mScale = 1f;
invalidateSelf();
}
return true;
}
@@ -205,12 +222,7 @@ public class FastBitmapDrawable extends Drawable {
private void invalidateDesaturationAndBrightness() {
setDesaturation(mIsDisabled ? DISABLED_DESATURATION : 0);
setBrightness(getExpectedBrightness());
}
private float getExpectedBrightness() {
return mIsDisabled ? DISABLED_BRIGHTNESS :
(mIsPressed ? PRESSED_BRIGHTNESS : 0);
setBrightness(mIsDisabled ? DISABLED_BRIGHTNESS : 0);
}
public void setIsDisabled(boolean isDisabled) {