美文网首页
渐变背景

渐变背景

作者: 贝贝beibei96 | 来源:发表于2018-05-07 16:34 被阅读18次
    Gradient

    1.在drawable文件夹下创建一些渐变颜色文件
    color1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="#614385"
            android:endColor="#516395"
            android:angle="0"/>
    </shape>
    

    color2.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="#5f2c82"
            android:endColor="#49a09d"
            android:angle="45"/>
    </shape>
    

    color3.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="#4776E6"
            android:endColor="#8E54E9"
            android:angle="90"/>
    </shape>
    

    color4.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:startColor="#7141e2"
            android:endColor="#d46cb3"
            android:angle="135"/>
    </shape>
    

    2.使用上面的渐变颜色创建一个动画列表animation_list.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="10000" />
        <item
            android:drawable="@drawable/color2"
            android:duration="10000" />
        <item
            android:drawable="@drawable/color3"
            android:duration="10000" />
        <item
            android:drawable="@drawable/color4"
            android:duration="10000" />
    </animation-list>
    

    3.将动画列表应用为Activity布局的顶级视图

    <android.support.constraint.ConstraintLayout
                android:id="@+id/cl_home"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/animation_list"
                android:id="@+id/container">
    
        <!-- Child Views -->
    
    </android.support.constraint.ConstraintLayout>
    

    4.在Activity里使用AnimationDrawable实现过渡切换

    ConstraintLayoutcontainer = findViewById(R.id.container);
    
    AnimationDrawable anim = (AnimationDrawable) container.getBackground();
    anim.setEnterFadeDuration(6000);
    anim.setExitFadeDuration(2000);
    
    // Starting animation:- start the animation on onResume.
    @Override
    protected void onResume() {
        super.onResume();
        if (anim != null && !anim.isRunning())
            anim.start();
    }
          
    // Stopping animation:- stop the animation on onPause.
    @Override
    protected void onPause() {
        super.onPause();
        if (anim != null && anim.isRunning())
            anim.stop();
    }
    

    相关文章

      网友评论

          本文标题:渐变背景

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