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:com.dunrite.xpaper.utility.Utils.java

/**
 * TODO: DEPRECATE//from  w  w  w. j a v  a 2 s. com
 * Combines two images into one while also coloring each separate image
 *
 * @param background the main background drawable
 * @param foreground the drawable in the front of the background]
 * @param context current context
 * @return colorized and combined drawable
 *
 * can add a 3rd parameter 'String loc' if you want to save the new image.
 * left some code to do that at the bottom
 */
public static Drawable combineImages(Drawable background, Drawable foreground, Drawable deviceMisc, int color1,
        int color2, String type, Context context) {
    Bitmap cs;
    Bitmap device = null;
    int width;
    int height;

    //convert from drawable to bitmap
    Bitmap back = ((BitmapDrawable) background).getBitmap();
    back = back.copy(Bitmap.Config.ARGB_8888, true);

    Bitmap x = ((BitmapDrawable) foreground).getBitmap();
    x = x.copy(Bitmap.Config.ARGB_8888, true);

    if (type.equals("device")) {
        device = ((BitmapDrawable) deviceMisc).getBitmap();
        device = device.copy(Bitmap.Config.ARGB_8888, true);
    }
    //initialize Canvas
    if (type.equals("preview") || type.equals("device")) {
        width = back.getWidth() / 2;
        height = back.getHeight() / 2;
    } else {
        width = back.getWidth();
        height = back.getHeight();
    }
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas comboImage = new Canvas(cs);

    //Filter for Background
    Paint paint1 = new Paint();
    paint1.setFilterBitmap(false);
    paint1.setColorFilter(new PorterDuffColorFilter(color1, PorterDuff.Mode.SRC_ATOP));

    //Filter for Foreground
    Paint paint2 = new Paint();
    paint2.setFilterBitmap(false);
    paint2.setColorFilter(new PorterDuffColorFilter(color2, PorterDuff.Mode.SRC_ATOP));

    //Draw both images
    if (type.equals("preview") || type.equals("device")) {
        if (type.equals("device"))
            comboImage.drawBitmap(
                    Bitmap.createScaledBitmap(device, device.getWidth() / 2, device.getHeight() / 2, true), 0,
                    0, null);
        comboImage.drawBitmap(Bitmap.createScaledBitmap(back, back.getWidth() / 2, back.getHeight() / 2, true),
                0, 0, paint1);
        comboImage.drawBitmap(Bitmap.createScaledBitmap(x, x.getWidth() / 2, x.getHeight() / 2, true), 0, 0,
                paint2);
    } else {
        comboImage.drawBitmap(back, 0, 0, paint1);
        comboImage.drawBitmap(x, 0, 0, paint2);
    }

    return new BitmapDrawable(context.getResources(), cs);
}

From source file:com.android.deskclock.Utils.java

/**
 * For screensavers to dim the lights if necessary.
 *///  w w w. j a  v a 2  s .c  om
public static void dimClockView(boolean dim, View clockView) {
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    paint.setColorFilter(new PorterDuffColorFilter((dim ? 0x40FFFFFF : 0xC0FFFFFF), PorterDuff.Mode.MULTIPLY));
    clockView.setLayerType(View.LAYER_TYPE_HARDWARE, paint);
}

From source file:com.dunrite.xpaper.utility.Utils.java

public static Drawable combineImages2(Drawable background, Drawable foreground, Drawable deviceMisc,
        int backgroundCol, int foregroundCol, String type, Context context) {
    Bitmap cs;// www  .  j  a v  a 2s .  c o m
    Bitmap device = null;
    int width;
    int height;

    //TEXTURE TESTING
    String textureLocation = "";
    Bitmap foregroundTexture = null;
    //TODO: will need some type of way to know which location to put the texture (foreground/background/both)
    //        String textureLocation = "foreground";
    //        type = "";
    //TODO: will need some type of way to know which foreground texture drawable to pull from
    //        Bitmap foregroundTexture = ((BitmapDrawable) ContextCompat.getDrawable(context, R.drawable.texture_bamboo)).getBitmap();
    //        foregroundTexture = foregroundTexture.copy(Bitmap.Config.ARGB_8888, true);

    //convert from drawable to bitmap
    Bitmap back = ((BitmapDrawable) background).getBitmap();
    back = back.copy(Bitmap.Config.ARGB_8888, true);

    Bitmap fore = ((BitmapDrawable) foreground).getBitmap();
    fore = fore.copy(Bitmap.Config.ARGB_8888, true);

    if (type.equals("device")) {
        device = ((BitmapDrawable) deviceMisc).getBitmap();
        device = device.copy(Bitmap.Config.ARGB_8888, true);
    }
    //initialize Canvas
    if (type.equals("preview") || type.equals("device")) {
        width = back.getWidth() / 2;
        height = back.getHeight() / 2;
    } else {
        width = back.getWidth();
        height = back.getHeight();
    }
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas comboImage = new Canvas(cs);

    Paint paint1 = new Paint();
    paint1.setFilterBitmap(false);
    //Filter for Background
    if (textureLocation.equals("background") || textureLocation.equals("both")) {
        paint1.setColorFilter(new PorterDuffColorFilter(backgroundCol, PorterDuff.Mode.DST_ATOP));
    } else {
        paint1.setColorFilter(new PorterDuffColorFilter(backgroundCol, PorterDuff.Mode.SRC_ATOP));
    }

    //Filter for Foreground
    Paint paint2 = new Paint();
    paint2.setFilterBitmap(false);
    if (textureLocation.equals("foreground") || textureLocation.equals("both")) {
        //DIFFICULT CASE
        //create new canvas to combine
        Canvas foreCanvas = new Canvas(fore);

        //set up paint for texture
        Paint paintTexture = new Paint();
        paintTexture.setFilterBitmap(false);
        paintTexture.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        //draw our combination
        foreCanvas.drawBitmap(foregroundTexture, 0, 0, paintTexture);

        //set up theme for outer image
        paint2.setColorFilter(new PorterDuffColorFilter(foregroundCol, PorterDuff.Mode.DST_IN));
    } else {
        paint2.setColorFilter(new PorterDuffColorFilter(foregroundCol, PorterDuff.Mode.SRC_ATOP));
    }

    //Draw both images
    if (type.equals("preview") || type.equals("device")) {
        if (type.equals("device") && device != null) {
            comboImage.drawBitmap(
                    Bitmap.createScaledBitmap(device, device.getWidth() / 2, device.getHeight() / 2, true), 0,
                    0, null);
            device.recycle();
        }
        comboImage.drawBitmap(Bitmap.createScaledBitmap(back, back.getWidth() / 2, back.getHeight() / 2, true),
                0, 0, paint1);
        comboImage.drawBitmap(Bitmap.createScaledBitmap(fore, fore.getWidth() / 2, fore.getHeight() / 2, true),
                0, 0, paint2);

    } else {
        comboImage.drawBitmap(back, 0, 0, paint1);
        comboImage.drawBitmap(fore, 0, 0, paint2);
    }
    back.recycle();
    fore.recycle();

    return new BitmapDrawable(context.getResources(), cs);
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

/**
 * Creates a tinted copy of the supplied bitmap.
 * @param b a bitmap image/*w  ww . j a  v  a2 s  .c o m*/
 * @param color a color
 * @return a bitmap tinted to color
 */
public static Bitmap tintBitmap(Bitmap b, int color) {
    Bitmap tinted = Bitmap.createBitmap(b.getWidth(), b.getHeight(), b.getConfig());
    Canvas c = new Canvas(tinted);
    Paint p = new Paint();
    p.setColorFilter(new LightingColorFilter(color, 0));
    c.drawBitmap(b, 0, 0, p);
    return tinted;
}

From source file:com.dunrite.xpaper.utility.Utils.java

/**
 * Creates drawable for the wallpaper/*from w  w  w .j av a 2s . c  om*/
 * @param context current app context
 * @param foreground foreground drawable
 * @param bgColor color of background
 * @param fgColor color of foreground
 * @param isPreview if this is for the preview or not
 * @return the final, constructed wallpaper
 */
public static Drawable constructWallpaper(Context context, Drawable foreground, int bgColor, int fgColor,
        boolean isPreview) {
    final int WIDTH = 2560;
    final int HEIGHT = 1440;
    Canvas comboImage;
    Bitmap cs, fg;
    Paint fgPaint = new Paint();

    //create bitmap from foreground drawable
    fg = ((BitmapDrawable) foreground).getBitmap();

    if (isPreview)
        cs = Bitmap.createBitmap(WIDTH / 2, HEIGHT / 2, Bitmap.Config.ARGB_8888);
    else
        cs = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888);
    comboImage = new Canvas(cs);

    fgPaint.setFilterBitmap(false);
    fgPaint.setColorFilter(new PorterDuffColorFilter(fgColor, PorterDuff.Mode.SRC_ATOP));

    comboImage.drawRGB(Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor));
    if (isPreview)
        comboImage.drawBitmap(Bitmap.createScaledBitmap(fg, WIDTH / 2, HEIGHT / 2, true), 0, 0, fgPaint);
    else
        comboImage.drawBitmap(fg, 0, 0, fgPaint);

    return new BitmapDrawable(context.getResources(), cs);
}

From source file:com.f2prateek.dfg.core.GenerateFrameService.java

@Override
public void startingImage(Bitmap screenshot) {
    // Create the large notification icon
    int imageWidth = screenshot.getWidth();
    int imageHeight = screenshot.getHeight();
    int iconSize = resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
    final int shortSide = imageWidth < imageHeight ? imageWidth : imageHeight;

    // Check for if config is null, http://crashes.to/s/dd0857c8648
    Bitmap preview = Bitmap.createBitmap(shortSide, shortSide,
            screenshot.getConfig() == null ? Bitmap.Config.ARGB_8888 : screenshot.getConfig());
    Canvas c = new Canvas(preview);
    Paint paint = new Paint();
    ColorMatrix desat = new ColorMatrix();
    desat.setSaturation(0.25f);//from   w  w  w  .j  a  v a 2 s .c o  m
    paint.setColorFilter(new ColorMatrixColorFilter(desat));
    Matrix matrix = new Matrix();
    matrix.postTranslate((shortSide - imageWidth) / 2, (shortSide - imageHeight) / 2);
    c.drawBitmap(screenshot, matrix, paint);
    c.drawColor(0x40FFFFFF);

    Bitmap croppedIcon = Bitmap.createScaledBitmap(preview, iconSize, iconSize, true);

    Intent nullIntent = new Intent(this, MainActivity.class);
    nullIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationBuilder = new NotificationCompat.Builder(this)
            .setTicker(resources.getString(R.string.screenshot_saving_ticker))
            .setContentTitle(resources.getString(R.string.screenshot_saving_title))
            .setSmallIcon(R.drawable.ic_stat_app_notification)
            .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(preview))
            .setContentIntent(PendingIntent.getActivity(this, 0, nullIntent, 0))
            .setWhen(System.currentTimeMillis()).setProgress(0, 0, true).setLargeIcon(croppedIcon);

    Notification n = notificationBuilder.build();
    n.flags |= Notification.FLAG_NO_CLEAR;
    notificationManager.notify(DFG_NOTIFICATION_ID, n);
}

From source file:com.sspai.dkjt.service.GenerateFrameService.java

@Override
public void startingImage(Bitmap screenshot) {
    // Create the large notification icon
    int imageWidth = screenshot.getWidth();
    int imageHeight = screenshot.getHeight();
    int iconSize = resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
    final int shortSide = imageWidth < imageHeight ? imageWidth : imageHeight;

    // Check for if config is null, http://crashes.to/s/dd0857c8648
    Bitmap preview = Bitmap.createBitmap(shortSide, shortSide,
            screenshot.getConfig() == null ? Bitmap.Config.ARGB_8888 : screenshot.getConfig());
    Canvas c = new Canvas(preview);
    Paint paint = new Paint();
    ColorMatrix desat = new ColorMatrix();
    desat.setSaturation(0.25f);/*www .j  av  a  2 s . co m*/
    paint.setColorFilter(new ColorMatrixColorFilter(desat));
    Matrix matrix = new Matrix();
    matrix.postTranslate((shortSide - imageWidth) / 2, (shortSide - imageHeight) / 2);
    c.drawBitmap(screenshot, matrix, paint);
    c.drawColor(0x40FFFFFF);

    Bitmap croppedIcon = Bitmap.createScaledBitmap(preview, iconSize, iconSize, true);

    Intent nullIntent = new Intent(this, MainActivity.class);
    nullIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationBuilder = new NotificationCompat.Builder(this)
            .setTicker(resources.getString(R.string.screenshot_saving_ticker))
            .setContentTitle(resources.getString(R.string.screenshot_saving_title))
            .setSmallIcon(R.drawable.ic_actionbar_logo)
            .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(preview))
            .setContentIntent(PendingIntent.getActivity(this, 0, nullIntent, 0))
            .setWhen(System.currentTimeMillis()).setProgress(0, 0, true).setLargeIcon(croppedIcon);

    Notification n = notificationBuilder.build();
    n.flags |= Notification.FLAG_NO_CLEAR;
    notificationManager.notify(DFG_NOTIFICATION_ID, n);
}

From source file:com.channelsoft.common.bitmapUtil.ImageWorker.java

/**
* ?/*from  w  w  w .  j a va  2 s.  c  o  m*/
* @param bmpOriginal
* @return
*/
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();
    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;
}

From source file:com.crystal.CrystalBeanWallpapers.Wallpaper.java

private Bitmap getColoredBitmap(Bitmap src, int color) {
    int width = src.getWidth();
    int height = src.getHeight();

    Bitmap dest = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    Canvas canvas = new Canvas(dest);
    Paint paint = new Paint();
    paint.setColorFilter(new PorterDuffColorFilter(color, Mode.OVERLAY));
    canvas.drawBitmap(src, 0, 0, paint);

    return dest;//  w w w .  j a va 2  s  . co  m
}

From source file:org.kiwix.kiwixmobile.views.KiwixWebView.java

public void toggleNightMode() {
    Paint paint = new Paint();
    ColorMatrixColorFilter filterInvert = new ColorMatrixColorFilter(NIGHT_MODE_COLORS);
    paint.setColorFilter(filterInvert);
    setLayerType(View.LAYER_TYPE_HARDWARE, paint);
}