Deniz Uğur Deniz Uğur - 4 months ago 12
Android Question

LinearGradient not going transparent as end color

I've set my LinearGradient like this

TextPaint paintText = new TextPaint(Paint.ANTI_ALIAS_FLAG);
paintText.setColor(Color.WHITE);
paintText.setTextSize(size);
paintText.setStyle(Paint.Style.FILL_AND_STROKE);
paintText.setShadowLayer(3f, 3f, 3f, Color.BLACK);

int pL = bitmap.getWidth() / 100;
int pT = bitmap.getHeight() / 100;

StaticLayout mTextLayout = new StaticLayout(
gagTitle,
paintText,
newCanvas.getWidth(),
Layout.Alignment.ALIGN_NORMAL,
1.0f,
0.0f,
false);

int dummyHeight = mTextLayout.getHeight();
Shader shader = new LinearGradient(0, 0, 0, (dummyHeight * 2), Color.BLACK, Color.TRANSPARENT, Shader.TileMode.MIRROR);
Paint paint = new Paint();
paint.setShader(shader);
newCanvas.drawRect(new RectF(0, 0, bitmap.getWidth(), dummyHeight), paint);


I don't know why but at the end of generated gradient it's less brighter black other than being transparent.

Here is the result

example

Best Regards

Answer

The LinearGradient coordinates are not relative, they are given in absolute values of the canvas grid.

Also it is not necessary to have a mirror boundary in your case.

 Shader shader = new LinearGradient(0, 0, 0, dummyHeight, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);