开发者

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 tig开发者_Python百科ht 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜