如何使用Android动画开发框架Lottie?

来源:深圳沃龙软件开发公司     2017-04-01

动画片

碉堡的Lottie

        Airbnb开源的一个名叫Lottie的动画库,它能够同时支持iOS,Android与ReactNative的开发.此消息一出,还在苦于探索自定义控件各种炫酷特效的我,兴奋地就像发现的新大陆一般.可以说,Lottie的出现,将极大地解放Android/iOS工程师于无尽的编写原生自定义动画的工作中

        碉堡在什么地方呢?动画在项目中就打的缺点就是占用内存大,一个gif动画1-2M的都算小了,这是最让人头大的地方,然而Lottie完美的解决了这一问题,Lottie使用AE(Adobe After Effects)生产的JSON格式的动画,占用内存是普通gif大小的5%,流畅度完全没有改变。


        使用起来也是非常简单,动画json文件放在app/src/main/assets,在项目的build.gradle文件中加入:


 dependencies {  
      compile 'com.airbnb.android:lottie:1.0.1'
      ...
    }

xml文件代码

<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" />

或者你在java代码中:

 LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);
    animationView.setAnimation("hello-world.json");
    animationView.loop(true);

你还可以通过API控制动画,并且设置一些监听:

animationView.addAnimatorUpdateListener((animation) -> {        // Do something.
    });
    animationView.playAnimation();
    ...    if (animationView.isAnimating()) {        // Do something.
    }
    ...
    animationView.setProgress(0.5f);
    ...    // 自定义速度与时长
    ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f)
        .setDuration(500);
    animator.addUpdateListener(animation -> {
        animationView.setProgress(animation.getAnimatedValue());
    });
    animator.start();
    ...
    animationView.cancelAnimation();

简单粗暴!

lottieGit源码:https://github.com/airbnb/lottie-android


本内容皆为 深圳沃龙软件开发公司 原创
如需转载,请注明文章出处和来源网址:http://www.wooolong.com/blog/7.html

全部评论

Winston | 2024-07-17

If some one wants expert view on the topic of blogging and site-building after that i advise him/her to visit this webpage, Keep up the fastidious work.

Reply

Phil Stewart | 2024-07-28

Quick question: how would you like your ad to be seen by millions of people? I sent this message to your contact form, and you're reading it now! Visit my site below to learn more. HHtp://wk54zm.contactblasting.xyz

Reply

Phil Stewart | 2024-08-12

Hey, want your ad to reach millions of contact forms? Just like you're reading this message, others can read yours too. Visit my site below for details. HHtp://nab4tc.contactblasting.xyz

Reply

Gene | 2024-08-14

I am in fact thankful to the holder of this web page who has shared this impressive piece of writing at here.

Reply

Willy | 2024-08-18

Hi there, I found your site via Google whilst searching for a similar subject, your site got here up, it appears to be like great. I have bookmarked it in my google bookmarks. Hello there, just changed into aware of your blog via Google, and found that it is truly informative. I'm going to watch out for brussels. I will appreciate should you continue this in future. Lots of people will probably be benefited out of your writing. Cheers!

Reply

Phil Stewart | 2024-08-31

Congratulations! By reading this, you’ve proven that contact form blasting captures attention. The biggest challenge in advertising is getting your ad seen, and you’ve just done that with my ad and I can do it with yours too! Now, let’s blast your ad to a wider audience and boost your sales, followers, and YouTube views. Explore our high-volume blasting plans tailored for affiliate marketers and agencies.Find more information on my site HHtp://lsn85a.contactformmarketing.xyz

Reply

Terra Abbott | 2024-09-04

Are you struggling to get your website updated to the way you need it ? Tired of paying $50+ per hour just for a few tweaks? We are a FULL SERVICE, USA managed web development agency with wholesale pricing. No job too big or small. Test us out to see our value. Use the link in my signature, for a quick turn around quote. Kristine Avocet Senior Web Specialist Fusion Web Experts 186 Daniel Island Drive Daniel Island, SC 29492 www.fusionwebexperts.tech

Reply

Drusilla | 2024-09-06

It is really a nice and useful piece of information. I am happy that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.

Reply

Phil Stewart | 2024-09-11

Curious about getting your ad seen by millions? I sent this message to your contact form, and here you are reading it! Visit my site below to find out more. HHtp://3m1erq.contactformblasting.xyz

Reply

Jocelyn | 2024-09-16

Hi there to all, how is everything, I think every one is getting more from this web site, and your views are nice in support of new people.

Reply

发表评论