java - 以编程方式创建径向渐变

标签 java android

我正在尝试以编程方式重现以下渐变。

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

最佳答案

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

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

关于java - 以编程方式创建径向渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8481322/

相关文章:

java - 简单XML : Add attributes to primitives

java - 尝试上传图像时,base64_decode 返回 null

java - 在普通 servlet 应用程序中使用 spring jdbc 模板进行数据库访问

android - 如何更改状态栏颜色以匹配 Lollipop 中的应用程序? [安卓]

java - 可写文件权限问题

android - Jetpack 撰写 : ViewTreeLifecycleOwner not found

java - 优化sqlite应用程序启动

android - iOS 和 Android 应用首选项的 Mobile Iron MDM 兼容性

android - 更改为与布局不同后如何单击 ListView 项目?

Java 字符串模式识别