问题描述:

I've implemented a vertical LinearGradient for a custom View. It is working good on most of devices but we've noticed that on a Galaxy Note 2, running Android 4.1.2, the gradient is not transitioning smoothly between colors (color interpolation), but each color just starts at some point.

I'm constructing the gradient like this

 ViewCompat.setLayerPaint(myView, null);

Paint paint = myView.getPaintRender();

paint.setDither(true);

int height = myView.getHeight();

// create a gradient background paint which will be later on clipped

mColors = generateColorsRage(myDTOProvidingData);

float[] percentages = generateColorPercentages();

LinearGradient linearGradient = new LinearGradient(0, 0, 0, height, mColors, percentages, Shader.TileMode.CLAMP);

paint.setShader(linearGradient);

Can somebody point me to a direction?

Thanks!

网友答案:

I've finally come to a solution. It seems that the LinearGradient class on the Android 4.1.2 has a weird way of handling percentages. It seems that the colors interpolations are not working if the percentages do not have at least one value equals to 1f.

So to fix this, I made sure that the percentages[] have the last value set to 1f and both mColors[] and percentages[] are the same length in order for this to properly work.

相关阅读:
Top