draw Altered Image - Android Graphics

Android examples for Graphics:Image Shape

Description

draw Altered Image

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;

import android.graphics.Bitmap.Config;

public class Main {
    public static Bitmap drawAlteredImage(Bitmap bmp, float contr,
            float bright, float satur) {
        Bitmap alteredBitmap = drawAlteredImage(bmp,
                changeColorFilter(contr, bright, satur));
        return alteredBitmap;
    }//from w w  w  . j  a v  a2  s . c  o m

    public static Bitmap drawAlteredImage(Bitmap bmp, float contr,
            float bright, float satur, Config config) {
        Bitmap alteredBitmap = drawAlteredImage(bmp,
                changeColorFilter(contr, bright, satur), config);
        return alteredBitmap;
    }

    public static Bitmap drawAlteredImage(Bitmap bmp, ColorFilter filter) {

        return drawAlteredImage(bmp, filter, bmp.getConfig());
    }

    public static Bitmap drawAlteredImage(Bitmap bmp, ColorFilter filter,
            Config config) {
        Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),
                bmp.getHeight(), config);
        Canvas canvas = new Canvas(alteredBitmap);
        Paint paint = new Paint();
        paint.setColorFilter(filter);
        Matrix matrix = new Matrix();
        canvas.drawBitmap(bmp, matrix, paint);
        return alteredBitmap;
    }

    public static ColorMatrixColorFilter changeColorFilter(float contr,
            float bright, float satur) {
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(satur);
        final float m[] = cm.getArray();
        final float c = contr;
        cm.set(new float[] { m[0] * c, m[1] * c, m[2] * c, m[3] * c,
                m[4] * c + bright, m[5] * c, m[6] * c, m[7] * c, m[8] * c,
                m[9] * c + bright, m[10] * c, m[11] * c, m[12] * c,
                m[13] * c, m[14] * c + bright, m[15], m[16], m[17], m[18],
                m[19] });

        return new ColorMatrixColorFilter(cm);
    }
}

Related Tutorials