lottie 动画框架  https://github.com/airbnb/lottie-android

<com.airbnb.lottie.LottieAnimationView        android:id="@+id/animation_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:lottie_fileName="hello-world.json"        app:lottie_loop="true"        app:lottie_autoPlay="true" />
LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);animationView.setAnimation("hello-world.json");animationView.loop(true);animationView.playAnimation();
 LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view); ... Cancellable compositionCancellable = LottieComposition.Factory.fromJson(getResources(), jsonObject, (composition) -> {     animationView.setComposition(composition);     animationView.playAnimation(); });
animationView.addAnimatorUpdateListener((animation) -> {    // Do something.});animationView.playAnimation();...if (animationView.isAnimating()) {    // Do something.}...animationView.setProgress(0.5f);...// Custom animation speed or duration.ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f)    .setDuration(500);animator.addUpdateListener(animation -> {    animationView.setProgress(animation.getAnimatedValue());});animator.start();...animationView.cancelAnimation();

You can add a color filter to the whole animation, a specific layer, or specific content within a layer:

// Any class that conforms to the ColorFilter interfacefinal PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.LIGHTEN);// Adding a color filter to the whole viewanimationView.addColorFilter(colorFilter);// Adding a color filter to a specific layeranimationView.addColorFilterToLayer("hello_layer", colorFilter);// Adding a color filter to specfic content on the "hello_layer"animationView.addColorFilterToContent("hello_layer", "hello", colorFilter);// Clear all color filtersanimationView.clearColorFilters();

You can also add a color filter to the whole animation in the layout XML, which will be applied with PorterDuff.Mode.SRC_ATOP:

<com.airbnb.lottie.LottieAnimationView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:lottie_fileName="hello-world.json"        app:lottie_colorFilter="@color/blue" />

Note: Color filters are only available for layers such as Image layer and Solid layer as well as content that includes fill, stroke, or group content.

Under the hood, LottieAnimationView uses LottieDrawable to render its animations. If you need to, you can use the drawable form directly:

LottieDrawable drawable = new LottieDrawable();LottieComposition.Factory.fromAssetFileName(getContext(), "hello-world.json", (composition) -> {    drawable.setComposition(composition);});

If your animation will be frequently reused, LottieAnimationView has an optional caching strategy built in. Use LottieAnimationView#setAnimation(String, CacheStrategy). CacheStrategy can be Strong, Weak, or None to have LottieAnimationView hold a strong or weak reference to the loaded and parsed animation.

Image Support

You can animate images if your animation is loaded from assets and your image file is in asubdirectory of assets. Just call setImageAssetsFolder on LottieAnimationView orLottieDrawable with the relative folder inside of assets and make sure that the images thatbodymovin export are in that folder with their names unchanged (should be img_#).If you use LottieDrawable directly, you must call recycleBitmaps when you are done with it.

If you need to provide your own bitmaps if you downloaded them from the network or something, youcan provide a delegate to do that:

animationView.setImageAssetDelegate(new ImageAssetDelegate() {         @Override public Bitmap fetchBitmap(LottieImageAsset asset) {           getBitmap(asset);         }       });

Supported After Effects Features

Pre-composition


Keyframe Interpolation


  • Linear Interpolation

  • Bezier Interpolation

  • Hold Interpolation

  • Rove Across Time

  • Spatial Bezier

Solids


  • Transform Anchor Point

  • Transform Position

  • Transform Scale

  • Transform Rotation

  • Transform Opacity

Masks


  • Path

  • Opacity

  • Multiple Masks (additive, subtractive, inverted)

Track Mattes


  • Alpha Matte

Parenting


  • Multiple Parenting

  • Nulls

Shape Layers


  • Rectangle (All properties)

  • Ellipse (All properties)

  • Polystar (All properties)

  • Polygon (All properties. Integer point values only.)

  • Path (All properties)

  • Anchor Point

  • Position

  • Scale

  • Rotation

  • Opacity

  • Group Transforms (Anchor point, position, scale etc)

  • Multiple paths in one group

  • Merge paths (off by default and must be explicitly enabled withenableMergePathsForKitKatAndAbove)

Stroke (shape layer)


  • Stroke Color

  • Stroke Opacity

  • Stroke Width

  • Line Cap

  • Dashes

Fill (shape layer)


  • Fill Color

  • Fill Opacity

  • Fill Rule

Trim Paths (shape layer)


  • Trim Paths Start

  • Trim Paths End

  • Trim Paths Offset

Performance and Memory

  1. If the composition has no masks or mattes then the performance and memory overhead should be quite good. No bitmaps are created and most operations are simple canvas draw operations.
  2. If the composition has masks or mattes, offscreen buffers will be used and there willbe a performance hit has it gets drawn.
  3. If you are using your animation in a list, it is recommended to use a CacheStrategy inLottieAnimationView.setAnimation(String, CacheStrategy) so the animations do not have to be deserialized every time.

Try it out

Clone this repository and run the LottieSample module to see a bunch of sample animations. The JSON files for them are located in LottieSample/src/main/assets and the original After Effects files are located in /After Effects Samples

The sample app can also load json files at a given url or locally on your device (like Downloads or on your sdcard).

Alternatives

  1. Build animations by hand. Building animations by hand is a huge time commitment for design and engineering across Android and iOS. It's often hard or even impossible to justify spending so much time to get an animation right.
  2. Facebook Keyframes. Keyframes is a wonderful new library from Facebook that they built for reactions. However, Keyframes doesn't support some of Lottie's features such as masks, mattes, trim paths, dash patterns, and more.
  3. Gifs. Gifs are more than double the size of a bodymovin JSON and are rendered at a fixed size that can't be scaled up to match large and high density screens.
  4. Png sequences. Png sequences are even worse than gifs in that their file sizes are often 30-50x the size of the bodymovin json and also can't be scaled up.

Why is it called Lottie?

Lottie is named after a German film director and the foremost pioneer of silhouette animation. Her best known films are The Adventures of Prince Achmed (1926) – the oldest surviving feature-length animated film, preceding Walt Disney's feature-length Snow White and the Seven Dwarfs (1937) by over ten yearsThe art of Lotte Reineger

Contributing

Contributors are more than welcome. Just upload a PR with a description of your changes.Lottie uses Facebook screenshot tests for Android to identify pixel level changes/breakages. Please run ./gradlew --daemon recordMode screenshotTests before uploading a PR to ensure that nothing has broken. Use a Nexus 5 emulator running Lollipop for this. Changed screenshots will show up in your git diff if you have.

If you would like to add more JSON files and screenshot tests, feel free to do so and add the test to LottieTest.

Issues or feature requests?

File github issues for anything that is unexpectedly broken. If an After Effects file is not working, please attach it to your issue. Debugging without the original file is much more difficult.


更多相关文章

  1. android对话框弹出方式动画
  2. 【Android】【动画】自定义插值器Interpolator
  3. android 滑动动画效果
  4. android 多媒体框架
  5. DialogFragment设置自定义动画
  6. Android Recyclerview设置条目属性动画
  7. Android属性动画(ObjectAnimation)
  8. Android遍历API (1) 动画篇——克隆动画AnimationCloning
  9. Android ORM框架 GreenDao 的使用详解

随机推荐

  1. Android双列滑动表格(双表头不动)
  2. android 自带 xml解析
  3. android修改AlertDialog主题
  4. 攔截Android的SMS並且停止轉發
  5. Android的Bitmap类中常用方法
  6. Android读取设备内存大小
  7. Android(安卓)自定义View--ProgressBar篇
  8. Android 跳转界面 自动弹出输入框
  9. Android总结笔记
  10. 关于android Http访问,上传,用了三个方法