Android中創(chuàng)建類似Instagram的漸變背景效果

我在我最近的項(xiàng)目用到這個(gè)效果,給大家分享下
https://github.com/zhaoweihaoChina/hnuplus
1. 在drawable文件夾創(chuàng)建一些漸變顏色的資源
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. 創(chuàng)建一個(gè)用到上面創(chuàng)建的漸變色的動(dòng)畫序列,命名為animation_list.xml,放進(jìn)去drawable文件夾
<?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. 將上面已經(jīng)創(chuàng)建好的動(dòng)畫序列應(yīng)用到你layout的背景頂層的view中
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/animation_list" android:id="@+id/container"> <!-- Child Views --> </LinearLayout>
4.在你的activity中用AnimationDrawable去實(shí)現(xiàn)過渡效果
LinearLayout container = (LinearLayout) findViewById(R.id.container);
AnimationDrawable anim = (AnimationDrawable) container.getBackground();
anim.setEnterFadeDuration(6000);
anim.setExitFadeDuration(2000);
// 開始播放動(dòng)畫:在onResume方法中開始播放漸變動(dòng)畫
@Override
protected void onResume() {
super.onResume();
if (anim != null && !anim.isRunning())
anim.start();
}
// 停止播放動(dòng)畫:在onPause方法中停止播放漸變動(dòng)畫
@Override
protected void onPause() {
super.onPause();
if (anim != null && anim.isRunning())
anim.stop();
}
將狀態(tài)欄設(shè)置透明(去除狀態(tài)欄)
values/styles.xml
<resources> <style name="Theme.AppTheme.TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar" /> </resources>
values-v19/styles.xml
<resources>
<style name="Theme.AppTheme.TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
values-v21/styles.xml
<resources>
<style name="Theme.AppTheme.TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>
values-v23/styles.xml
<resources>
<style name="Theme.AppTheme.TranslucentStatusBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowLightStatusBar">true</item>
</style>
</resources>
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加入下面的代碼
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
findViewById(android.R.id.content).setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
setContentView(R.layout.activity_splash);
}
}
<activity
android:name=".MainActivity" android:theme="@style/Theme.AppTheme.TranslucentStatusBar" />
總結(jié)
以上所述是小編給大家介紹的Android中創(chuàng)建類似Instagram的漸變背景效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android編程實(shí)現(xiàn)圖片背景漸變切換與圖層疊加效果
- Android編程實(shí)現(xiàn)左右滑動(dòng)切換背景的方法
- Android實(shí)現(xiàn)動(dòng)態(tài)切換組件背景的方法
- 修改Android FloatingActionButton的title的文字顏色及背景顏色實(shí)例詳解
- Android編程實(shí)現(xiàn)控件不同狀態(tài)文字顯示不同顏色的方法
- Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法
- Android開發(fā)中ImageLoder加載網(wǎng)絡(luò)圖片時(shí)將圖片設(shè)置為ImageView背景的方法
- android開發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例
- Android開發(fā)之背景動(dòng)畫簡單實(shí)現(xiàn)方法
- Android編程實(shí)現(xiàn)對(duì)話框Dialog背景透明功能示例
- Android開發(fā)實(shí)現(xiàn)按鈕點(diǎn)擊切換背景并修改文字顏色的方法
相關(guān)文章
Android性能優(yōu)化之利用強(qiáng)大的LeakCanary檢測(cè)內(nèi)存泄漏及解決辦法
本篇文章主要介紹了Android性能優(yōu)化之利用LeakCanary檢測(cè)內(nèi)存泄漏及解決辦法,有興趣的同學(xué)可以了解一下。2016-11-11
Android4.2中全屏或者取消標(biāo)題欄的方法總結(jié)
有的時(shí)候我們會(huì)看到,會(huì)先出現(xiàn)標(biāo)題欄,然后再消失,因?yàn)槲覀冎皇窃赼ctivity的oncreate方法中定義的,其他實(shí)現(xiàn)方法如下,感興趣的朋友可以了解下哈2013-06-06
Android xml實(shí)現(xiàn)animation的4種動(dòng)畫效果實(shí)例代碼
在Android應(yīng)用程序,使用動(dòng)畫效果,能帶給用戶更好的感覺,做動(dòng)畫可以通過XML或Android代碼來實(shí)現(xiàn)。本文給大家介紹Android xml實(shí)現(xiàn)animation的4種動(dòng)畫效果實(shí)例代碼,一起看看吧2016-05-05
Kotlin基礎(chǔ)學(xué)習(xí)之位運(yùn)算
一提起位運(yùn)算,人們往往想到它的高效性,無論是嵌入式編程還是優(yōu)化系統(tǒng)的核心代碼,適當(dāng)?shù)倪\(yùn)用位運(yùn)算總是一種迷人的手段,下面這篇文章主要給大家介紹了關(guān)于Kotlin基礎(chǔ)學(xué)習(xí)之位運(yùn)算的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11
Android自定義View 實(shí)現(xiàn)水波紋動(dòng)畫引導(dǎo)效果
在android程序開發(fā)中,我們經(jīng)常簡單通過自定義view實(shí)現(xiàn)水波紋動(dòng)畫引導(dǎo)功能,下面通過本文給大家分享實(shí)現(xiàn)代碼,需要的朋友參考下2017-01-01
Android 圖片存入系統(tǒng)相冊(cè)更新顯示實(shí)例詳解
這篇文章主要介紹了Android 圖片存入系統(tǒng)相冊(cè)更新顯示實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Flow轉(zhuǎn)LiveData數(shù)據(jù)丟失原理詳解
這篇文章主要為大家介紹了Flow轉(zhuǎn)LiveData數(shù)據(jù)丟失原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù))
這篇文章主要介紹了Android帶進(jìn)度條的文件上傳示例(使用AsyncTask異步任務(wù)),使用起來比較方便,將幾個(gè)方法實(shí)現(xiàn)就行,感興趣的小伙伴們可以參考一下。2016-11-11

