一日一学_ToolBar渐变动画

作者: WuXiao_ | 来源:发表于2017-01-22 11:14 被阅读571次

    今天说点简单有意思的东西减减压,迎接即将来临的春节。
    之前有个产品Toolbar进行颜色渐变,如下:

    演示
    效果如丝绸般顺滑。
    思考了很久
    不影响apk体积,还如此顺滑,只有shape进行渐变了。
    渐变以后怎么进行动画效果了?
    对,动画!原谅我平时不爱加特效,去android开发者平台(https://developer.android.google.cn/index.html 无需翻墙,google回归的第一步)进行了查看,animation-list动画集合,他有什么作用?
    进一步学习了解它。
    首先创建渐变drawable
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="#7141e2"
            android:endColor="#d46cb3"
            //渐变角度(45度倍数进行调整)
            android:angle="0"/>
    
    </shape>
    

    带动画的drawable.xml

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:drawable="@drawable/color1"
            android:duration="300" />
        <item
            android:drawable="@drawable/color2"
            android:duration="300" />
        <item
            android:drawable="@drawable/color3"
            android:duration="300" />
    
    </animation-list>
    

    animation-list可以设置每个文件的动画时间,次数,形成一个帧动画
    应用:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:background="@drawable/toolbar_list"
        android:layout_height="?android:attr/actionBarSize">
    
    </LinearLayout>
    

    部分实现代码:

       .......
            anim = (AnimationDrawable)  linearLayout.getBackground();
            //更改drawable结束全局渐变持续时间,使动画更加顺滑。
            //(此方法很重要,如果不设置动画会很机械化)
            anim.setExitFadeDuration(500);
            //动画执行一次
            anim.setOneShot(true);
            anim.start();
      ........
    

    相关文章

      网友评论

        本文标题:一日一学_ToolBar渐变动画

        本文链接:https://www.haomeiwen.com/subject/wzavbttx.html