I am trying to create a gradient drawable to depict a ratio of something. As a general example, lets say you were looking at a listview containing a fleet of cars. The background could be a fuel gauge - I don't want a smooth, wide transition. I want a very tight transition, and I want to be able to set where that transition takes place.
Here's what I'm staring with, but I'm not getting very far.
<?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>
Thanks!
Ok, here's how you can do it:
ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient lg = new LinearGradient(0, 0, width, height,
new int[]{Color.GREEN, Color.GREEN, Color.WHITE, Color.WHITE},
new float[]{0,0.5f,.55f,1}, Shader.TileMode.REPEAT);
return lg;
}
};
PaintDrawable p=new PaintDrawable();
p.setShape(new RectShape());
p.setShaderFactory(sf);
Then just set that as your backroundDrawable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With