Fun with Android, Text and Gradient Shadow

While we all waiting for 2nd part of a story with color picker(yeah, me too!). I decided to play with fonts a little bit.

We could use the same trick with LinearGradient as I described in kopi to make your text looks like a rainbow. All we need to do is override onMeasure function(no methods here, we are using Kotlin).

Too easy, right? What if I want my gradient to appear only on shadow behind the text? So yeah, it’s a little bit tricky. There would be three steps to achieve that:

  1. Draw text with default shadow by setting params we need to paint.setShadowLayer() function.
  2. Draw our gradient with PorterDuff.Mode.SRC_ATOP.
  3. Draw our text once again on top of our colored shadow.

Here is a code:

Here is how it looks like:

Don’t forget to call setLayerType(LAYER_TYPE_SOFTWARE, null) before you draw. Unfortunately, xfer modes don’t work with hardware acceleration.

You could found all source code here https://github.com/stepango/kopi/blob/master/app/src/main/java/com/stepango/colorpicker/RainbowTextView.kt

And as conclusion — small hint for different types of xfer modes. Play with it and have fun!

A small hint for Xfer modes.