From 901b641c4d3237f4131e58601976d9731b740f1f Mon Sep 17 00:00:00 2001 From: ankur2136 Date: Thu, 21 Nov 2019 13:54:44 -0800 Subject: [PATCH] Add support for start delay in shimmer library. (#92) Summary: Shimmer library right now only supports repeat delay but not start delay while ValueAnimator supports both. This is useful to stagger the shimmer animations in case there are multiple ShimmerFrameLayouts in a view. Pull Request resolved: https://github.com/facebook/shimmer-android/pull/92 Differential Revision: D18642161 Pulled By: xiphirx fbshipit-source-id: d30e13194d27d7a12e6a01dc79b9f876679de8a4 --- .../main/java/com/facebook/shimmer/Shimmer.java | 17 ++++++++++++++++- .../com/facebook/shimmer/ShimmerDrawable.java | 1 + shimmer/src/main/res/values/attrs.xml | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/shimmer/src/main/java/com/facebook/shimmer/Shimmer.java b/shimmer/src/main/java/com/facebook/shimmer/Shimmer.java index 492f9ee..6b158d3 100644 --- a/shimmer/src/main/java/com/facebook/shimmer/Shimmer.java +++ b/shimmer/src/main/java/com/facebook/shimmer/Shimmer.java @@ -78,6 +78,7 @@ public class Shimmer { int repeatMode = ValueAnimator.RESTART; long animationDuration = 1000L; long repeatDelay; + long startDelay; Shimmer() {} @@ -179,7 +180,11 @@ T consumeAttributes(TypedArray a) { setRepeatMode( a.getInt(R.styleable.ShimmerFrameLayout_shimmer_repeat_mode, mShimmer.repeatMode)); } - + if (a.hasValue(R.styleable.ShimmerFrameLayout_shimmer_start_delay)) { + setStartDelay( + a.getInt( + R.styleable.ShimmerFrameLayout_shimmer_start_delay, (int) mShimmer.startDelay)); + } if (a.hasValue(R.styleable.ShimmerFrameLayout_shimmer_direction)) { int direction = a.getInt(R.styleable.ShimmerFrameLayout_shimmer_direction, mShimmer.direction); @@ -260,6 +265,7 @@ public T copyFrom(Shimmer other) { setRepeatCount(other.repeatCount); setRepeatMode(other.repeatMode); setRepeatDelay(other.repeatDelay); + setStartDelay(other.startDelay); setDuration(other.animationDuration); mShimmer.baseColor = other.baseColor; mShimmer.highlightColor = other.highlightColor; @@ -399,6 +405,15 @@ public T setRepeatDelay(long millis) { return getThis(); } + /** Sets how long to wait for starting the shimmering animation. */ + public T setStartDelay(long millis) { + if (millis < 0) { + throw new IllegalArgumentException("Given a negative start delay: " + millis); + } + mShimmer.startDelay = millis; + return getThis(); + } + /** Sets how long the shimmering animation takes to do one full sweep. */ public T setDuration(long millis) { if (millis < 0) { diff --git a/shimmer/src/main/java/com/facebook/shimmer/ShimmerDrawable.java b/shimmer/src/main/java/com/facebook/shimmer/ShimmerDrawable.java index b1aaae5..2a0ba1d 100644 --- a/shimmer/src/main/java/com/facebook/shimmer/ShimmerDrawable.java +++ b/shimmer/src/main/java/com/facebook/shimmer/ShimmerDrawable.java @@ -163,6 +163,7 @@ private void updateValueAnimator() { mValueAnimator = ValueAnimator.ofFloat(0f, 1f + (float) (mShimmer.repeatDelay / mShimmer.animationDuration)); mValueAnimator.setRepeatMode(mShimmer.repeatMode); + mValueAnimator.setStartDelay(mShimmer.startDelay); mValueAnimator.setRepeatCount(mShimmer.repeatCount); mValueAnimator.setDuration(mShimmer.animationDuration + mShimmer.repeatDelay); mValueAnimator.addUpdateListener(mUpdateListener); diff --git a/shimmer/src/main/res/values/attrs.xml b/shimmer/src/main/res/values/attrs.xml index e5d5280..d954a0e 100644 --- a/shimmer/src/main/res/values/attrs.xml +++ b/shimmer/src/main/res/values/attrs.xml @@ -15,6 +15,7 @@ +