Example usage for android.graphics ColorMatrix ColorMatrix

List of usage examples for android.graphics ColorMatrix ColorMatrix

Introduction

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

Prototype

public ColorMatrix(ColorMatrix src) 

Source Link

Document

Create a new colormatrix initialized with the specified colormatrix.

Usage

From source file:Main.java

public static Bitmap setBrightness(Bitmap srcBitmap, int brightness) {
    Bitmap bitmap = Bitmap.createBitmap(srcBitmap);
    ColorMatrix cm = new ColorMatrix(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0,
            brightness, 0, 0, 0, 1, 0 });
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap setContrast(Bitmap srcBitmap, float contrast) {
    Bitmap bitmap = Bitmap.createBitmap(srcBitmap);
    ColorMatrix cm = new ColorMatrix(
            new float[] { contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, 1, 0 });
    cm.setSaturation(contrast);/*from  w  ww .j  av a2s. c o  m*/
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

/**
 * /*from   w ww .  j  a  v a  2 s.co  m*/
 * @param paint
 */
public static void invertCanvasColors(Paint paint) {
    float mx[] = { -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f,
            1.0f, 1.0f, 1.0f, 1.0f, 0.0f };
    ColorMatrix cm = new ColorMatrix(mx);
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
}

From source file:Main.java

public static ColorFilter createColorFilterForOpacity(float opacity) {
    // 5x4 identity color matrix + fade the alpha to achieve opacity
    float[] matrix = { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, opacity, 0 };

    return new ColorMatrixColorFilter(new ColorMatrix(matrix));
}

From source file:Main.java

public static final Bitmap alpha(Bitmap bitmap, int alpha) {
    float[] matrixItems = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, alpha / 255f, 0,
            0, 0, 0, 0, 1 };//from   ww w  . j  av  a 2 s  . c  om
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap alphaBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(alphaBitmap);
    Paint paint = new Paint();
    ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    paint.setColorFilter(colorMatrixFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return alphaBitmap;
}

From source file:Main.java

public static void setGrayFilter(Drawable draw) {
    ColorMatrix cm = new ColorMatrix(new float[] { 0.299f, 0.587f, 0.114f, 0, 0, 0.299f, 0.587f, 0.114f, 0, 0,
            0.299f, 0.587f, 0.114f, 0, 0, 0, 0, 0, 1, 0 });
    setMatrixColorFilter(cm, draw);/*  w ww.j  a  v  a2s  .c  om*/
}

From source file:Main.java

public static void setReverseFilter(Drawable draw) {
    ColorMatrix cm = new ColorMatrix(
            new float[] { -1, 0, 0, 0, 255, 0, -1, 0, 0, 255, 0, 0, -1, 0, 255, 0, 0, 0, 1, 0 });
    setMatrixColorFilter(cm, draw);//www. java 2s. com
}

From source file:Main.java

/**
 * /* ww w  .j ava2s.  c om*/
 * @param paint
 */
public static void setThreshold(Paint paint, float threshold) {
    /*
     * Elevation matrix. This will threshold the elevation with GPS altitude.
     * The factor is used to increase the brightness for a given elevation map.
     * Elevation map is prepared so that altitudes from 0-5000 meter are encoded with 0-200 pixel values.
     * Each pixel level is 25 meter. 
     * 
     * Negative sign for black threshold instead of white.
     * Threshold of to 0 to 100 translated to 0 - 200 for all pixels thresholded at 5000 meters.
     * 
     * Calibrated (visually) at 
     * KRNO - 1346 meters, threshold = 28
     * KLXV - 3027 meters, threshold = 61
     * L70  - 811  meters, threshold = 16
     * KHIE - 326  meters, threshold = 7
     *--------------------------------------------
     *       5510                    = 112  ~ 50 meters per px
     * Formula to calculate threshold is:
     * threshold = altitude / 3 (meters per foot) / 50
     * Give 2 levels margin of safety
     */
    float factor = 4.f;
    float mx[] = { factor, 0, 0, 0, -(factor) * (threshold - 5) * 2.0f, 0, factor / 1.5f, 0, 0,
            -(factor) * (threshold - 5) * 2.0f, 0, 0, factor / 2.0f, 0, -(factor) * (threshold - 5) * 2.0f, 0,
            0, 0, 1, 0 };
    ColorMatrix cm = new ColorMatrix(mx);
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
}

From source file:Main.java

/**
 * Copy the red channel to a new Bitmap's alpha channel.  The old
 * one will be recycled.//from w  ww  .ja  v  a 2  s  .c  om
 */
static Bitmap redToAlpha(Bitmap src, boolean recycle) {
    Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);

    float[] matrix = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 };

    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(matrix)));

    Canvas canvas = new Canvas(dest);
    canvas.setDensity(Bitmap.DENSITY_NONE);
    canvas.drawBitmap(src, 0, 0, paint);

    if (recycle)
        src.recycle();
    return dest;
}

From source file:Main.java

private static Drawable getSelectedDrawable(GradientDrawable mDrawable) {
    GradientDrawable gradientDrawable = (GradientDrawable) mDrawable.getConstantState().newDrawable();
    gradientDrawable.mutate();/*  w ww .  j  a  v  a  2s . c o m*/
    gradientDrawable.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(new float[] { 1, 0, 0, 0,
            -DRAW_DEEP, 0, 1, 0, 0, -DRAW_DEEP, 0, 0, 1, 0, -DRAW_DEEP, 0, 0, 0, 1, 0 })));
    return gradientDrawable;
}