【问题标题】:Create a radial gradient programmatically以编程方式创建径向渐变
【发布时间】:2011-12-12 21:45:26
【问题描述】:

我试图以编程方式重现以下渐变。

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient 
        android:startColor="@color/startcolor" 
        android:centerColor="#343434"
        android:endColor="#00000000"
        android:type="radial"
        android:gradientRadius="140"
        android:centerY="45%"
     />
    <corners android:radius="0dp" />
</shape>

如何以编程方式设置参数?谢谢

        android:centerY="45%"

【问题讨论】:

    标签: java android


    【解决方案1】:

    http://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html

    要设置该特定参数(我假设一个 centerX 值,因为您没有指定一个):

    yourGradientDrawable.setGradientCenter(1.0f,  0.45f);
    

    所以要以编程方式创建上述渐变(不同颜色除外):

    GradientDrawable g = new GradientDrawable(Orientation.TL_BR, new int[] { getResources().getColor(R.color.startcolor), Color.rgb(255, 0, 0), Color.BLUE });
    g.setGradientType(GradientDrawable.RADIAL_GRADIENT);
    g.setGradientRadius(140.0f);
    g.setGradientCenter(0.0f, 0.45f);
    

    注意:径向渐变忽略方向,但采用颜色的构造函数需要。

    【讨论】:

    • 这对我不起作用,没有径向渐变,只有纯白色背景。
    • 如果要使用默认中心值,则无需致电setGradientCenter
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    相关资源
    最近更新 更多