Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added override for system animations enabled behavior #1747

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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