Android中創(chuàng)建類(lèi)似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)畫(huà)序列,命名為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)畫(huà)序列應(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)過(guò)渡效果
LinearLayout container = (LinearLayout) findViewById(R.id.container); AnimationDrawable anim = (AnimationDrawable) container.getBackground(); anim.setEnterFadeDuration(6000); anim.setExitFadeDuration(2000); // 開(kāi)始播放動(dòng)畫(huà):在onResume方法中開(kāi)始播放漸變動(dòng)畫(huà) @Override protected void onResume() { super.onResume(); if (anim != null && !anim.isRunning()) anim.start(); } // 停止播放動(dòng)畫(huà):在onPause方法中停止播放漸變動(dòng)畫(huà) @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)建類(lèi)似Instagram的漸變背景效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(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開(kāi)發(fā)中ImageLoder加載網(wǎng)絡(luò)圖片時(shí)將圖片設(shè)置為ImageView背景的方法
- android開(kāi)發(fā)修改狀態(tài)欄背景色和圖標(biāo)顏色的示例
- Android開(kāi)發(fā)之背景動(dòng)畫(huà)簡(jiǎn)單實(shí)現(xiàn)方法
- Android編程實(shí)現(xiàn)對(duì)話(huà)框Dialog背景透明功能示例
- Android開(kāi)發(fā)實(shí)現(xiàn)按鈕點(diǎn)擊切換背景并修改文字顏色的方法
相關(guān)文章
Android巧用ViewPager實(shí)現(xiàn)左右循環(huán)滑動(dòng)圖片
這篇文章主要為大家詳細(xì)介紹了Android巧用ViewPager實(shí)現(xiàn)左右循環(huán)滑動(dòng)圖片的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05詳解Xamarin.Android 利用Fragment實(shí)現(xiàn)底部菜單
這篇文章主要介紹了詳解Xamarin.Android 利用Fragment實(shí)現(xiàn)底部菜單,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板
這篇文章主要為大家詳細(xì)介紹了Flutter質(zhì)感設(shè)計(jì)之模態(tài)底部面板,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android中將一個(gè)圖片切割成多個(gè)圖片的實(shí)現(xiàn)方法
有種場(chǎng)景,我們想將一個(gè)圖片切割成多個(gè)圖片。比如我們?cè)陂_(kāi)發(fā)一個(gè)拼圖的游戲,就首先要對(duì)圖片進(jìn)行切割2013-05-05Android自定義View原理(實(shí)戰(zhàn))
這篇文章主要介紹了Android自定義View原理,由于Android系統(tǒng)內(nèi)置的View不滿(mǎn)足我們的業(yè)務(wù)需求,變產(chǎn)生了需要自定義View的原因,關(guān)于自定義詳情,需要的小伙伴可以參考下面文章具體詳情2022-05-05Android開(kāi)發(fā)中獲取View視圖寬與高的常用方法小結(jié)
這篇文章主要介紹了Android開(kāi)發(fā)中獲取View視圖寬與高的常用方法,結(jié)合實(shí)例形式總結(jié)分析了Android獲取View視圖寬與高的三種常用方法及使用場(chǎng)景,需要的朋友可以參考下2017-10-10Android?DialogFragment使用之底部彈窗封裝示例
這篇文章主要為大家介紹了Android?DialogFragment使用之底部彈窗封裝示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09