Skip to content

Commit

Permalink
[New API] Added override for system animations enabled behavior (#1747)
Browse files Browse the repository at this point in the history
Allow overriding the default behavior of respecting when system animations are disabled.
XML: app:lottie_ignoreDisabledSystemAnimations="true"
Code: setIgnoreDisabledSystemAnimations(boolean)
  • Loading branch information
IljaKosynkin authored Mar 13, 2021
1 parent 81ad033 commit 0809f91
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
18 changes: 18 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ private void init(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
setRenderMode(RenderMode.values()[renderModeOrdinal]);
}

setIgnoreDisabledSystemAnimations(
ta.getBoolean(
R.styleable.LottieAnimationView_lottie_ignoreDisabledSystemAnimations,
false
)
);

ta.recycle();

lottieDrawable.setSystemAnimationsAreEnabled(Utils.getAnimationScale(getContext()) != 0f);
Expand Down Expand Up @@ -353,6 +360,17 @@ protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onDetachedFromWindow();
}

/**
* Allows ignoring system animations settings, therefore allowing animations to run even if they are disabled.
*
* Defaults to false.
*
* @param ignore if true animations will run even when they are disabled in the system settings.
*/
public void setIgnoreDisabledSystemAnimations(boolean ignore) {
lottieDrawable.setIgnoreDisabledSystemAnimations(ignore);
}

/**
* Enable this to get merge path support for devices running KitKat (19) and above.
*
Expand Down
27 changes: 23 additions & 4 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ private interface LazyCompositionTask {
private LottieComposition composition;
private final LottieValueAnimator animator = new LottieValueAnimator();
private float scale = 1f;

//Call animationsEnabled() instead of using these fields directly
private boolean systemAnimationsEnabled = true;
private boolean ignoreSystemAnimationsDisabled = false;

private boolean safeMode = false;

private final ArrayList<LazyCompositionTask> lazyCompositionTasks = new ArrayList<>();
Expand Down Expand Up @@ -445,10 +449,10 @@ public void run(LottieComposition composition) {
return;
}

if (systemAnimationsEnabled || getRepeatCount() == 0) {
if (animationsEnabled() || getRepeatCount() == 0) {
animator.playAnimation();
}
if (!systemAnimationsEnabled) {
if (!animationsEnabled()) {
setFrame((int) (getSpeed() < 0 ? getMinFrame() : getMaxFrame()));
animator.endAnimation();
}
Expand Down Expand Up @@ -476,10 +480,10 @@ public void run(LottieComposition composition) {
return;
}

if (systemAnimationsEnabled || getRepeatCount() == 0) {
if (animationsEnabled() || getRepeatCount() == 0) {
animator.resumeAnimation();
}
if (!systemAnimationsEnabled) {
if (!animationsEnabled()) {
setFrame((int) (getSpeed() < 0 ? getMinFrame() : getMaxFrame()));
animator.endAnimation();
}
Expand Down Expand Up @@ -866,12 +870,27 @@ public boolean isAnimating() {
return animator.isRunning();
}

private boolean animationsEnabled() {
return systemAnimationsEnabled || ignoreSystemAnimationsDisabled;
}

void setSystemAnimationsAreEnabled(Boolean areEnabled) {
systemAnimationsEnabled = areEnabled;
}

// </editor-fold>

/**
* Allows ignoring system animations settings, therefore allowing animations to run even if they are disabled.
*
* Defaults to false.
*
* @param ignore if true animations will run even when they are disabled in the system settings.
*/
public void setIgnoreDisabledSystemAnimations(boolean ignore) {
ignoreSystemAnimationsDisabled = ignore;
}

/**
* Set the scale on the current composition. The only cost of this function is re-rendering the
* current frame so you may call it frequent to scale something up or down.
Expand Down
1 change: 1 addition & 0 deletions lottie/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<attr name="lottie_scale" format="float" />
<attr name="lottie_speed" format="float" />
<attr name="lottie_cacheComposition" format="boolean" />
<attr name="lottie_ignoreDisabledSystemAnimations" format="boolean" />
<!-- These values must be kept in sync with the RenderMode enum -->
<attr name="lottie_renderMode" format="enum">
<enum name="automatic" value="0" />
Expand Down

0 comments on commit 0809f91

Please sign in to comment.