Question

For various Views in Android, I use a simple background color so far. But in order to make it more interesting, I'd like to add a texture or noise to the background.

Finding background patterns is easy. And making them into a tiled BitmapDrawable to be used as a background just as well:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/pattern"
    android:tileMode="repeat" />

But how can I add the color now? You know, I'd like to combine the texture with a color, so that I only have the pattern of the texture and "ignore" or "drop" the color, but instead colorize the pattern with arbitrary colors from code. How is that possible?

Do I have to use the pattern as normal, but then override the onDraw() method of the view and add the color somehow? This answer talks about a BitmapShader, but I have no idea how to use that. Any help?

Was it helpful?

Solution

use Drawable.setColorFilter() method. in most cases you will use a PorterDuffColorFilter or LighingColorFilter

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top