Example usage for android.graphics Paint setColorFilter

List of usage examples for android.graphics Paint setColorFilter

Introduction

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

Prototype

public ColorFilter setColorFilter(ColorFilter filter) 

Source Link

Document

Set or clear the paint's colorfilter, returning the parameter.

Usage

From source file:Main.java

/**
 * Adds tint effect to target bitmap.//from w ww . j  ava  2 s  .c  o  m
 *
 * @param src
 *            PNG bitmap to apply tint affect
 * @param argb
 *            tint color
 * @return bitmap with tint affect
 */
public static Bitmap addTintEffect(Bitmap src, int argb) {
    Paint paint = new Paint();
    paint.setColorFilter(new PorterDuffColorFilter(argb, PorterDuff.Mode.SRC_OVER));

    Canvas bottomCanvas = new Canvas(src);
    bottomCanvas.drawBitmap(src, 0, 0, paint);

    return src;
}

From source file:Main.java

/**
 * Copy the red channel to a new Bitmap's alpha channel.  The old
 * one will be recycled./*w  ww  .java2  s  . c o  m*/
 */
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

static public void setImageColor(ImageView view, Bitmap sourceBitmap, int rgbcolor)// ,Bitmap sourceBitmap)
{
    if (sourceBitmap != null) {
        float R = Color.red(rgbcolor);
        float G = Color.green(rgbcolor);
        float B = Color.blue(rgbcolor);

        Log.v("R:G:B", R + ":" + G + ":" + B); //

        float[] colorTransform = { R / 255f, 0, 0, 0, 0, // R color
                0, G / 255f, 0, 0, 0 // G color
                , 0, 0, B / 255f, 0, 0 // B color
                , 0, 0, 0, 1f, 0f };//from www  . j a  v  a2s .c  om

        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0f); // Remove Colour

        colorMatrix.set(colorTransform);
        ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
        Paint paint = new Paint();
        paint.setColorFilter(colorFilter);

        Bitmap mutableBitmap = sourceBitmap.copy(Bitmap.Config.ARGB_8888, true);
        view.setImageBitmap(mutableBitmap);
        Canvas canvas = new Canvas(mutableBitmap);
        canvas.drawBitmap(mutableBitmap, 0, 0, paint);

    }
}

From source file:Main.java

public static int filter(final ColorFilter filter, final int color) {
    final Paint paint = new Paint();
    paint.setColor(color);//from w  w w .j  a va 2s.c o m
    paint.setColorFilter(filter);
    final Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    canvas.drawPaint(paint);
    return bitmap.getPixel(0, 0);
}

From source file:Main.java

public static Bitmap changeBitmapColor(@NonNull Bitmap sourceBitmap, @IntegerRes int color) {

    Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth() - 1,
            sourceBitmap.getHeight() - 1);
    Paint p = new Paint();
    ColorFilter filter = new LightingColorFilter(color, 1);
    p.setColorFilter(filter);

    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(resultBitmap, 0, 0, p);
    return resultBitmap;
}

From source file:Main.java

public static Bitmap toGreyBitmap(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);//from   w  ww. j  a  va2  s.co  m
    p.setColorFilter(new ColorMatrixColorFilter(cm));
    c.drawBitmap(bitmap, 0, 0, p);
    return grey;
}

From source file:Main.java

@SuppressWarnings("deprecation")
private static Drawable getSelectedDrawable(BitmapDrawable mDrawable) {
    Bitmap srcBitmap = mDrawable.getBitmap();
    Bitmap bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Config.ARGB_8888);
    ColorMatrix cMatrix = new ColorMatrix();
    cMatrix.set(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 });//from  w  w w.  java  2 s.  c o  m
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cMatrix));
    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return new BitmapDrawable(bmp);
}

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);//  w  ww  .java 2  s .  c o m
    p.setColorFilter(new ColorMatrixColorFilter(cm));
    c.drawBitmap(bitmap, 0, 0, p);
    return grey;
}

From source file:Main.java

public static Bitmap handleColorMatrix(Bitmap bm, float[] matrixs) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(matrixs);// www .j ava  2  s  .c o m
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

    canvas.drawBitmap(bm, 0, 0, paint);

    return bitmap;
}

From source file:Main.java

public static Bitmap convertGrayscale(final Bitmap source) {
    final int width = source.getWidth();
    final int height = source.getHeight();
    final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0f);/*w w w.j av  a2 s  .  c o  m*/
    paint.setColorFilter(new ColorMatrixColorFilter(matrix));
    canvas.drawBitmap(source, 0, 0, paint);
    return output;
}