288 lines
9.4 KiB
Java
288 lines
9.4 KiB
Java
package com.airbnb.lottie.utils;
|
|
|
|
import android.view.Choreographer;
|
|
import com.airbnb.lottie.L;
|
|
import com.airbnb.lottie.LottieComposition;
|
|
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class LottieValueAnimator extends BaseLottieAnimator implements Choreographer.FrameCallback {
|
|
private LottieComposition composition;
|
|
private float speed = 1.0f;
|
|
private boolean speedReversedForRepeatMode = false;
|
|
private long lastFrameTimeNs = 0;
|
|
private float frame = BitmapDescriptorFactory.HUE_RED;
|
|
private int repeatCount = 0;
|
|
private float minFrame = -2.1474836E9f;
|
|
private float maxFrame = 2.1474836E9f;
|
|
protected boolean running = false;
|
|
|
|
@Override // android.animation.ValueAnimator
|
|
public Object getAnimatedValue() {
|
|
return Float.valueOf(getAnimatedValueAbsolute());
|
|
}
|
|
|
|
public float getAnimatedValueAbsolute() {
|
|
LottieComposition lottieComposition = this.composition;
|
|
return lottieComposition == null ? BitmapDescriptorFactory.HUE_RED : (this.frame - lottieComposition.getStartFrame()) / (this.composition.getEndFrame() - this.composition.getStartFrame());
|
|
}
|
|
|
|
@Override // android.animation.ValueAnimator
|
|
public float getAnimatedFraction() {
|
|
float minFrame;
|
|
float maxFrame;
|
|
float minFrame2;
|
|
if (this.composition == null) {
|
|
return BitmapDescriptorFactory.HUE_RED;
|
|
}
|
|
if (isReversed()) {
|
|
minFrame = getMaxFrame() - this.frame;
|
|
maxFrame = getMaxFrame();
|
|
minFrame2 = getMinFrame();
|
|
} else {
|
|
minFrame = this.frame - getMinFrame();
|
|
maxFrame = getMaxFrame();
|
|
minFrame2 = getMinFrame();
|
|
}
|
|
return minFrame / (maxFrame - minFrame2);
|
|
}
|
|
|
|
@Override // android.animation.ValueAnimator, android.animation.Animator
|
|
public long getDuration() {
|
|
if (this.composition == null) {
|
|
return 0L;
|
|
}
|
|
return r0.getDuration();
|
|
}
|
|
|
|
@Override // android.view.Choreographer.FrameCallback
|
|
public void doFrame(long j) {
|
|
postFrameCallback();
|
|
if (this.composition == null || !isRunning()) {
|
|
return;
|
|
}
|
|
L.beginSection("LottieValueAnimator#doFrame");
|
|
long j2 = this.lastFrameTimeNs;
|
|
float frameDurationNs = ((float) (j2 != 0 ? j - j2 : 0L)) / getFrameDurationNs();
|
|
float f = this.frame;
|
|
if (isReversed()) {
|
|
frameDurationNs = -frameDurationNs;
|
|
}
|
|
float f2 = f + frameDurationNs;
|
|
this.frame = f2;
|
|
boolean contains = MiscUtils.contains(f2, getMinFrame(), getMaxFrame());
|
|
this.frame = MiscUtils.clamp(this.frame, getMinFrame(), getMaxFrame());
|
|
this.lastFrameTimeNs = j;
|
|
notifyUpdate();
|
|
if (!contains) {
|
|
if (getRepeatCount() != -1 && this.repeatCount >= getRepeatCount()) {
|
|
this.frame = this.speed < BitmapDescriptorFactory.HUE_RED ? getMinFrame() : getMaxFrame();
|
|
removeFrameCallback();
|
|
notifyEnd(isReversed());
|
|
} else {
|
|
notifyRepeat();
|
|
this.repeatCount++;
|
|
if (getRepeatMode() == 2) {
|
|
this.speedReversedForRepeatMode = !this.speedReversedForRepeatMode;
|
|
reverseAnimationSpeed();
|
|
} else {
|
|
this.frame = isReversed() ? getMaxFrame() : getMinFrame();
|
|
}
|
|
this.lastFrameTimeNs = j;
|
|
}
|
|
}
|
|
verifyFrame();
|
|
L.endSection("LottieValueAnimator#doFrame");
|
|
}
|
|
|
|
private float getFrameDurationNs() {
|
|
LottieComposition lottieComposition = this.composition;
|
|
if (lottieComposition == null) {
|
|
return Float.MAX_VALUE;
|
|
}
|
|
return (1.0E9f / lottieComposition.getFrameRate()) / Math.abs(this.speed);
|
|
}
|
|
|
|
public void setComposition(LottieComposition lottieComposition) {
|
|
boolean z = this.composition == null;
|
|
this.composition = lottieComposition;
|
|
if (z) {
|
|
setMinAndMaxFrames(Math.max(this.minFrame, lottieComposition.getStartFrame()), Math.min(this.maxFrame, lottieComposition.getEndFrame()));
|
|
} else {
|
|
setMinAndMaxFrames((int) lottieComposition.getStartFrame(), (int) lottieComposition.getEndFrame());
|
|
}
|
|
float f = this.frame;
|
|
this.frame = BitmapDescriptorFactory.HUE_RED;
|
|
setFrame((int) f);
|
|
notifyUpdate();
|
|
}
|
|
|
|
public void setFrame(float f) {
|
|
if (this.frame == f) {
|
|
return;
|
|
}
|
|
this.frame = MiscUtils.clamp(f, getMinFrame(), getMaxFrame());
|
|
this.lastFrameTimeNs = 0L;
|
|
notifyUpdate();
|
|
}
|
|
|
|
public void setMinFrame(int i) {
|
|
setMinAndMaxFrames(i, (int) this.maxFrame);
|
|
}
|
|
|
|
public void setMaxFrame(float f) {
|
|
setMinAndMaxFrames(this.minFrame, f);
|
|
}
|
|
|
|
public void setMinAndMaxFrames(float f, float f2) {
|
|
if (f > f2) {
|
|
throw new IllegalArgumentException(String.format("minFrame (%s) must be <= maxFrame (%s)", Float.valueOf(f), Float.valueOf(f2)));
|
|
}
|
|
LottieComposition lottieComposition = this.composition;
|
|
float startFrame = lottieComposition == null ? -3.4028235E38f : lottieComposition.getStartFrame();
|
|
LottieComposition lottieComposition2 = this.composition;
|
|
float endFrame = lottieComposition2 == null ? Float.MAX_VALUE : lottieComposition2.getEndFrame();
|
|
float clamp = MiscUtils.clamp(f, startFrame, endFrame);
|
|
float clamp2 = MiscUtils.clamp(f2, startFrame, endFrame);
|
|
if (clamp == this.minFrame && clamp2 == this.maxFrame) {
|
|
return;
|
|
}
|
|
this.minFrame = clamp;
|
|
this.maxFrame = clamp2;
|
|
setFrame((int) MiscUtils.clamp(this.frame, clamp, clamp2));
|
|
}
|
|
|
|
public void reverseAnimationSpeed() {
|
|
setSpeed(-getSpeed());
|
|
}
|
|
|
|
@Override // android.animation.ValueAnimator
|
|
public void setRepeatMode(int i) {
|
|
super.setRepeatMode(i);
|
|
if (i == 2 || !this.speedReversedForRepeatMode) {
|
|
return;
|
|
}
|
|
this.speedReversedForRepeatMode = false;
|
|
reverseAnimationSpeed();
|
|
}
|
|
|
|
public void playAnimation() {
|
|
this.running = true;
|
|
notifyStart(isReversed());
|
|
setFrame((int) (isReversed() ? getMaxFrame() : getMinFrame()));
|
|
this.lastFrameTimeNs = 0L;
|
|
this.repeatCount = 0;
|
|
postFrameCallback();
|
|
}
|
|
|
|
public void endAnimation() {
|
|
removeFrameCallback();
|
|
notifyEnd(isReversed());
|
|
}
|
|
|
|
public void pauseAnimation() {
|
|
removeFrameCallback();
|
|
}
|
|
|
|
public void resumeAnimation() {
|
|
this.running = true;
|
|
postFrameCallback();
|
|
this.lastFrameTimeNs = 0L;
|
|
if (isReversed() && getFrame() == getMinFrame()) {
|
|
this.frame = getMaxFrame();
|
|
} else {
|
|
if (isReversed() || getFrame() != getMaxFrame()) {
|
|
return;
|
|
}
|
|
this.frame = getMinFrame();
|
|
}
|
|
}
|
|
|
|
@Override // android.animation.ValueAnimator, android.animation.Animator
|
|
public void cancel() {
|
|
notifyCancel();
|
|
removeFrameCallback();
|
|
}
|
|
|
|
private boolean isReversed() {
|
|
return getSpeed() < BitmapDescriptorFactory.HUE_RED;
|
|
}
|
|
|
|
public float getMinFrame() {
|
|
LottieComposition lottieComposition = this.composition;
|
|
if (lottieComposition == null) {
|
|
return BitmapDescriptorFactory.HUE_RED;
|
|
}
|
|
float f = this.minFrame;
|
|
return f == -2.1474836E9f ? lottieComposition.getStartFrame() : f;
|
|
}
|
|
|
|
public float getMaxFrame() {
|
|
LottieComposition lottieComposition = this.composition;
|
|
if (lottieComposition == null) {
|
|
return BitmapDescriptorFactory.HUE_RED;
|
|
}
|
|
float f = this.maxFrame;
|
|
return f == 2.1474836E9f ? lottieComposition.getEndFrame() : f;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
@Override // com.airbnb.lottie.utils.BaseLottieAnimator
|
|
public void notifyCancel() {
|
|
super.notifyCancel();
|
|
notifyEnd(isReversed());
|
|
}
|
|
|
|
protected void postFrameCallback() {
|
|
if (isRunning()) {
|
|
removeFrameCallback(false);
|
|
Choreographer.getInstance().postFrameCallback(this);
|
|
}
|
|
}
|
|
|
|
protected void removeFrameCallback() {
|
|
removeFrameCallback(true);
|
|
}
|
|
|
|
protected void removeFrameCallback(boolean z) {
|
|
Choreographer.getInstance().removeFrameCallback(this);
|
|
if (z) {
|
|
this.running = false;
|
|
}
|
|
}
|
|
|
|
private void verifyFrame() {
|
|
if (this.composition == null) {
|
|
return;
|
|
}
|
|
float f = this.frame;
|
|
float f2 = this.minFrame;
|
|
if (f < f2 || f > this.maxFrame) {
|
|
throw new IllegalStateException(String.format("Frame must be [%f,%f]. It is %f", Float.valueOf(f2), Float.valueOf(this.maxFrame), Float.valueOf(f)));
|
|
}
|
|
}
|
|
|
|
public void setSpeed(float f) {
|
|
this.speed = f;
|
|
}
|
|
|
|
@Override // android.animation.ValueAnimator, android.animation.Animator
|
|
public boolean isRunning() {
|
|
return this.running;
|
|
}
|
|
|
|
public float getSpeed() {
|
|
return this.speed;
|
|
}
|
|
|
|
public float getFrame() {
|
|
return this.frame;
|
|
}
|
|
|
|
public void clearComposition() {
|
|
this.composition = null;
|
|
this.minFrame = -2.1474836E9f;
|
|
this.maxFrame = 2.1474836E9f;
|
|
}
|
|
}
|