为自定义搜索栏动态更改 LinearGradient 颜色比率

Ami*_* De 5 java android android-seekbar material-design android-graphics

我创建了一个带有自定义LinearGradient可绘制对象的搜索栏。但是,我希望能够更改每种颜色的渐变比例,这里我使用 3 种颜色,如果positions是,它们会均匀分布null。我想要的,其实是提供宽度或比于每种颜色,例如红色的变化率,并将其设置只形成0%至10%seekBar width

在这里,我想设置 0% 到 10% 的红色、10% - 80% 的黄色和 80% 到 100% 的红色,并且能够动态更改每种颜色的宽度值。

这是可能的,如果是,有人可以指导我如何操作吗?

我的代码

private ShapeDrawable getSeekBarGradientDrawable(int mRectWidth, int mRectHeight) {
    int[] colors = new int[] { Color.RED, Color.YELLOW, getResources().getColor(R.color.primaryButton, null)};

DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels - (int) getResources().getDimension(R.dimen.margin_padding_size_medium);

    Shader shader = new LinearGradient(
            0,
            0,
            width,
            mRectHeight,
            colors, new float[] {0.1f,0.7f,0.2f},
            Shader.TileMode.CLAMP);

    ShapeDrawable shape = new ShapeDrawable(new RectShape());
    shape.getPaint().setShader(shader);
    return shape;
}
Run Code Online (Sandbox Code Playgroud)

图像可以从当前设置中看到,它不像我描述的那样。

在此处输入图片说明