Old Fashion Effect Bitmap - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

Old Fashion 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 huaijiuEffect(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap base = Bitmap/*  ww w  .j  av  a  2s  .com*/
                .createBitmap(width, height, bitmap.getConfig());

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

        int newR, newG, newB;
        for (int i = 0; i < width; i++)//
        {
            for (int j = 0; j < height; j++) {
                int current_color = bitmap.getPixel(i, j);
                int r = Color.red(current_color);
                int g = Color.green(current_color);
                int b = Color.blue(current_color);
                int a = Color.alpha(current_color);

                /**/
                newR = (int) (0.393 * r + 0.769 * g + 0.189 * b);
                newG = (int) (0.349 * r + 0.686 * g + 0.168 * b);
                newB = (int) (0.272 * r + 0.534 * g + 0.131 * b);

                int newColor = Color.argb(a, newR > 255  255 : newR,
                        newG > 255  255 : newG, newB > 255  255 : newB);
                base.setPixel(i, j, newColor);
            }
        }
        return base;
    }
}

Related Tutorials