diff options
Diffstat (limited to 'src/com/android/launcher3/util')
7 files changed, 216 insertions, 118 deletions
diff --git a/src/com/android/launcher3/util/CircleRevealOutlineProvider.java b/src/com/android/launcher3/util/CircleRevealOutlineProvider.java new file mode 100644 index 000000000..c19212019 --- /dev/null +++ b/src/com/android/launcher3/util/CircleRevealOutlineProvider.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2016 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.util; + +import android.annotation.TargetApi; +import android.os.Build; + +@TargetApi(Build.VERSION_CODES.LOLLIPOP) +public class CircleRevealOutlineProvider extends RevealOutlineAnimation { + + private int mCenterX; + private int mCenterY; + private float mRadius0; + private float mRadius1; + + /** + * @param x reveal center x + * @param y reveal center y + * @param r0 initial radius + * @param r1 final radius + */ + public CircleRevealOutlineProvider(int x, int y, float r0, float r1) { + mCenterX = x; + mCenterY = y; + mRadius0 = r0; + mRadius1 = r1; + } + + @Override + public boolean shouldRemoveElevationDuringAnimation() { + return true; + } + + @Override + public void setProgress(float progress) { + mOutlineRadius = (1 - progress) * mRadius0 + progress * mRadius1; + + mOutline.left = (int) (mCenterX - mOutlineRadius); + mOutline.top = (int) (mCenterY - mOutlineRadius); + mOutline.right = (int) (mCenterX + mOutlineRadius); + mOutline.bottom = (int) (mCenterY + mOutlineRadius); + } +} diff --git a/src/com/android/launcher3/util/ComponentKey.java b/src/com/android/launcher3/util/ComponentKey.java index b7aafaea9..144b411fa 100644 --- a/src/com/android/launcher3/util/ComponentKey.java +++ b/src/com/android/launcher3/util/ComponentKey.java @@ -18,6 +18,7 @@ package com.android.launcher3.util; import android.content.ComponentName; import android.content.Context; + import com.android.launcher3.compat.UserHandleCompat; import com.android.launcher3.compat.UserManagerCompat; @@ -60,17 +61,6 @@ public class ComponentKey { mHashCode = Arrays.hashCode(new Object[] {componentName, user}); } - /** - * Encodes a component key as a string of the form [flattenedComponentString#userId]. - */ - public String flattenToString(Context context) { - String flattened = componentName.flattenToString(); - if (user != null) { - flattened += "#" + UserManagerCompat.getInstance(context).getSerialNumberForUser(user); - } - return flattened; - } - @Override public int hashCode() { return mHashCode; @@ -81,4 +71,12 @@ public class ComponentKey { ComponentKey other = (ComponentKey) o; return other.componentName.equals(componentName) && other.user.equals(user); } + + /** + * Encodes a component key as a string of the form [flattenedComponentString#userId]. + */ + @Override + public String toString() { + return componentName.flattenToString() + "#" + user; + } }
\ No newline at end of file diff --git a/src/com/android/launcher3/util/IconNormalizer.java b/src/com/android/launcher3/util/IconNormalizer.java index 4087d7bcf..040a1b51a 100644 --- a/src/com/android/launcher3/util/IconNormalizer.java +++ b/src/com/android/launcher3/util/IconNormalizer.java @@ -19,6 +19,7 @@ package com.android.launcher3.util; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.RectF; import android.graphics.drawable.Drawable; import com.android.launcher3.LauncherAppState; @@ -74,8 +75,10 @@ public class IconNormalizer { * * This closeness is used to determine the ratio of hull area to the full icon size. * Refer {@link #MAX_CIRCLE_AREA_FACTOR} and {@link #MAX_SQUARE_AREA_FACTOR} + * + * @param outBounds optional rect to receive the fraction distance from each edge. */ - public synchronized float getScale(Drawable d) { + public synchronized float getScale(Drawable d, RectF outBounds) { int width = d.getIntrinsicWidth(); int height = d.getIntrinsicHeight(); if (width <= 0 || height <= 0) { @@ -168,6 +171,14 @@ public class IconNormalizer { scaleRequired = MAX_SQUARE_AREA_FACTOR + LINEAR_SCALE_SLOPE * (1 - hullByRect); } + if (outBounds != null) { + outBounds.left = ((float) leftX) / width; + outBounds.right = 1 - ((float) rightX) / width; + + outBounds.top = ((float) topY) / height; + outBounds.bottom = 1 - ((float) bottomY) / height; + } + float areaScale = area / (width * height); // Use sqrt of the final ratio as the images is scaled across both width and height. float scale = areaScale > scaleRequired ? (float) Math.sqrt(scaleRequired / areaScale) : 1; diff --git a/src/com/android/launcher3/util/PillRevealOutlineProvider.java b/src/com/android/launcher3/util/PillRevealOutlineProvider.java new file mode 100644 index 000000000..09ff9bda4 --- /dev/null +++ b/src/com/android/launcher3/util/PillRevealOutlineProvider.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2016 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.util; + +import android.annotation.TargetApi; +import android.graphics.Rect; +import android.os.Build; +import android.view.ViewOutlineProvider; + +@TargetApi(Build.VERSION_CODES.LOLLIPOP) +/** + * A {@link ViewOutlineProvider} that animates a reveal in a "pill" shape. + * A pill is simply a round rect, but we assume the width is greater than + * the height and that the radius is equal to half the height. + */ +public class PillRevealOutlineProvider extends RevealOutlineAnimation { + + private int mCenterX; + private int mCenterY; + private Rect mPillRect; + + /** + * @param x reveal center x + * @param y reveal center y + * @param pillRect round rect that represents the final pill shape + * @param pillRectRadius radius of the round rect + */ + public PillRevealOutlineProvider(int x, int y, Rect pillRect, float pillRectRadius) { + mCenterX = x; + mCenterY = y; + mPillRect = pillRect; + mOutlineRadius = pillRectRadius; + } + + @Override + public boolean shouldRemoveElevationDuringAnimation() { + return false; + } + + @Override + public void setProgress(float progress) { + // Assumes width is greater than height. + int centerToEdge = Math.max(mCenterX, mPillRect.width() - mCenterX); + int currentSize = (int) (progress * centerToEdge); + + // Bound the outline to the final pill shape defined by mPillRect. + mOutline.left = Math.max(mPillRect.left, mCenterX - currentSize); + mOutline.top = Math.max(mPillRect.top, mCenterY - currentSize); + mOutline.right = Math.min(mPillRect.right, mCenterX + currentSize); + mOutline.bottom = Math.min(mPillRect.bottom, mCenterY + currentSize); + } +} diff --git a/src/com/android/launcher3/util/RevealOutlineAnimation.java b/src/com/android/launcher3/util/RevealOutlineAnimation.java new file mode 100644 index 000000000..4447c3ba9 --- /dev/null +++ b/src/com/android/launcher3/util/RevealOutlineAnimation.java @@ -0,0 +1,72 @@ +package com.android.launcher3.util; + +import android.animation.Animator; +import android.animation.AnimatorListenerAdapter; +import android.animation.ValueAnimator; +import android.graphics.Outline; +import android.graphics.Rect; +import android.view.View; +import android.view.ViewOutlineProvider; + +import com.android.launcher3.Utilities; + +/** + * A {@link ViewOutlineProvider} that has helper functions to create reveal animations. + * This class should be extended so that subclasses can define the reveal shape as the + * animation progresses from 0 to 1. + */ +public abstract class RevealOutlineAnimation extends ViewOutlineProvider { + protected Rect mOutline; + protected float mOutlineRadius; + + public RevealOutlineAnimation() { + mOutline = new Rect(); + } + + /** Returns whether elevation should be removed for the duration of the reveal animation. */ + abstract boolean shouldRemoveElevationDuringAnimation(); + /** Sets the progress, from 0 to 1, of the reveal animation. */ + abstract void setProgress(float progress); + + public ValueAnimator createRevealAnimator(final View revealView) { + ValueAnimator va = ValueAnimator.ofFloat(0f, 1f); + final float elevation = revealView.getElevation(); + + va.addListener(new AnimatorListenerAdapter() { + public void onAnimationStart(Animator animation) { + revealView.setOutlineProvider(RevealOutlineAnimation.this); + revealView.setClipToOutline(true); + if (shouldRemoveElevationDuringAnimation()) { + revealView.setTranslationZ(-elevation); + } + } + + public void onAnimationEnd(Animator animation) { + revealView.setOutlineProvider(ViewOutlineProvider.BACKGROUND); + revealView.setClipToOutline(false); + if (shouldRemoveElevationDuringAnimation()) { + revealView.setTranslationZ(0); + } + } + + }); + + va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator arg0) { + float progress = arg0.getAnimatedFraction(); + setProgress(progress); + revealView.invalidateOutline(); + if (!Utilities.ATLEAST_LOLLIPOP_MR1) { + revealView.invalidate(); + } + } + }); + return va; + } + + @Override + public void getOutline(View v, Outline outline) { + outline.setRoundRect(mOutline, mOutlineRadius); + } +} diff --git a/src/com/android/launcher3/util/RevealOutlineProvider.java b/src/com/android/launcher3/util/RevealOutlineProvider.java deleted file mode 100644 index 0db3984f8..000000000 --- a/src/com/android/launcher3/util/RevealOutlineProvider.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.android.launcher3.util; - -import android.annotation.TargetApi; -import android.graphics.Outline; -import android.graphics.Rect; -import android.os.Build; -import android.view.View; -import android.view.ViewOutlineProvider; - -@TargetApi(Build.VERSION_CODES.LOLLIPOP) -public class RevealOutlineProvider extends ViewOutlineProvider { - - private int mCenterX; - private int mCenterY; - private float mRadius0; - private float mRadius1; - private int mCurrentRadius; - - private final Rect mOval; - - /** - * @param x reveal center x - * @param y reveal center y - * @param r0 initial radius - * @param r1 final radius - */ - public RevealOutlineProvider(int x, int y, float r0, float r1) { - mCenterX = x; - mCenterY = y; - mRadius0 = r0; - mRadius1 = r1; - - mOval = new Rect(); - } - - public void setProgress(float progress) { - mCurrentRadius = (int) ((1 - progress) * mRadius0 + progress * mRadius1); - - mOval.left = mCenterX - mCurrentRadius; - mOval.top = mCenterY - mCurrentRadius; - mOval.right = mCenterX + mCurrentRadius; - mOval.bottom = mCenterY + mCurrentRadius; - } - - @Override - public void getOutline(View v, Outline outline) { - outline.setRoundRect(mOval, mCurrentRadius); - } -} diff --git a/src/com/android/launcher3/util/UiThreadCircularReveal.java b/src/com/android/launcher3/util/UiThreadCircularReveal.java deleted file mode 100644 index f2b5e5e15..000000000 --- a/src/com/android/launcher3/util/UiThreadCircularReveal.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.android.launcher3.util; - -import android.animation.Animator; -import android.animation.AnimatorListenerAdapter; -import android.animation.ValueAnimator; -import android.animation.ValueAnimator.AnimatorUpdateListener; -import android.annotation.TargetApi; -import android.os.Build; -import android.view.View; -import android.view.ViewOutlineProvider; - -import com.android.launcher3.Utilities; - -@TargetApi(Build.VERSION_CODES.LOLLIPOP) -public class UiThreadCircularReveal { - - public static ValueAnimator createCircularReveal(View v, int x, int y, float r0, float r1) { - return createCircularReveal(v, x, y, r0, r1, ViewOutlineProvider.BACKGROUND); - } - - public static ValueAnimator createCircularReveal(View v, int x, int y, float r0, float r1, - final ViewOutlineProvider originalProvider) { - ValueAnimator va = ValueAnimator.ofFloat(0f, 1f); - - final View revealView = v; - final RevealOutlineProvider outlineProvider = new RevealOutlineProvider(x, y, r0, r1); - final float elevation = v.getElevation(); - - va.addListener(new AnimatorListenerAdapter() { - public void onAnimationStart(Animator animation) { - revealView.setOutlineProvider(outlineProvider); - revealView.setClipToOutline(true); - revealView.setTranslationZ(-elevation); - } - - public void onAnimationEnd(Animator animation) { - revealView.setOutlineProvider(originalProvider); - revealView.setClipToOutline(false); - revealView.setTranslationZ(0); - } - - }); - - va.addUpdateListener(new AnimatorUpdateListener() { - @Override - public void onAnimationUpdate(ValueAnimator arg0) { - float progress = arg0.getAnimatedFraction(); - outlineProvider.setProgress(progress); - revealView.invalidateOutline(); - if (!Utilities.ATLEAST_LOLLIPOP_MR1) { - revealView.invalidate(); - } - } - }); - return va; - } -} |