Pha*_*tom 5 android gradient programmatically
我有以下渐变
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:centerColor="#42f448"
android:centerX="0.5"
android:centerY="0.5"
android:endColor="#000000"
android:startColor="#000000"
android:type="linear" />
Run Code Online (Sandbox Code Playgroud)
我需要做的是基于ListView上的位置更改来更改渐变的中心。是否可以通过编程方式创建此渐变?
我已经尝试过这种方法,但是如果我更改中心值,则什么也没有发生,它将保持不变:
int colors[] = {0xff000000, 0xff42f448, 0xff000000};
GradientDrawable shape = new GradientDrawable();
shape.setOrientation(GradientDrawable.Orientation.TOP_BOTTOM);
shape.setColors(colors);
shape.setGradientCenter(0.3f, 0.3f);
view.setBackgroundDrawable(shape);
Run Code Online (Sandbox Code Playgroud)
我想念什么?谢谢。
使用Shader就可以实现你的目标
ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
return new LinearGradient(width/2, 0, width/2, height,
new int[]{0xff000000, 0xff42f448, 0xff000000},
new float[]{0, 0.3f, 1}, // start, center and end position
Shader.TileMode.CLAMP);
}
};
PaintDrawable pd = new PaintDrawable();
pd.setShape(new RectShape());
pd.setShaderFactory(sf);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackground(pd);
} else
layout.setBackgroundDrawable(pd);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1014 次 |
| 最近记录: |