How would I extend TextView to allow the drawing of text with a gradient effect?
How would I extend TextView to allow the drawing of text with a gradient
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It doesn’t appear possible to extend TextView to draw text with a gradient. It is, however, possible to achieve this effect by creating a canvas and drawing on it. First we need to declare our custom UI element. In the initiation we need to create a subclass of Layout. In this case, we will use BoringLayout which only supports text with a single line.
We then override
onMeasureandonDraw:Our implementation of
onDrawis at this point quite lazy (it completely ignores the measurement specs!, but so long as you guarantee that the view is given sufficent space, it should work okay.Alternatively, it would be possible to inherit from a
Canvasand override theonPaintmethod. If this is done, then unfortunately the anchor for text being drawn will always be on the bottom so we have to add-textPaint.getFontMetricsInt().ascent()to our y coordinate.