溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-06-30 11:52:29 來源:億速云 閱讀:180 作者:小新 欄目:移動開發(fā)

這篇文章主要為大家展示了“Android中如何創(chuàng)建類似Instagram的漸變背景效果”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“Android中如何創(chuàng)建類似Instagram的漸變背景效果”這篇文章吧。

效果圖:

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

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" />

以上是“Android中如何創(chuàng)建類似Instagram的漸變背景效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI