歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android用代碼完成顏色漸變處理

Android用代碼完成顏色漸變處理

日期:2017/3/1 11:02:20   编辑:Linux編程

Android完成顏色漸變是可以靠代碼實現的,那麼我簡單介紹一下實現的方法,下圖是我在項目開發中的一個截圖,其中上方顏色值為#e8e8e8,下方顏色值為#dbdbdb,想要實現漸變並且在漸變的結束有一條明顯的線,那麼就需要漸變結束時的顏色略深於dbdbdb就OK了,

在drawable中建立一個test_gradient.xml文件內容如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle" >
  4. <gradient
  5. android:angle="270"
  6. android:endColor="#b9b9b9"
  7. android:startColor="#e8e8e8" />
  8. </shape>

然後用法如下:

  1. <ImageView
  2. android:layout_width="fill_parent"
  3. android:layout_height="10dip"
  4. android:background="@drawable/test_gradient" />

OK了,我的endColor略深於下面的顏色dbdbdb,這樣漸變結束的時候會有一條明顯的線,如果不想要這個線,則把endColor設置成dbdbdb,就OK~。gradient中的angle指的是漸變顏色的角度,改成90,180,270,大家分別自己試一試效果吧,我就不截圖了。

Copyright © Linux教程網 All Rights Reserved