Gray Effect Bitmap - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

Gray Effect Bitmap

Demo Code


//package com.java2s;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;

public class Main {

    public static Bitmap huiduEffect(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap base = Bitmap// w w  w.  j av  a  2  s.  c o  m
                .createBitmap(width, height, bitmap.getConfig());

        Canvas canvas = new Canvas(base);//basecanvas
        canvas.drawBitmap(bitmap, new Matrix(), new Paint());

        for (int i = 0; i < width; i++)//
        {
            for (int j = 0; j < height; j++) {
                int color = bitmap.getPixel(i, j);

                int r = Color.red(color);
                int g = Color.green(color);
                int b = Color.blue(color);
                int a = Color.alpha(color);

                int value = (int) (r * 0.3 + g * 0.59 + b * 0.11);//
                int newColor = Color.argb(a, value, value, value);
                base.setPixel(i, j, newColor);
            }
        }
        return base;
    }
}

Related Tutorials