android颜色渐变如何实现从四周往中心渐变 或者从中心往四周渐变 都行,不是 从左往右

如题所述

android 颜色渐变是指通知xml或者java代码,设置相关参数,是界面的某个指定的视图显示成从开始位置的颜色,逐渐过度到结尾位置的颜色的技术。

android颜色渐变的分类有:

LinearGradient线性渐变

RadialGradient镜像渐变

 SweepGradient角度渐变


一、LinearGradient线性渐变
顾名思义,是只颜色在一个直线方向上逐渐改变。

文件代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <gradient
        android:endColor="#0000FF"
        android:startColor="#FF0000"
        android:type="linear" />

</shape>

效果:


二、RadialGradient镜像渐变
镜像渐变就是楼主问的问题了:只要将type设置为oval,然后增加

        android:gradientRadius

属性。

楼主特殊要求是圆形的话,需要在shape里面添加
android:shape="oval"

文件代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <gradient
        android:endColor="#0000FF"
        android:gradientRadius="100%p"
        android:startColor="#FF0000"
        android:type="linear" />

</shape>

效果:


三、 SweepGradient角度渐变

是指以中心点为射线的一个断点,顺时针旋转所扫过的区域,颜色逐渐改变的一种渐变方式

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:endColor="#0000FF"
        android:startColor="#FF0000"
        android:type="sweep" />

</shape>

效果图:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-13
<gradient>
Specifies a gradient color for the shape.
attributes:
android:angle
Integer. The angle for the gradient, in degrees. 0 is left to right, 90 is bottom to top. It must be a multiple of 45. Default is 0.
android:centerX
Float. The relative X-position for the center of the gradient (0 - 1.0). Does not apply when android:type="linear".
android:centerY
Float. The relative Y-position for the center of the gradient (0 - 1.0). Does not apply when android:type="linear".
android:centerColor
Color. Optional color that comes between the start and end colors, as a hexadecimal value or color resource.
android:endColor
Color. The ending color, as a hexadecimal value or color resource.
android:gradientRadius
Float. The radius for the gradient. Only applied when android:type="radial".
android:startColor
Color. The starting color, as a hexadecimal value or color resource.
android:type
Keyword. The type of gradient pattern to apply. Valid values are:
Value Description
"linear" A linear gradient. This is the default.
"radial" A radial gradient. The start color is the center color.
"sweep" A sweeping line gradient.

用 radial type 再设一下 android:centerY android:centerX追问

表示看不懂

追答

看看android 官方文档 里面 resource shapeDrawable 的定义 。
还有 看看 android.graphics.drawable.GradientDrawable 类 。

本回答被网友采纳
第2个回答  2011-10-13
使用RadialGradient即可。
定义一个Paint,然后调用setShader方法将RadialGradient传进去。
然后调用canvas的draw方法将Paint对象传进去即可。追问

能给哥简单的代码示例么

相似回答