在Android中製作移動的漸變背景
原文地址:http://thetechnocafe.com/make-a-moving-gradient-background-in-android/
這是一個關於如何在Android上製作移動漸變背景的快速教程。
為了實現這個,我們需要使用AnimationList,現在讓我們開始吧。
首先我們需要建立5個漸變的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>
然後再一個新的xml drawable檔案中新增如下程式碼,包含一個AnimationList用來改變background從一個漸變到另一個漸變,在AnimationList標籤中,新增5個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>
現在將它作為背景設定到activity的根佈局上,並且不要忘了給View/ViewGroup設定一個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>
現在我們需要做的就是在java程式碼中告訴animation list開啟動畫,我們可以呼叫AnimationDrawable.start()
方法,程式碼如下:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear_layout);
AnimationDrawable animationDrawable = (AnimationDrawable)linearLayout.getBackground();
animationDrawable.setEnterFadeDuration(2500);
animationDrawable.setExitFadeDuration(5000);
animationDrawable.start();
可以看到,我們引用了將動畫列表作為背景的LinearLayout,然後我們從AnimationDrawable中獲取他的背景,然後我們設定進入和退出動畫的持續時間,並啟動它。

相關文章
- css radial-gradient繪製漸變背景CSS
- CSS背景漸變CSS
- CSS 背景漸變CSS
- CSS3 背景漸變CSSS3
- Dreamweaver製作滑鼠經過圖片漸漸變暗效果教程
- Dreamweaver中CSS怎麼製作徑向圓形漸變的五種方法CSS
- Android 顏色漸變 屬性動畫Android動畫
- 背景色漸變生成網址
- css3背景顏色漸變CSSS3
- 巧用漸變實現高階感拉滿的背景光動畫動畫
- 【譯】React Native中的動畫漸變React Native動畫
- css漸變背景的頂級用法:linear-gradient()CSS
- PPT怎麼製作漸變UI圖示?PPT扁平化圖示圖片的製作方法UI
- Android中使按鈕的背景變得透明&前端中css設定透明背景Android前端CSS
- css3實現文字線性漸變,css3實現背景漸變CSSS3
- Photoshop中的漸變工具
- 展廳中移動滑軌屏製作的幾個步驟
- Lottie Android 動畫製作與使用Android動畫
- CSS3背景漸變效果程式碼例項CSSS3
- css奇技淫巧-色彩漸變與動態漸變CSS
- iOS 繪製漸變·基礎篇iOS
- 移動端網站製作中需要注意哪些方面?網站
- css背景漸變相容性問題(非原創)CSS
- SVG 漸變動畫效果SVG動畫
- canvas繪製帶有漸變效果的直線Canvas
- Flutter 中漸變的高階用法Flutter
- Dreamweaver網頁元素怎麼製作漸隱漸現效果教程網頁
- PS新手教程:如何在Photoshop中使用“漸變工具”製作質感按鈕?
- 純CSS漸變繪製 Chrome 圖示CSSChrome
- 在swoole中製作一款仿製laravel的框架Laravel框架
- Android開源庫的製作Android
- React Native在Android當中實踐(一)——背景介紹React NativeAndroid
- 如何製作動態圖,GIF怎麼在電腦上製作
- 實時渲染如何改變影片製作和動畫製作動畫
- 漸變色進度條的兩種繪製方案
- 移動的“豹變”
- 漸變色彩藝術海報背景素材|感官刺激、個性突出
- erp在製作型企業中的優勢
- [譯] Android 實現顏色漸變的一個小 tipAndroid