Android 如何创建具有中间颜色步骤的渐变?

Android 如何创建具有中间颜色步骤的渐变?,android,android-widget,android-layout,Android,Android Widget,Android Layout,我通过xml文件处理渐变 问题是,虽然渐变效果很好,但我希望从一种颜色逐步过渡到另一种颜色,而不是逐渐过渡 似乎没有阶梯渐变的选项。 我该怎么办?如果您可以/想要使用编程解决方案,API允许您使用颜色步骤创建渐变,并使用两个数组为每种颜色定义相对(0.0-1.0)位置: // this creates a semi-lucid cylinder gradient int[] colGreen = {Color.parseColor("#006600"), Color.pa

我通过xml文件处理渐变

问题是,虽然渐变效果很好,但我希望从一种颜色逐步过渡到另一种颜色,而不是逐渐过渡

似乎没有阶梯渐变的选项。
我该怎么办?

如果您可以/想要使用编程解决方案,API允许您使用颜色步骤创建渐变,并使用两个数组为每种颜色定义相对(0.0-1.0)位置:

// this creates a semi-lucid cylinder gradient
int[] colGreen =  {Color.parseColor("#006600"),
           Color.parseColor("#01cc00"),
           Color.parseColor("#119911"),
           Color.parseColor("#22bb33"),
           Color.parseColor("#ddffdd"),
           Color.parseColor("#006600") };

float[] pos = {0.0f, 0.25f, 0.5f, 0.55f, 0.85f, 1f};

LinearGradient gr = new LinearGradient(x0, y0, x1, y1, colGreen, pos, tilemode));

如果您可以/希望移动到编程解决方案,则API允许您使用颜色步骤创建渐变,并使用两个数组为每种颜色定义相对(0.0-1.0)位置:

// this creates a semi-lucid cylinder gradient
int[] colGreen =  {Color.parseColor("#006600"),
           Color.parseColor("#01cc00"),
           Color.parseColor("#119911"),
           Color.parseColor("#22bb33"),
           Color.parseColor("#ddffdd"),
           Color.parseColor("#006600") };

float[] pos = {0.0f, 0.25f, 0.5f, 0.55f, 0.85f, 1f};

LinearGradient gr = new LinearGradient(x0, y0, x1, y1, colGreen, pos, tilemode));

您可以做的是创建一个背景布局,其中包含所需步骤的尽可能多的视图。然后为每个视图设置不同的渐变

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/background"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <View
        android:id="@+id/gradient1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/gradient1" />

    <View
        android:id="@+id/gradient2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/gradient2" />

</LinearLayout>

<LinearLayout
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

</LinearLayout>

</RelativeLayout>

您可以创建一个背景布局,其中包含所需步骤的任意多个视图。然后为每个视图设置不同的渐变

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/background"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <View
        android:id="@+id/gradient1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/gradient1" />

    <View
        android:id="@+id/gradient2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/gradient2" />

</LinearLayout>

<LinearLayout
    android:id="@+id/content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

</LinearLayout>

</RelativeLayout>


非常感谢。。。但是我不想使用线性渐变,因为代码看起来已经太笨拙了。我尝试使用样式并创建了一个单独的xml。在这种情况下不能做任何事情吗???@Abhik我很抱歉,我知道,您不能用xml指定渐变步骤。非常感谢。。。但是我不想使用线性渐变,因为代码看起来已经太笨拙了。我尝试使用样式并创建了一个单独的xml。在这种情况下不能做任何事情吗???@Abhik对不起,我知道,您不能用xml指定渐变步骤。