在Android中制作移動(dòng)的漸變背景

原文地址:http://thetechnocafe.com/make-a-moving-gradient-background-in-android/
這是一個(gè)關(guān)于如何在Android上制作移動(dòng)漸變背景的快速教程。
為了實(shí)現(xiàn)這個(gè),我們需要使用AnimationList,現(xiàn)在讓我們開(kāi)始吧。

首先我們需要?jiǎng)?chuàng)建5個(gè)漸變的drawables,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
        android:angle="225"
        android:endColor="#1a2980"
        android:startColor="#26d0ce"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
        android:angle="45"
        android:endColor="#614385"
        android:startColor="#516395"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
        android:angle="135"
        android:endColor="#1d2b64"
        android:startColor="#f8cdda"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
        android:angle="45"
        android:endColor="#ff512f"
        android:startColor="#dd2476"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
  <gradient
        android:angle="135"
        android:endColor="#34e89e"
        android:startColor="#0f3443"/>
</shape>

然后再一個(gè)新的xml drawable文件中添加如下代碼,包含一個(gè)AnimationList用來(lái)改變background從一個(gè)漸變到另一個(gè)漸變,在AnimationList標(biāo)簽中,添加5個(gè)item。

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
  
<item android:drawable="@drawable/gradient_blue"
      android:duration="5000"/>
<item android:drawable="@drawable/gradient_red"
      android:duration="5000"/>
<item android:drawable="@drawable/gradient_teal"
      android:duration="5000"/>
<item android:drawable="@drawable/gradient_purple"
      android:duration="5000"/>
<item android:drawable="@drawable/gradient_indigo"
      android:duration="5000"/>

</animation-list>

現(xiàn)在將它作為背景設(shè)置到activity的根布局上,并且不要忘了給View/ViewGroup設(shè)置一個(gè)id,我們需要在java代碼中引用他。

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

  <!--Content goes here-->
</LinearLayout>

現(xiàn)在我們需要做的就是在java代碼中告訴animation list開(kāi)啟動(dòng)畫(huà),我們可以調(diào)用AnimationDrawable.start()方法,代碼如下:

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear_layout);
AnimationDrawable animationDrawable = (AnimationDrawable)linearLayout.getBackground();

animationDrawable.setEnterFadeDuration(2500);
animationDrawable.setExitFadeDuration(5000);
animationDrawable.start();

可以看到,我們引用了將動(dòng)畫(huà)列表作為背景的LinearLayout,然后我們從AnimationDrawable中獲取他的背景,然后我們?cè)O(shè)置進(jìn)入和退出動(dòng)畫(huà)的持續(xù)時(shí)間,并啟動(dòng)它。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容