我想创建渐变背景,其中渐变位于上半部分,下半部分是纯色,如下图所示:

我不能因为centerColor展开以覆盖底部和顶部.

如何制作像第一张图像的背景?如何制作centerColor不散布的小号?
这是上面背景按钮的XML代码.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<gradient
android:startColor="#6586F0"
android:centerColor="#D6D6D6"
android:endColor="#4B6CD6"
android:angle="90"/>
<corners
android:radius="0dp"/>
</shape>
Run Code Online (Sandbox Code Playgroud) 所以我在尝试设置视图的背景可绘制时有点混乱.该代码依赖于知道视线的高度,所以我不能把它onCreate()或onResume(),因为getHeight()返回0 onResume()似乎是最接近我可以得到虽然.我应该在哪里放置如下所示的代码,以便在显示给用户时背景发生变化?
TextView tv = (TextView)findViewById(R.id.image_test);
LayerDrawable ld = (LayerDrawable)tv.getBackground();
int height = tv.getHeight(); //when to call this so as not to get 0?
int topInset = height / 2;
ld.setLayerInset(1, 0, topInset, 0, 0);
tv.setBackgroundDrawable(ld);
Run Code Online (Sandbox Code Playgroud) 根据我读过的内容,您可以使用gradientDrawable并为其设置三种颜色,例如:
<gradient startColor="#00FF00" centerColor="#FFFF00" endColor="#FFFFFF"/>
Run Code Online (Sandbox Code Playgroud)
但是,如果我想要超过三种颜色,不仅如此,我希望能够设置每个颜色的位置(重量/百分比)?
是否可以使用API或我应该制作自己的自定义drawable?如果我需要制作自己定制的可绘制的,我应该怎么做?
如何使用XML制作这种可绘制渐变?

我可以做一个从颜色A到颜色B的简单渐变,但我不知道如何在同一个drawable中组合两个渐变.
我有一个在xml中定义的渐变drawable,我将它用作背景,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="4dp">
<shape>
<gradient
android:startColor="@color/blue"
android:endColor="@color/dark_blue"
android:angle="270" />
</shape>
</item>
<item android:top="98dp">
<shape>
<gradient
android:startColor="@color/black"
android:endColor="@color/transparent_black"
android:angle="270" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
我需要以编程方式实现它.我尝试使用GradientDrawable如下(此方法在自定义视图上实现):
int[] colors1 = {getResources().getColor(R.color.black), getResources().getColor(R.color.trasparent_black)};
GradientDrawable shadow = new GradientDrawable(Orientation.TOP_BOTTOM, colors1);
shadow.setBounds(0,98, 0, 0);
int[] colors = new int[2];
colors[0] = getResources().getColor(R.color.blue);
colors[1] = getResources().getColor(R.color.dark_blue);
GradientDrawable backColor = new GradientDrawable(Orientation.TOP_BOTTOM, colors);
backColor.setBounds(0, 0,0, 4);
//finally create a layer list and set them as background.
Drawable[] layers = new Drawable[2];
layers[0] = backColor; …Run Code Online (Sandbox Code Playgroud) 我想要一个以非线性速率从起始颜色进展到最终颜色的渐变.梯度仅沿单个笛卡尔轴变化.RadialGradient或SweepGradient不是我在这里提到的.
我的问题是,Android是否支持控制渐变转换率,而无需编写自定义着色器?
我最近想让我的状态栏颜色渐变.我知道WindowManager的工作方式.但我决定找另一种方法用渐变为我的状态栏着色.
所以我这样做了,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">@drawable/gradient</color>
<color name="colorAccent">#FF4081</color>
</resources>
Run Code Online (Sandbox Code Playgroud)
@绘制/梯度
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="135" android:startColor="#f56f2c" android:endColor="#fa9f46"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
@ drawable/gradient是我设置的渐变颜色.虽然IDE说它不是正确的方法,但它是有效的.
我的问题:这是正确的方法吗?有没有人有这种经历?
我试图创建一个渐变drawable来描绘某事物的比例.作为一般示例,假设您正在查看包含车队的列表视图.背景可能是一个燃料表 - 我不希望平稳,广泛的过渡.我想要一个非常紧凑的过渡,我希望能够设置转换发生的位置.
这是我正在盯着的,但我没有走得太远.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#D2E3D3"
android:endColor="#E6E6E3"
android:angle="0"
/>
<corners android:radius="0dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我在drawable文件夹中有一个可绘制的形状.这是XML结构:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#4D0418"
android:centerColor="#5F031D"
android:endColor="#7D0227"
android:angle="-90" />
</shape>
Run Code Online (Sandbox Code Playgroud)
现在我想从运行时的java代码中更改startColor,centerColor,endColor.如何改变?