Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android "Advanced" Gradients - 'thumb' positions?

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!

like image 785
Adam Avatar asked Dec 17 '22 20:12

Adam


1 Answers

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

like image 165
Adam Avatar answered Dec 27 '22 05:12

Adam