Android用代码完成颜色渐变处理

来源:互联网 发布:3g网络语音电话 编辑:程序博客网 时间:2024/05/21 10:37

     android完成颜色渐变是可以靠代码实现的,那么我简单介绍一下实现的方法,下图是我在项目开发中的一个截图,其中上方颜色值为#e8e8e8,下方颜色值为#dbdbdb,想要实现渐变并且在渐变的结束有一条明显的线,那么就需要渐变结束时的颜色略深于dbdbdb就OK了,

                  

 

在drawable中建立一个test_gradient.xml文件内容如下:

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


 

然后用法如下:

 

    <ImageView        android:layout_width="fill_parent"        android:layout_height="10dip"        android:background="@drawable/test_gradient" />


 

 

OK了,我的endColor略深于下面的颜色dbdbdb,这样渐变结束的时候会有一条明显的线,如果不想要这个线,则把endColor设置成dbdbdb,就OK~。gradient中的angle指的是渐变颜色的角度,改成90,180,270,大家分别自己试一试效果吧,我就不截图了。