Wednesday 21 October 2020

change LinearGradient colors ratio dynamically for custom seekBar

I have created a seekbar with a custom LinearGradient drawable. However, I want to be able to change gradient ratio for each color, here I use 3 colors, they get equally distributed if positions is null. What I want, in fact, is to provide width or ratio to each color, for example change red ratio and set it only form 0% to 10% of seekBar width.

Here i want to, set 0% to 10% red, 10% - 80% yellow, and 80% to 100% red, and be able to change the width values for each color dynamically.

Is this possible, and if yes, can anyone guide me on how?

My code

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;
}

Image can be seen from current settings, its not as i described.

enter image description here



from change LinearGradient colors ratio dynamically for custom seekBar

No comments:

Post a Comment