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