如何在安卓系统中创建自定义渐变?

3
3个回答

6

这里有一个用于创建Android渐变形状的在线工具:

http://angrytools.com/gradient/

点击Android标签,您将获得包含形状的XML代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient 
    android:type="linear"
    android:centerX="47%" 
    android:startColor="#FF70C1FF" 
    android:centerColor="#FFBDC9FF" 
    android:endColor="#FF734080" 
    android:angle="45"/>
</shape>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient 
    android:type="radial"
    android:centerX="50%" 
    android:centerY="50%" 
    android:startColor="#FF70C1FF" 
    android:centerColor="#FFBDC9FF" 
    android:endColor="#FF734080" 
    android:gradientRadius="47"/>
</shape>

实际上它正在提供线性渐变颜色形状,但正如您在图像中所看到的那样,它并不完全是线性渐变。如果您选择两种颜色,则应该从上到下,每种颜色从浅到深的阴影并排放置。 - BSKANIA

1
int startColor=Color.parseColor("#a6c1de");
//you can add as many gradient colors as you want here
//and include it in the int array while initializing GradientDrawable 
int endColor=Color.parseColor("#aa82b7");
GradientDrawable mDrawable=new GradientDrawable(Orientation.LEFT_RIGHT,
                                    new int[] {startColor,endColor});

LinearLayout mLayout=(LinearLayout)findViewById(R.id.your_id);
mLayout.setBackgroundDrawable(mDrawable);

可以的,但你可以在中心添加另一种颜色。new int[] startColor,centerColor, endColor} - makata

0

您无法像图像中那样使用多种颜色创建Gradient,其中两种颜色呈线性。因此,您应该使用图像而不是渐变。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接