Android “Advanced” Gradients - 'thumb' positions?

北战南征 提交于 2020-01-01 00:26:10

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/3947850/android-advanced-gradients-thumb-positions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!