Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式创建径向渐变_Java_Android - Fatal编程技术网

Java 以编程方式创建径向渐变

Java 以编程方式创建径向渐变,java,android,Java,Android,我试图以编程的方式重现下面的渐变 <shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="@color/startcolor" android:centerColor="#343434" android:endColor="#00000000" android:typ

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

<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>

要设置该特定参数(我假设centerX值,因为您尚未指定):

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

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
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);