Last active
August 29, 2015 14:06
-
-
Save StelianMorariu/b523c5244a83ceda8e9d to your computer and use it in GitHub Desktop.
Android - create gray scale image from drawable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Drawable getGrayScaleDrawable() { | |
int id = getAreaCharacterId(); | |
Bitmap bmp = BitmapFactory.decodeResource(getResources(),id); | |
int width = bmp.getWidth(); | |
int height = bmp.getHeight(); | |
Bitmap grayScale = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888); | |
Canvas c = new Canvas(grayScale); | |
Paint paint = new Paint(); | |
ColorMatrix cm = new ColorMatrix(); | |
cm.setSaturation(0); | |
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); | |
paint.setColorFilter(f); | |
c.drawBitmap(bmp, 0, 0, paint); | |
Drawable grayed = new BitmapDrawable(getResources(),grayScale); | |
bmp.recycle(); | |
// grayScale.recycle(); | |
return grayed; | |
} | |
private int getAreaCharacterId() { | |
int id = 0; | |
switch (mSelected){ | |
case 1: | |
id = R.drawable.angel; | |
break; | |
case 2: | |
id =R.drawable.ginger; | |
break; | |
case 3: | |
id =R.drawable.snowman; | |
break; | |
case 4: | |
id =R.drawable.reindeer; | |
break; | |
case 5: | |
id =R.drawable.santa; | |
break; | |
case 6: | |
id =R.drawable.elf; | |
break; | |
} | |
return id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment