Merging ScrimView into GradientView

Less banding, less drawing for All Apps Transition.

Bug: 63873246

Change-Id: I6c7c856e2939a10db7e44b266c1d6d51334fd152
This commit is contained in:
Mario Bertschler
2017-07-24 14:41:23 -07:00
parent 8806475b8e
commit 254bd42729
14 changed files with 84 additions and 207 deletions

View File

@@ -28,7 +28,6 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
@@ -654,27 +653,4 @@ public final class Utilities {
return hashSet;
}
/**
* @return creates a new alpha mask bitmap out of an existing bitmap
*/
public static Bitmap convertToAlphaMask(Bitmap b, int applyAlpha) {
Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(a);
Paint paint = new Paint();
paint.setAlpha(applyAlpha);
c.drawBitmap(b, 0f, 0f, paint);
return a;
}
/**
* @return a new white 1x1 bitmap with ALPHA_8
*/
public static Bitmap createOnePixBitmap() {
Bitmap a = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(a);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
c.drawPaint(paint);
return a;
}
}

View File

@@ -25,7 +25,6 @@ import com.android.launcher3.Workspace;
import com.android.launcher3.anim.SpringAnimationHandler;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.graphics.GradientView;
import com.android.launcher3.graphics.ScrimView;
import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
import com.android.launcher3.util.SystemUiController;
@@ -100,7 +99,6 @@ public class AllAppsTransitionController implements TouchController, VerticalPul
private boolean mIsTranslateWithoutWorkspace = false;
private AnimatorSet mDiscoBounceAnimation;
private GradientView mGradientView;
private ScrimView mScrimView;
private SpringAnimationHandler mSpringAnimationHandler;
@@ -302,13 +300,6 @@ public class AllAppsTransitionController implements TouchController, VerticalPul
mGradientView.setVisibility(View.VISIBLE);
}
mGradientView.setProgress(progress);
// scrim
if (mScrimView == null) {
mScrimView = (ScrimView) mLauncher.findViewById(R.id.scrim_bg);
mScrimView.setVisibility(View.VISIBLE);
}
mScrimView.setProgress(progress);
}
/**

View File

@@ -18,13 +18,14 @@ package com.android.launcher3.graphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.v4.graphics.ColorUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
@@ -34,6 +35,7 @@ import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.dynamicui.WallpaperColorInfo;
import com.android.launcher3.util.Themes;
/**
* Draws a translucent radial gradient background from an initial state with progress 0.0 to a
@@ -42,44 +44,44 @@ import com.android.launcher3.dynamicui.WallpaperColorInfo;
public class GradientView extends View implements WallpaperColorInfo.OnChangeListener {
private static final int DEFAULT_COLOR = Color.WHITE;
private static final float GRADIENT_ALPHA_MASK_LENGTH_DP = 300;
private static final int ALPHA_MASK_HEIGHT_DP = 500;
private static final int ALPHA_MASK_WIDTH_DP = 2;
private static final int ALPHA_COLORS = 0xBF;
private static final boolean DEBUG = false;
private final Bitmap mFinalGradientMask;
private final Bitmap mAlphaGradientMask;
private boolean mShowScrim = true;
private int mColor1 = DEFAULT_COLOR;
private int mColor2 = DEFAULT_COLOR;
private int mWidth;
private int mHeight;
private final RectF mAlphaMaskRect = new RectF();
private final RectF mFinalMaskRect = new RectF();
private final Paint mPaint = new Paint();
private final Paint mPaintWithScrim = new Paint();
private final Paint mPaintNoScrim = new Paint();
private float mProgress;
private final int mMaskHeight;
private final int mMaskHeight, mMaskWidth;
private final Context mAppContext;
private final Paint mDebugPaint = DEBUG ? new Paint() : null;
private final Interpolator mAccelerator = new AccelerateInterpolator();
private final float mAlphaStart;
private final WallpaperColorInfo mWallpaperColorInfo;
private final int mScrimColor;
public GradientView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mAppContext = context.getApplicationContext();
this.mMaskHeight = Utilities.pxFromDp(GRADIENT_ALPHA_MASK_LENGTH_DP,
this.mMaskHeight = Utilities.pxFromDp(ALPHA_MASK_HEIGHT_DP,
mAppContext.getResources().getDisplayMetrics());
this.mMaskWidth = Utilities.pxFromDp(ALPHA_MASK_WIDTH_DP,
mAppContext.getResources().getDisplayMetrics());
Launcher launcher = Launcher.getLauncher(context);
this.mAlphaStart = launcher.getDeviceProfile().isVerticalBarLayout() ? 0 : 100;
this.mScrimColor = Themes.getAttrColor(context, R.attr.allAppsScrimColor);
this.mWallpaperColorInfo = WallpaperColorInfo.getInstance(launcher);
updateColors();
int finalAlpha = 0xBF;
mFinalGradientMask = Utilities.convertToAlphaMask(
Utilities.createOnePixBitmap(), finalAlpha);
Bitmap alphaMaskFromResource = BitmapFactory.decodeResource(context.getResources(),
R.drawable.all_apps_alpha_mask);
mAlphaGradientMask = Utilities.convertToAlphaMask(
alphaMaskFromResource, finalAlpha);
mAlphaGradientMask = createDitheredAlphaMask();
}
@Override
@@ -101,8 +103,10 @@ public class GradientView extends View implements WallpaperColorInfo.OnChangeLis
}
private void updateColors() {
this.mColor1 = mWallpaperColorInfo.getMainColor();
this.mColor2 = mWallpaperColorInfo.getSecondaryColor();
this.mColor1 = ColorUtils.setAlphaComponent(mWallpaperColorInfo.getMainColor(),
ALPHA_COLORS);
this.mColor2 = ColorUtils.setAlphaComponent(mWallpaperColorInfo.getSecondaryColor(),
ALPHA_COLORS);
if (mWidth + mHeight > 0) {
createRadialShader();
}
@@ -122,34 +126,53 @@ public class GradientView extends View implements WallpaperColorInfo.OnChangeLis
private void createRadialShader() {
final float gradientCenterY = 1.05f;
float radius = Math.max(mHeight, mWidth) * gradientCenterY;
float posScreenBottom = (radius - mHeight) / radius; // center lives below screen
RadialGradient shader = new RadialGradient(
RadialGradient shaderNoScrim = new RadialGradient(
mWidth * 0.5f,
mHeight * gradientCenterY,
radius,
new int[] {mColor1, mColor1, mColor2},
new float[] {0f, posScreenBottom, 1f},
Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mPaintNoScrim.setShader(shaderNoScrim);
int color1 = ColorUtils.compositeColors(mScrimColor,mColor1);
int color2 = ColorUtils.compositeColors(mScrimColor,mColor2);
RadialGradient shaderWithScrim = new RadialGradient(
mWidth * 0.5f,
mHeight * gradientCenterY,
radius,
new int[] { color1, color1, color2 },
new float[] {0f, posScreenBottom, 1f},
Shader.TileMode.CLAMP);
mPaintWithScrim.setShader(shaderWithScrim);
}
public void setProgress(float progress) {
setProgress(progress, true);
}
public void setProgress(float progress, boolean showScrim) {
this.mProgress = progress;
this.mShowScrim = showScrim;
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = mShowScrim ? mPaintWithScrim : mPaintNoScrim;
float head = 0.29f;
float linearProgress = head + (mProgress * (1f - head));
float startMaskY = (1f - linearProgress) * mHeight - mMaskHeight * linearProgress;
float interpolatedAlpha = (255 - mAlphaStart) * mAccelerator.getInterpolation(mProgress);
mPaint.setAlpha((int) (mAlphaStart + interpolatedAlpha));
mAlphaMaskRect.set(0, startMaskY, mWidth, startMaskY + mMaskHeight);
mFinalMaskRect.set(0, startMaskY + mMaskHeight, mWidth, mHeight);
canvas.drawBitmap(mAlphaGradientMask, null, mAlphaMaskRect, mPaint);
canvas.drawBitmap(mFinalGradientMask, null, mFinalMaskRect, mPaint);
paint.setAlpha((int) (mAlphaStart + interpolatedAlpha));
float div = (float) Math.floor(startMaskY + mMaskHeight);
mAlphaMaskRect.set(0, startMaskY, mWidth, div);
mFinalMaskRect.set(0, div, mWidth, mHeight);
canvas.drawBitmap(mAlphaGradientMask, null, mAlphaMaskRect, paint);
canvas.drawRect(mFinalMaskRect, paint);
if (DEBUG) {
mDebugPaint.setColor(0xFF00FF00);
@@ -157,4 +180,20 @@ public class GradientView extends View implements WallpaperColorInfo.OnChangeLis
canvas.drawLine(0, startMaskY + mMaskHeight, mWidth, startMaskY + mMaskHeight, mDebugPaint);
}
}
public Bitmap createDitheredAlphaMask() {
Bitmap dst = Bitmap.createBitmap(mMaskWidth, mMaskHeight, Bitmap.Config.ALPHA_8);
Canvas c = new Canvas(dst);
Paint paint = new Paint(Paint.DITHER_FLAG);
LinearGradient lg = new LinearGradient(0, 0, 0, mMaskHeight,
new int[]{
0x00FFFFFF,
ColorUtils.setAlphaComponent(Color.WHITE, (int) (0xFF * 0.95)),
0xFFFFFFFF},
new float[]{0f, 0.8f, 1f},
Shader.TileMode.CLAMP);
paint.setShader(lg);
c.drawRect(0, 0, mMaskWidth, mMaskHeight, paint);
return dst;
}
}

View File

@@ -1,115 +0,0 @@
/*
* Copyright (C) 2017 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.
*/
package com.android.launcher3.graphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.graphics.ColorUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.util.Themes;
public class ScrimView extends View {
private static final boolean DEBUG = false;
private static final int MASK_HEIGHT_DP = 300;
private static final float MASK_START_LENGTH_FACTOR = 1f;
private static final boolean APPLY_ALPHA = true;
private final Bitmap mFinalScrimMask;
private final Bitmap mAlphaScrimMask;
private final int mMaskHeight;
private int mVisibleHeight;
private final int mHeadStart;
private final RectF mAlphaMaskRect = new RectF();
private final RectF mFinalMaskRect = new RectF();
private final Paint mPaint = new Paint();
private float mProgress;
private final Interpolator mAccelerator = new AccelerateInterpolator();
private final Paint mDebugPaint = DEBUG ? new Paint() : null;
private final int mAlphaStart;
public ScrimView(Context context, AttributeSet attrs) {
super(context, attrs);
mMaskHeight = Utilities.pxFromDp(MASK_HEIGHT_DP, getResources().getDisplayMetrics());
mHeadStart = (int) (mMaskHeight * MASK_START_LENGTH_FACTOR);
mAlphaStart = Launcher.getLauncher(context)
.getDeviceProfile().isVerticalBarLayout() ? 0 : 55;
int scrimColor = Themes.getAttrColor(context, R.attr.allAppsScrimColor);
int scrimAlpha = Color.alpha(scrimColor);
mPaint.setColor(scrimColor);
mFinalScrimMask = Utilities.convertToAlphaMask(
Utilities.createOnePixBitmap(), scrimAlpha);
Bitmap alphaMaskFromResource = BitmapFactory.decodeResource(getResources(),
R.drawable.all_apps_alpha_mask);
mAlphaScrimMask = Utilities.convertToAlphaMask(alphaMaskFromResource, scrimAlpha);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
mVisibleHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, mVisibleHeight * 2);
setProgress(mProgress);
}
public void setProgress(float progress) {
mProgress = progress;
float initialY = mVisibleHeight - mHeadStart;
float fullTranslationY = mVisibleHeight;
float linTranslationY = initialY - progress * fullTranslationY;
setTranslationY(linTranslationY);
if (APPLY_ALPHA) {
int alpha = mAlphaStart + (int) ((255f - mAlphaStart)
* mAccelerator.getInterpolation(progress));
mPaint.setAlpha(alpha);
invalidate();
}
}
@Override
protected void onDraw(Canvas canvas) {
mAlphaMaskRect.set(0, 0, getWidth(), mMaskHeight);
mFinalMaskRect.set(0, mMaskHeight, getWidth(), getHeight());
canvas.drawBitmap(mAlphaScrimMask, null, mAlphaMaskRect, mPaint);
canvas.drawBitmap(mFinalScrimMask, null, mFinalMaskRect, mPaint);
if (DEBUG) {
mDebugPaint.setColor(0xFF0000FF);
canvas.drawLine(0, mAlphaMaskRect.top, getWidth(), mAlphaMaskRect.top, mDebugPaint);
canvas.drawLine(0, mAlphaMaskRect.bottom, getWidth(), mAlphaMaskRect.bottom, mDebugPaint);
}
}
}

View File

@@ -276,7 +276,9 @@ public class WidgetsBottomSheet extends AbstractFloatingView implements Insettab
public void setTranslationY(float translationY) {
super.setTranslationY(translationY);
if (mGradientBackground == null) return;
mGradientBackground.setProgress((mTranslationYClosed - translationY) / mTranslationYRange);
float p = (mTranslationYClosed - translationY) / mTranslationYRange;
boolean showScrim = p <= 0;
mGradientBackground.setProgress(p, showScrim);
}
@Override