Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lighten or darken a bitmap

Tags:

java

android

How do I take an existing bitmap, say

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.somebitmap);

and write a method that returns a darkened version of the bitmap?

private Bitmap darkenBitMap(Bitmap bm) { }

I've been trying to use Paint and Canvas with no results so far.

like image 765
the_prole Avatar asked Jan 02 '16 03:01

the_prole


2 Answers

To make view darker.

canvas.drawARGB(200, 0, 0, 0);

Short and simple :)

like image 63
oxied Avatar answered Oct 09 '22 17:10

oxied


I got it finally. Hope it helps someone else.

private Bitmap darkenBitMap(Bitmap bm) {

    Canvas canvas = new Canvas(bm);
    Paint p = new Paint(Color.RED);
    //ColorFilter filter = new LightingColorFilter(0xFFFFFFFF , 0x00222222); // lighten
    ColorFilter filter = new LightingColorFilter(0xFF7F7F7F, 0x00000000);    // darken
    p.setColorFilter(filter);
    canvas.drawBitmap(bm, new Matrix(), p);

    return bm;
}
like image 38
the_prole Avatar answered Oct 09 '22 18:10

the_prole