Example usage for android.graphics ColorMatrix setSaturation

List of usage examples for android.graphics ColorMatrix setSaturation

Introduction

In this page you can find the example usage for android.graphics ColorMatrix setSaturation.

Prototype

public void setSaturation(float sat) 

Source Link

Document

Set the matrix to affect the saturation of colors.

Usage

From source file:Main.java

public static Drawable toGreyDrawable(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return drawable;
    }/*from   w  w w  .j av  a2s.co  m*/
    Rect bounds = drawable.getBounds();
    Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    drawable.setColorFilter(new ColorMatrixColorFilter(cm));
    drawable.setBounds(0, 0, w, h);
    drawable.draw(c);
    drawable.clearColorFilter();
    drawable.setBounds(bounds);
    BitmapDrawable bd = new BitmapDrawable(grey);
    bd.setBounds(0, 0, w, h);
    return bd;
}

From source file:Main.java

/**
 * Convert bitmap to the grayscale/*from www .  jav a2 s  . c  o  m*/
 * http://androidsnippets.com/convert-bitmap-to-grayscale
 *
 * @param bmpOriginal Original bitmap
 * @return Grayscale bitmap
 */
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    final int height = bmpOriginal.getHeight();
    final int width = bmpOriginal.getWidth();

    final Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas c = new Canvas(bmpGrayscale);
    final Paint paint = new Paint();
    final ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    final ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap toGrey(Bitmap bitmap) {
    Bitmap grey = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    Paint p = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    p.setColorFilter(new ColorMatrixColorFilter(cm));
    c.drawBitmap(bitmap, 0, 0, p);/*from   ww  w  . ja  va 2  s.  co  m*/
    return grey;
}

From source file:Main.java

public static Bitmap toSepia(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();//from  w  ww .j a va2s.  c  o  m
    width = bmpOriginal.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();

    ColorMatrix grMatrix = new ColorMatrix();
    grMatrix.setSaturation(0);

    ColorMatrix scMatrix = new ColorMatrix();
    scMatrix.setScale(1f, .85f, .72f, 1.0f);
    grMatrix.setConcat(scMatrix, grMatrix);

    ColorMatrixColorFilter f = new ColorMatrixColorFilter(grMatrix);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap greyScale(Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    ColorMatrix saturation = new ColorMatrix();
    saturation.setSaturation(0f);
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(saturation));
    canvas.drawBitmap(source, 0, 0, paint);
    source.recycle();/*from  w  w  w.  ja va  2 s. c  o  m*/

    if (source != bitmap) {
        source.recycle();
    }

    return bitmap;
}

From source file:Main.java

private static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();//from  w  w  w .  ja  va2 s.com
    width = bmpOriginal.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);

    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();/*  w w  w . j  a v a2s. c  o m*/
    width = bmpOriginal.getWidth();
    try {
        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    } catch (OutOfMemoryError e) {
        return bmpOriginal;
    }
}

From source file:Main.java

public static Drawable toGrey(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return drawable;
    }//w w w . java2  s  .co m
    Rect bounds = drawable.getBounds();
    Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    drawable.setColorFilter(new ColorMatrixColorFilter(cm));
    drawable.setBounds(0, 0, w, h);
    drawable.draw(c);
    drawable.clearColorFilter();
    drawable.setBounds(bounds);
    BitmapDrawable bd = new BitmapDrawable(grey);
    bd.setBounds(0, 0, w, h);
    return bd;
}

From source file:Main.java

public static Bitmap getNewCombinedByPiecesAlsoGrayscaled(List<Bitmap> bitmapList, int currentStage,
        int numStages) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*  w ww.j a  v  a 2  s .  c o m*/
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(colorMatrixColorFilter);

    //i mean, don't use greyscale, but add here all the functionalities to reuse the canvas
    int originalTotalWidth = bitmapList.get(0).getWidth() * numStages;
    Bitmap finalBitmap = Bitmap.createBitmap(originalTotalWidth, bitmapList.get(0).getHeight(),
            Bitmap.Config.ARGB_8888);
    float delta = 0f;
    Canvas comboImage = new Canvas(finalBitmap);
    for (int i = 0; i < numStages; i++) {
        comboImage.translate(delta, 0f);
        if (i > currentStage) {
            comboImage.drawBitmap(bitmapList.get(i), 0f, 0f, paint);
        } else {
            comboImage.drawBitmap(bitmapList.get(i), 0f, 0f, null);
        }
        delta = originalTotalWidth / numStages;
    }
    return finalBitmap;
}

From source file:Main.java

public static Bitmap toGrayScale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();// ww w.  j a v a2  s . c om
    width = bmpOriginal.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}