开发者

Android how to get gradient effect on setBackgroundColor

Is there a way to get a gradient effect or a semi-transparant effect when setting the background color of a view? For example, this code:

selView.setBackgroundColor(Color.rgb(240, 128, 1开发者_JAVA技巧28)); // light red

highlights a selected view in a list view. Either a gradient or a background would be cool.


You would first need to create a gradient in xml.

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle"> 
  <gradient 
  android:startColor="#FFFF00FF" 
  android:endColor="#FFFFFFFF" 
  android:angle="270"/> 
</shape>

You should add this to one of your drawables.xml. You should then be able to apply this to your ListView in xml.

android:background=@drawable/yourdrawable

or

setBackgroundResource(R.drawable.yourdrawable);


i will give you a sample code

  <item android:state_pressed="true" >
    <shape>
    <gradient
    android:startColor="#ff5500"
    android:endColor="#999999"
    android:angle="270" />
    <stroke
    android:width="3dp"
    android:color="#999999" />
    <corners
    android:radius="3dp" />
    <padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />
    </shape>
    </item>

    <item android:state_focused="true" >
    <shape>
    <gradient
    android:endColor="#ff5500"
    android:startColor="#999999"
    android:angle="270" />
    <stroke
    android:width="3dp"
    android:color="#999999" />
    <corners
    android:radius="3dp" />
    <padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />
    </shape>
    </item>

    <item> 
    <shape 
    android:shape="rectangle">

    <gradient android:type="radial" android:gradientRadius="50"
    android:startColor="#999999" android:endColor="#000000" />
    <!-- <corners-->
    <!-- android:radius="10dp" />-->
    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
    android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
    </shape>
    </item>
    </selector>

apply this to your view from drawable


setBackgroundDrawable(R.drawable.my_fancy_gradient_or_picture)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜