How to make TextView with long shadow in Android

Displaying TextView like Word Art will get your app more good-looking. In Android API, there are afew options for customizing beautiful TextView. By this reason, we must make our own Component based on View to display.
How to make this? Solution for this case:

1. Detect bounds of text using Paint.getTextBounds() method
2. Create transparent Bitmap with such metrics (W + H) x H (you may use Bitmap.Config.ARGB_8888 to optimize memory usage).
3. Draw text on this Bitmap at 0x0 position.
4. Copy first row of Bitmap into new one with original width, but with height of 1px.
5. Iterate over the Y-axis of Bitmap (from top to bottom) and draw single-line Bitmap with the corresponding offset by X-axis (you will overdraw some transparent pixels).
6. Now you have the top-part of your shadow
7. Draw the bottom part using same technique, but choosing last row of this Bitmap.

This algorithm may be optimized if you detect, that all pixels in last row have the same color (full shadow).
And source:
Create it's AttributeSets in res/values/attrs.xml:
Declare in activity_main.xml:
Finally, ActivityMain source for running program:

Screen shots (click for full size):
pic name pic name pic name


Previous post
« Prev Post
Next post
Next Post »