/
LottieAnimationView.java
1252 lines (1115 loc) · 45.6 KB
/
LottieAnimationView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.airbnb.lottie;
import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.FloatRange;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import androidx.annotation.RequiresApi;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.view.ViewCompat;
import com.airbnb.lottie.model.KeyPath;
import com.airbnb.lottie.utils.Logger;
import com.airbnb.lottie.utils.Utils;
import com.airbnb.lottie.value.LottieFrameInfo;
import com.airbnb.lottie.value.LottieValueCallback;
import com.airbnb.lottie.value.SimpleLottieValueCallback;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
/**
* This view will load, deserialize, and display an After Effects animation exported with
* bodymovin (https://github.com/bodymovin/bodymovin).
* <p>
* You may set the animation in one of two ways:
* 1) Attrs: {@link R.styleable#LottieAnimationView_lottie_fileName}
* 2) Programmatically:
* {@link #setAnimation(String)}
* {@link #setAnimation(int)}
* {@link #setAnimation(InputStream, String)}
* {@link #setAnimationFromJson(String, String)}
* {@link #setAnimationFromUrl(String)}
* {@link #setComposition(LottieComposition)}
* <p>
* You can set a default cache strategy with {@link R.attr#lottie_cacheComposition}.
* <p>
* You can manually set the progress of the animation with {@link #setProgress(float)} or
* {@link R.attr#lottie_progress}
*
* @see <a href="http://airbnb.io/lottie">Full Documentation</a>
*/
@SuppressWarnings({"WeakerAccess"}) public class LottieAnimationView extends AppCompatImageView {
private static final String TAG = LottieAnimationView.class.getSimpleName();
private static final LottieListener<Throwable> DEFAULT_FAILURE_LISTENER = new LottieListener<Throwable>() {
@Override public void onResult(Throwable throwable) {
// By default, fail silently for network errors.
if (Utils.isNetworkException(throwable)) {
Logger.warning("Unable to load composition.", throwable);
return;
}
throw new IllegalStateException("Unable to parse composition", throwable);
}
};
private final LottieListener<LottieComposition> loadedListener = new LottieListener<LottieComposition>() {
@Override public void onResult(LottieComposition composition) {
setComposition(composition);
}
};
private final LottieListener<Throwable> wrappedFailureListener = new LottieListener<Throwable>() {
@Override
public void onResult(Throwable result) {
if (fallbackResource != 0) {
setImageResource(fallbackResource);
}
LottieListener<Throwable> l = failureListener == null ? DEFAULT_FAILURE_LISTENER : failureListener;
l.onResult(result);
}
};
@Nullable private LottieListener<Throwable> failureListener;
@DrawableRes private int fallbackResource = 0;
private final LottieDrawable lottieDrawable = new LottieDrawable();
private boolean isInitialized;
private String animationName;
private @RawRes int animationResId;
private boolean playAnimationWhenShown = false;
private boolean wasAnimatingWhenNotShown = false;
private boolean wasAnimatingWhenDetached = false;
/**
* When we set a new composition, we set LottieDrawable to null then back again so that ImageView re-checks its bounds.
* However, this causes the drawable to get unscheduled briefly. Normally, we would pause the animation but in this case, we don't want to.
*/
private boolean ignoreUnschedule = false;
private boolean autoPlay = false;
private boolean cacheComposition = true;
private RenderMode renderMode = RenderMode.AUTOMATIC;
private boolean useSoftwareRendering = false;
private final Set<LottieOnCompositionLoadedListener> lottieOnCompositionLoadedListeners = new HashSet<>();
/**
* Prevents a StackOverflowException on 4.4 in which getDrawingCache() calls buildDrawingCache().
* This isn't a great solution but it works and has very little performance overhead.
* At some point in the future, the original goal of falling back to hardware rendering when
* the animation is set to software rendering but it is too large to fit in a software bitmap
* should be reevaluated.
*/
private int buildDrawingCacheDepth = 0;
@Nullable private LottieTask<LottieComposition> compositionTask;
/**
* Can be null because it is created async
*/
@Nullable private LottieComposition composition;
public LottieAnimationView(Context context) {
super(context);
init(null, R.attr.lottieAnimationViewStyle);
}
public LottieAnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, R.attr.lottieAnimationViewStyle);
}
public LottieAnimationView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}
private void init(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.LottieAnimationView, defStyleAttr, 0);
cacheComposition = ta.getBoolean(R.styleable.LottieAnimationView_lottie_cacheComposition, true);
boolean hasRawRes = ta.hasValue(R.styleable.LottieAnimationView_lottie_rawRes);
boolean hasFileName = ta.hasValue(R.styleable.LottieAnimationView_lottie_fileName);
boolean hasUrl = ta.hasValue(R.styleable.LottieAnimationView_lottie_url);
if (hasRawRes && hasFileName) {
throw new IllegalArgumentException("lottie_rawRes and lottie_fileName cannot be used at " +
"the same time. Please use only one at once.");
} else if (hasRawRes) {
int rawResId = ta.getResourceId(R.styleable.LottieAnimationView_lottie_rawRes, 0);
if (rawResId != 0) {
setAnimation(rawResId);
}
} else if (hasFileName) {
String fileName = ta.getString(R.styleable.LottieAnimationView_lottie_fileName);
if (fileName != null) {
setAnimation(fileName);
}
} else if (hasUrl) {
String url = ta.getString(R.styleable.LottieAnimationView_lottie_url);
if (url != null) {
setAnimationFromUrl(url);
}
}
setFallbackResource(ta.getResourceId(R.styleable.LottieAnimationView_lottie_fallbackRes, 0));
if (ta.getBoolean(R.styleable.LottieAnimationView_lottie_autoPlay, false)) {
wasAnimatingWhenDetached = true;
autoPlay = true;
}
if (ta.getBoolean(R.styleable.LottieAnimationView_lottie_loop, false)) {
lottieDrawable.setRepeatCount(LottieDrawable.INFINITE);
}
if (ta.hasValue(R.styleable.LottieAnimationView_lottie_repeatMode)) {
setRepeatMode(ta.getInt(R.styleable.LottieAnimationView_lottie_repeatMode,
LottieDrawable.RESTART));
}
if (ta.hasValue(R.styleable.LottieAnimationView_lottie_repeatCount)) {
setRepeatCount(ta.getInt(R.styleable.LottieAnimationView_lottie_repeatCount,
LottieDrawable.INFINITE));
}
if (ta.hasValue(R.styleable.LottieAnimationView_lottie_speed)) {
setSpeed(ta.getFloat(R.styleable.LottieAnimationView_lottie_speed, 1f));
}
setImageAssetsFolder(ta.getString(R.styleable.LottieAnimationView_lottie_imageAssetsFolder));
setProgress(ta.getFloat(R.styleable.LottieAnimationView_lottie_progress, 0));
enableMergePathsForKitKatAndAbove(ta.getBoolean(
R.styleable.LottieAnimationView_lottie_enableMergePathsForKitKatAndAbove, false));
if (ta.hasValue(R.styleable.LottieAnimationView_lottie_colorFilter)) {
int colorRes = ta.getResourceId(R.styleable.LottieAnimationView_lottie_colorFilter, -1);
ColorStateList csl = AppCompatResources.getColorStateList(getContext(), colorRes);
SimpleColorFilter filter = new SimpleColorFilter(csl.getDefaultColor());
KeyPath keyPath = new KeyPath("**");
LottieValueCallback<ColorFilter> callback = new LottieValueCallback<ColorFilter>(filter);
addValueCallback(keyPath, LottieProperty.COLOR_FILTER, callback);
}
if (ta.hasValue(R.styleable.LottieAnimationView_lottie_scale)) {
lottieDrawable.setScale(ta.getFloat(R.styleable.LottieAnimationView_lottie_scale, 1f));
}
if (ta.hasValue(R.styleable.LottieAnimationView_lottie_renderMode)) {
int renderModeOrdinal = ta.getInt(R.styleable.LottieAnimationView_lottie_renderMode, RenderMode.AUTOMATIC.ordinal());
if (renderModeOrdinal >= RenderMode.values().length) {
renderModeOrdinal = RenderMode.AUTOMATIC.ordinal();
}
setRenderMode(RenderMode.values()[renderModeOrdinal]);
}
setIgnoreDisabledSystemAnimations(
ta.getBoolean(
R.styleable.LottieAnimationView_lottie_ignoreDisabledSystemAnimations,
false
)
);
ta.recycle();
lottieDrawable.setSystemAnimationsAreEnabled(Utils.getAnimationScale(getContext()) != 0f);
computeRenderMode();
isInitialized = true;
}
@Override public void setImageResource(int resId) {
cancelLoaderTask();
super.setImageResource(resId);
}
@Override public void setImageDrawable(Drawable drawable) {
cancelLoaderTask();
super.setImageDrawable(drawable);
}
@Override public void setImageBitmap(Bitmap bm) {
cancelLoaderTask();
super.setImageBitmap(bm);
}
@Override public void unscheduleDrawable(Drawable who) {
if (!ignoreUnschedule && who == lottieDrawable && lottieDrawable.isAnimating()) {
pauseAnimation();
} else if (!ignoreUnschedule && who instanceof LottieDrawable && ((LottieDrawable) who).isAnimating()) {
((LottieDrawable) who).pauseAnimation();
}
super.unscheduleDrawable(who);
}
@Override public void invalidateDrawable(@NonNull Drawable dr) {
if (getDrawable() == lottieDrawable) {
// We always want to invalidate the root drawable so it redraws the whole drawable.
// Eventually it would be great to be able to invalidate just the changed region.
super.invalidateDrawable(lottieDrawable);
} else {
// Otherwise work as regular ImageView
super.invalidateDrawable(dr);
}
}
@Override protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.animationName = animationName;
ss.animationResId = animationResId;
ss.progress = lottieDrawable.getProgress();
ss.isAnimating = lottieDrawable.isAnimating() || (!ViewCompat.isAttachedToWindow(this) && wasAnimatingWhenDetached);
ss.imageAssetsFolder = lottieDrawable.getImageAssetsFolder();
ss.repeatMode = lottieDrawable.getRepeatMode();
ss.repeatCount = lottieDrawable.getRepeatCount();
return ss;
}
@Override protected void onRestoreInstanceState(Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
animationName = ss.animationName;
if (!TextUtils.isEmpty(animationName)) {
setAnimation(animationName);
}
animationResId = ss.animationResId;
if (animationResId != 0) {
setAnimation(animationResId);
}
setProgress(ss.progress);
if (ss.isAnimating) {
playAnimation();
}
lottieDrawable.setImagesAssetsFolder(ss.imageAssetsFolder);
setRepeatMode(ss.repeatMode);
setRepeatCount(ss.repeatCount);
}
@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
// This can happen on older versions of Android because onVisibilityChanged gets called from the
// constructor of View so this will get called before lottieDrawable gets initialized.
// https://github.com/airbnb/lottie-android/issues/1143
// A simple null check on lottieDrawable would not work because when using Proguard optimization, a
// null check on a final field gets removed. As "usually" final fields cannot be null.
// However because this is called by super (View) before the initializer of the LottieAnimationView
// is called, it actually can be null here.
// Working around this by using a non final boolean that is set to true after the class initializer
// has run.
if (!isInitialized) {
return;
}
if (isShown()) {
if (wasAnimatingWhenNotShown) {
resumeAnimation();
} else if (playAnimationWhenShown) {
playAnimation();
}
wasAnimatingWhenNotShown = false;
playAnimationWhenShown = false;
} else {
if (isAnimating()) {
pauseAnimation();
wasAnimatingWhenNotShown = true;
}
}
}
@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!isInEditMode() && (autoPlay || wasAnimatingWhenDetached)) {
playAnimation();
// Autoplay from xml should only apply once.
autoPlay = false;
wasAnimatingWhenDetached = false;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// This is needed to mimic newer platform behavior.
// https://stackoverflow.com/a/53625860/715633
onVisibilityChanged(this, getVisibility());
}
}
@Override protected void onDetachedFromWindow() {
if (isAnimating()) {
cancelAnimation();
wasAnimatingWhenDetached = true;
}
super.onDetachedFromWindow();
}
/**
* Allows ignoring system animations settings, therefore allowing animations to run even if they are disabled.
* <p>
* 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.
* <p>
* Merge paths currently don't work if the the operand shape is entirely contained within the
* first shape. If you need to cut out one shape from another shape, use an even-odd fill type
* instead of using merge paths.
*/
public void enableMergePathsForKitKatAndAbove(boolean enable) {
lottieDrawable.enableMergePathsForKitKatAndAbove(enable);
}
/**
* Returns whether merge paths are enabled for KitKat and above.
*/
public boolean isMergePathsEnabledForKitKatAndAbove() {
return lottieDrawable.isMergePathsEnabledForKitKatAndAbove();
}
/**
* If set to true, all future compositions that are set will be cached so that they don't need to be parsed
* next time they are loaded. This won't apply to compositions that have already been loaded.
* <p>
* Defaults to true.
* <p>
* {@link R.attr#lottie_cacheComposition}
*/
public void setCacheComposition(boolean cacheComposition) {
this.cacheComposition = cacheComposition;
}
/**
* Enable this to debug slow animations by outlining masks and mattes. The performance overhead of the masks and mattes will
* be proportional to the surface area of all of the masks/mattes combined.
* <p>
* DO NOT leave this enabled in production.
*/
public void setOutlineMasksAndMattes(boolean outline) {
lottieDrawable.setOutlineMasksAndMattes(outline);
}
/**
* Sets the animation from a file in the raw directory.
* This will load and deserialize the file asynchronously.
*/
public void setAnimation(@RawRes final int rawRes) {
this.animationResId = rawRes;
animationName = null;
setCompositionTask(fromRawRes(rawRes));
}
private LottieTask<LottieComposition> fromRawRes(@RawRes final int rawRes) {
if (isInEditMode()) {
return new LottieTask<>(new Callable<LottieResult<LottieComposition>>() {
@Override public LottieResult<LottieComposition> call() {
return cacheComposition
? LottieCompositionFactory.fromRawResSync(getContext(), rawRes) : LottieCompositionFactory.fromRawResSync(getContext(), rawRes, null);
}
}, true);
} else {
return cacheComposition ?
LottieCompositionFactory.fromRawRes(getContext(), rawRes) : LottieCompositionFactory.fromRawRes(getContext(), rawRes, null);
}
}
public void setAnimation(final String assetName) {
this.animationName = assetName;
animationResId = 0;
setCompositionTask(fromAssets(assetName));
}
private LottieTask<LottieComposition> fromAssets(final String assetName) {
if (isInEditMode()) {
return new LottieTask<>(new Callable<LottieResult<LottieComposition>>() {
@Override public LottieResult<LottieComposition> call() {
return cacheComposition ?
LottieCompositionFactory.fromAssetSync(getContext(), assetName) : LottieCompositionFactory.fromAssetSync(getContext(), assetName, null);
}
}, true);
} else {
return cacheComposition ?
LottieCompositionFactory.fromAsset(getContext(), assetName) : LottieCompositionFactory.fromAsset(getContext(), assetName, null);
}
}
/**
* @see #setAnimationFromJson(String, String)
*/
@Deprecated
public void setAnimationFromJson(String jsonString) {
setAnimationFromJson(jsonString, null);
}
/**
* Sets the animation from json string. This is the ideal API to use when loading an animation
* over the network because you can use the raw response body here and a conversion to a
* JSONObject never has to be done.
*/
public void setAnimationFromJson(String jsonString, @Nullable String cacheKey) {
setAnimation(new ByteArrayInputStream(jsonString.getBytes()), cacheKey);
}
/**
* Sets the animation from an arbitrary InputStream.
* This will load and deserialize the file asynchronously.
* <p>
* This is particularly useful for animations loaded from the network. You can fetch the
* bodymovin json from the network and pass it directly here.
*/
public void setAnimation(InputStream stream, @Nullable String cacheKey) {
setCompositionTask(LottieCompositionFactory.fromJsonInputStream(stream, cacheKey));
}
/**
* Load a lottie animation from a url. The url can be a json file or a zip file. Use a zip file if you have images. Simply zip them together and
* lottie
* will unzip and link the images automatically.
* <p>
* Under the hood, Lottie uses Java HttpURLConnection because it doesn't require any transitive networking dependencies. It will download the file
* to the application cache under a temporary name. If the file successfully parses to a composition, it will rename the temporary file to one that
* can be accessed immediately for subsequent requests. If the file does not parse to a composition, the temporary file will be deleted.
* <p>
* You can replace the default network stack or cache handling with a global {@link LottieConfig}
*
* @see LottieConfig.Builder
* @see Lottie#initialize(LottieConfig)
*/
public void setAnimationFromUrl(String url) {
LottieTask<LottieComposition> task = cacheComposition ?
LottieCompositionFactory.fromUrl(getContext(), url) : LottieCompositionFactory.fromUrl(getContext(), url, null);
setCompositionTask(task);
}
/**
* Load a lottie animation from a url. The url can be a json file or a zip file. Use a zip file if you have images. Simply zip them together and
* lottie
* will unzip and link the images automatically.
* <p>
* Under the hood, Lottie uses Java HttpURLConnection because it doesn't require any transitive networking dependencies. It will download the file
* to the application cache under a temporary name. If the file successfully parses to a composition, it will rename the temporary file to one that
* can be accessed immediately for subsequent requests. If the file does not parse to a composition, the temporary file will be deleted.
* <p>
* You can replace the default network stack or cache handling with a global {@link LottieConfig}
*
* @see LottieConfig.Builder
* @see Lottie#initialize(LottieConfig)
*/
public void setAnimationFromUrl(String url, @Nullable String cacheKey) {
LottieTask<LottieComposition> task = LottieCompositionFactory.fromUrl(getContext(), url, cacheKey);
setCompositionTask(task);
}
/**
* Set a default failure listener that will be called if any of the setAnimation APIs fail for any reason.
* This can be used to replace the default behavior.
* <p>
* The default behavior will log any network errors and rethrow all other exceptions.
* <p>
* If you are loading an animation from the network, errors may occur if your user has no internet.
* You can use this listener to retry the download or you can have it default to an error drawable
* with {@link #setFallbackResource(int)}.
* <p>
* Unless you are using {@link #setAnimationFromUrl(String)}, errors are unexpected.
* <p>
* Set the listener to null to revert to the default behavior.
*/
public void setFailureListener(@Nullable LottieListener<Throwable> failureListener) {
this.failureListener = failureListener;
}
/**
* Set a drawable that will be rendered if the LottieComposition fails to load for any reason.
* Unless you are using {@link #setAnimationFromUrl(String)}, this is an unexpected error and
* you should handle it with {@link #setFailureListener(LottieListener)}.
* <p>
* If this is a network animation, you may use this to show an error to the user or
* you can use a failure listener to retry the download.
*/
public void setFallbackResource(@DrawableRes int fallbackResource) {
this.fallbackResource = fallbackResource;
}
private void setCompositionTask(LottieTask<LottieComposition> compositionTask) {
clearComposition();
cancelLoaderTask();
this.compositionTask = compositionTask
.addListener(loadedListener)
.addFailureListener(wrappedFailureListener);
}
private void cancelLoaderTask() {
if (compositionTask != null) {
compositionTask.removeListener(loadedListener);
compositionTask.removeFailureListener(wrappedFailureListener);
}
}
/**
* Sets a composition.
* You can set a default cache strategy if this view was inflated with xml by
* using {@link R.attr#lottie_cacheComposition}.
*/
public void setComposition(@NonNull LottieComposition composition) {
if (L.DBG) {
Log.v(TAG, "Set Composition \n" + composition);
}
lottieDrawable.setCallback(this);
this.composition = composition;
ignoreUnschedule = true;
boolean isNewComposition = lottieDrawable.setComposition(composition);
ignoreUnschedule = false;
computeRenderMode();
if (getDrawable() == lottieDrawable && !isNewComposition) {
// We can avoid re-setting the drawable, and invalidating the view, since the composition
// hasn't changed.
return;
} else if (!isNewComposition) {
// The current drawable isn't lottieDrawable but the drawable already has the right composition.
setLottieDrawable();
}
// This is needed to makes sure that the animation is properly played/paused for the current visibility state.
// It is possible that the drawable had a lazy composition task to play the animation but this view subsequently
// became invisible. Comment this out and run the espresso tests to see a failing test.
onVisibilityChanged(this, getVisibility());
requestLayout();
for (LottieOnCompositionLoadedListener lottieOnCompositionLoadedListener : lottieOnCompositionLoadedListeners) {
lottieOnCompositionLoadedListener.onCompositionLoaded(composition);
}
}
@Nullable public LottieComposition getComposition() {
return composition;
}
/**
* Returns whether or not any layers in this composition has masks.
*/
public boolean hasMasks() {
return lottieDrawable.hasMasks();
}
/**
* Returns whether or not any layers in this composition has a matte layer.
*/
public boolean hasMatte() {
return lottieDrawable.hasMatte();
}
/**
* Plays the animation from the beginning. If speed is {@literal <} 0, it will start at the end
* and play towards the beginning
*/
@MainThread
public void playAnimation() {
if (isShown()) {
lottieDrawable.playAnimation();
computeRenderMode();
} else {
playAnimationWhenShown = true;
}
}
/**
* Continues playing the animation from its current position. If speed {@literal <} 0, it will play backwards
* from the current position.
*/
@MainThread
public void resumeAnimation() {
if (isShown()) {
lottieDrawable.resumeAnimation();
computeRenderMode();
} else {
playAnimationWhenShown = false;
wasAnimatingWhenNotShown = true;
}
}
/**
* Sets the minimum frame that the animation will start from when playing or looping.
*/
public void setMinFrame(int startFrame) {
lottieDrawable.setMinFrame(startFrame);
}
/**
* Returns the minimum frame set by {@link #setMinFrame(int)} or {@link #setMinProgress(float)}
*/
public float getMinFrame() {
return lottieDrawable.getMinFrame();
}
/**
* Sets the minimum progress that the animation will start from when playing or looping.
*/
public void setMinProgress(float startProgress) {
lottieDrawable.setMinProgress(startProgress);
}
/**
* Sets the maximum frame that the animation will end at when playing or looping.
* <p>
* The value will be clamped to the composition bounds. For example, setting Integer.MAX_VALUE would result in the same
* thing as composition.endFrame.
*/
public void setMaxFrame(int endFrame) {
lottieDrawable.setMaxFrame(endFrame);
}
/**
* Returns the maximum frame set by {@link #setMaxFrame(int)} or {@link #setMaxProgress(float)}
*/
public float getMaxFrame() {
return lottieDrawable.getMaxFrame();
}
/**
* Sets the maximum progress that the animation will end at when playing or looping.
*/
public void setMaxProgress(@FloatRange(from = 0f, to = 1f) float endProgress) {
lottieDrawable.setMaxProgress(endProgress);
}
/**
* Sets the minimum frame to the start time of the specified marker.
*
* @throws IllegalArgumentException if the marker is not found.
*/
public void setMinFrame(String markerName) {
lottieDrawable.setMinFrame(markerName);
}
/**
* Sets the maximum frame to the start time + duration of the specified marker.
*
* @throws IllegalArgumentException if the marker is not found.
*/
public void setMaxFrame(String markerName) {
lottieDrawable.setMaxFrame(markerName);
}
/**
* Sets the minimum and maximum frame to the start time and start time + duration
* of the specified marker.
*
* @throws IllegalArgumentException if the marker is not found.
*/
public void setMinAndMaxFrame(String markerName) {
lottieDrawable.setMinAndMaxFrame(markerName);
}
/**
* Sets the minimum and maximum frame to the start marker start and the maximum frame to the end marker start.
* playEndMarkerStartFrame determines whether or not to play the frame that the end marker is on. If the end marker
* represents the end of the section that you want, it should be true. If the marker represents the beginning of the
* next section, it should be false.
*
* @throws IllegalArgumentException if either marker is not found.
*/
public void setMinAndMaxFrame(final String startMarkerName, final String endMarkerName, final boolean playEndMarkerStartFrame) {
lottieDrawable.setMinAndMaxFrame(startMarkerName, endMarkerName, playEndMarkerStartFrame);
}
/**
* @see #setMinFrame(int)
* @see #setMaxFrame(int)
*/
public void setMinAndMaxFrame(int minFrame, int maxFrame) {
lottieDrawable.setMinAndMaxFrame(minFrame, maxFrame);
}
/**
* @see #setMinProgress(float)
* @see #setMaxProgress(float)
*/
public void setMinAndMaxProgress(
@FloatRange(from = 0f, to = 1f) float minProgress,
@FloatRange(from = 0f, to = 1f) float maxProgress) {
lottieDrawable.setMinAndMaxProgress(minProgress, maxProgress);
}
/**
* Reverses the current animation speed. This does NOT play the animation.
*
* @see #setSpeed(float)
* @see #playAnimation()
* @see #resumeAnimation()
*/
public void reverseAnimationSpeed() {
lottieDrawable.reverseAnimationSpeed();
}
/**
* Sets the playback speed. If speed {@literal <} 0, the animation will play backwards.
*/
public void setSpeed(float speed) {
lottieDrawable.setSpeed(speed);
}
/**
* Returns the current playback speed. This will be {@literal <} 0 if the animation is playing backwards.
*/
public float getSpeed() {
return lottieDrawable.getSpeed();
}
public void addAnimatorUpdateListener(ValueAnimator.AnimatorUpdateListener updateListener) {
lottieDrawable.addAnimatorUpdateListener(updateListener);
}
public void removeUpdateListener(ValueAnimator.AnimatorUpdateListener updateListener) {
lottieDrawable.removeAnimatorUpdateListener(updateListener);
}
public void removeAllUpdateListeners() {
lottieDrawable.removeAllUpdateListeners();
}
public void addAnimatorListener(Animator.AnimatorListener listener) {
lottieDrawable.addAnimatorListener(listener);
}
public void removeAnimatorListener(Animator.AnimatorListener listener) {
lottieDrawable.removeAnimatorListener(listener);
}
public void removeAllAnimatorListeners() {
lottieDrawable.removeAllAnimatorListeners();
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void addAnimatorPauseListener(Animator.AnimatorPauseListener listener) {
lottieDrawable.addAnimatorPauseListener(listener);
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void removeAnimatorPauseListener(Animator.AnimatorPauseListener listener) {
lottieDrawable.removeAnimatorPauseListener(listener);
}
/**
* @see #setRepeatCount(int)
*/
@Deprecated
public void loop(boolean loop) {
lottieDrawable.setRepeatCount(loop ? ValueAnimator.INFINITE : 0);
}
/**
* Defines what this animation should do when it reaches the end. This
* setting is applied only when the repeat count is either greater than
* 0 or {@link LottieDrawable#INFINITE}. Defaults to {@link LottieDrawable#RESTART}.
*
* @param mode {@link LottieDrawable#RESTART} or {@link LottieDrawable#REVERSE}
*/
public void setRepeatMode(@LottieDrawable.RepeatMode int mode) {
lottieDrawable.setRepeatMode(mode);
}
/**
* Defines what this animation should do when it reaches the end.
*
* @return either one of {@link LottieDrawable#REVERSE} or {@link LottieDrawable#RESTART}
*/
@LottieDrawable.RepeatMode
public int getRepeatMode() {
return lottieDrawable.getRepeatMode();
}
/**
* Sets how many times the animation should be repeated. If the repeat
* count is 0, the animation is never repeated. If the repeat count is
* greater than 0 or {@link LottieDrawable#INFINITE}, the repeat mode will be taken
* into account. The repeat count is 0 by default.
*
* @param count the number of times the animation should be repeated
*/
public void setRepeatCount(int count) {
lottieDrawable.setRepeatCount(count);
}
/**
* Defines how many times the animation should repeat. The default value
* is 0.
*
* @return the number of times the animation should repeat, or {@link LottieDrawable#INFINITE}
*/
public int getRepeatCount() {
return lottieDrawable.getRepeatCount();
}
public boolean isAnimating() {
return lottieDrawable.isAnimating();
}
/**
* If you use image assets, you must explicitly specify the folder in assets/ in which they are
* located because bodymovin uses the name filenames across all compositions (img_#).
* Do NOT rename the images themselves.
* <p>
* If your images are located in src/main/assets/airbnb_loader/ then call
* `setImageAssetsFolder("airbnb_loader/");`.
* <p>
* Be wary if you are using many images, however. Lottie is designed to work with vector shapes
* from After Effects. If your images look like they could be represented with vector shapes,
* see if it is possible to convert them to shape layers and re-export your animation. Check
* the documentation at http://airbnb.io/lottie for more information about importing shapes from
* Sketch or Illustrator to avoid this.
*/
public void setImageAssetsFolder(String imageAssetsFolder) {
lottieDrawable.setImagesAssetsFolder(imageAssetsFolder);
}
@Nullable
public String getImageAssetsFolder() {
return lottieDrawable.getImageAssetsFolder();
}
/**
* When true, dynamically set bitmaps will be drawn at the size of the original bitmap.
* When false, dynamically set bitmaps will be drawn at 0,0 at the original bitmap but at whatever size the dynamic bitmap is.
*
* Defaults to false.
*/
public void setRescaleBitmaps(boolean rescaleBitmaps) {
lottieDrawable.setRescaleBitmaps(rescaleBitmaps);
}
/**
* When true, dynamically set bitmaps will be drawn at the size of the original bitmap.
* When false, dynamically set bitmaps will be drawn at 0,0 at the original bitmap but at whatever size the dynamic bitmap is.
*
* Defaults to false.
*/
public boolean getRescaleBitmaps() {
return lottieDrawable.getRescaleBitmaps();
}
/**
* Allows you to modify or clear a bitmap that was loaded for an image either automatically
* through {@link #setImageAssetsFolder(String)} or with an {@link ImageAssetDelegate}.
*
* @return the previous Bitmap or null.
*/
@Nullable
public Bitmap updateBitmap(String id, @Nullable Bitmap bitmap) {
return lottieDrawable.updateBitmap(id, bitmap);
}
/**
* Use this if you can't bundle images with your app. This may be useful if you download the
* animations from the network or have the images saved to an SD Card. In that case, Lottie
* will defer the loading of the bitmap to this delegate.
* <p>
* Be wary if you are using many images, however. Lottie is designed to work with vector shapes
* from After Effects. If your images look like they could be represented with vector shapes,
* see if it is possible to convert them to shape layers and re-export your animation. Check
* the documentation at http://airbnb.io/lottie for more information about importing shapes from
* Sketch or Illustrator to avoid this.
*/
public void setImageAssetDelegate(ImageAssetDelegate assetDelegate) {
lottieDrawable.setImageAssetDelegate(assetDelegate);
}
/**
* Use this to manually set fonts.
*/
public void setFontAssetDelegate(FontAssetDelegate assetDelegate) {
lottieDrawable.setFontAssetDelegate(assetDelegate);
}
/**
* Set this to replace animation text with custom text at runtime
*/
public void setTextDelegate(TextDelegate textDelegate) {
lottieDrawable.setTextDelegate(textDelegate);
}
/**
* Takes a {@link KeyPath}, potentially with wildcards or globstars and resolve it to a list of
* zero or more actual {@link KeyPath Keypaths} that exist in the current animation.
* <p>
* If you want to set value callbacks for any of these values, it is recommended to use the
* returned {@link KeyPath} objects because they will be internally resolved to their content
* and won't trigger a tree walk of the animation contents when applied.
*/
public List<KeyPath> resolveKeyPath(KeyPath keyPath) {
return lottieDrawable.resolveKeyPath(keyPath);
}
/**
* Add a property callback for the specified {@link KeyPath}. This {@link KeyPath} can resolve
* to multiple contents. In that case, the callback's value will apply to all of them.
* <p>
* Internally, this will check if the {@link KeyPath} has already been resolved with
* {@link #resolveKeyPath(KeyPath)} and will resolve it if it hasn't.
*/
public <T> void addValueCallback(KeyPath keyPath, T property, LottieValueCallback<T> callback) {
lottieDrawable.addValueCallback(keyPath, property, callback);
}
/**
* Overload of {@link #addValueCallback(KeyPath, Object, LottieValueCallback)} that takes an interface. This allows you to use a single abstract
* method code block in Kotlin such as:
* animationView.addValueCallback(yourKeyPath, LottieProperty.COLOR) { yourColor }
*/
public <T> void addValueCallback(KeyPath keyPath, T property,
final SimpleLottieValueCallback<T> callback) {
lottieDrawable.addValueCallback(keyPath, property, new LottieValueCallback<T>() {
@Override public T getValue(LottieFrameInfo<T> frameInfo) {
return callback.getValue(frameInfo);
}
});
}
/**
* 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.
* <p>
* The smaller the animation is, the better the performance will be. You may find that scaling an
* animation down then rendering it in a larger ImageView and letting ImageView scale it back up
* with a scaleType such as centerInside will yield better performance with little perceivable
* quality loss.