Skip to content

Instantly share code, notes, and snippets.

@oksep
Created April 1, 2016 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oksep/8bd80643a1fe2d27aa39142885f00b8b to your computer and use it in GitHub Desktop.
Save oksep/8bd80643a1fe2d27aa39142885f00b8b to your computer and use it in GitHub Desktop.
ImageUtil
public class ImageUtil {
public static void setColorFilter(ImageView imageView, int color) {
final float r = Color.red(color) / 255f;
final float g = Color.green(color) / 255f;
final float b = Color.blue(color) / 255f;
final float a = Color.alpha(color) / 255f;
setColorFilter(imageView, r, g, b, a);
}
public static void setColorFilter(ImageView imageView, float r, float g, float b, float a) {
/*
* 5x4 matrix for transforming the color+alpha components of a Bitmap.
* The matrix is stored in a single array, and its treated as follows:
* [ a, b, c, d, e,
* f, g, h, i, j,
* k, l, m, n, o,
* p, q, r, s, t ]
*
* When applied to a color [r, g, b, a], the resulting color is computed
* as (after clamping)
* R' = a*R + b*G + c*B + d*A + e;
* G' = f*R + g*G + h*B + i*A + j;
* B' = k*R + l*G + m*B + n*A + o;
* A' = p*R + q*G + r*B + s*A + t;
*/
float[] colorMatrix = { //
r, 0, 0, 0, 0, //red
0, g, 0, 0, 0, //green
0, 0, b, 0, 0, //blue
0, 0, 0, a, 0 //alpha
};
ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
imageView.setColorFilter(colorFilter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment