Example usage for android.graphics PorterDuffColorFilter PorterDuffColorFilter

List of usage examples for android.graphics PorterDuffColorFilter PorterDuffColorFilter

Introduction

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

Prototype

public PorterDuffColorFilter(@ColorInt int color, @NonNull PorterDuff.Mode mode) 

Source Link

Document

Create a color filter that uses the specified color and Porter-Duff mode.

Usage

From source file:co.ceryle.segmentedbutton.SegmentedButton.java

/**
 * If button has any drawable, it sets drawable's tint color without changing drawable's position.
 *
 * @param color is used to set drawable's tint color
 *///from  www  . ja  v  a2  s .  c  o  m
public void setDrawableTint(int color) {
    int pos = 0;
    Drawable drawable = null;

    if (getCompoundDrawables().length > 0) {
        for (int i = 0; i < getCompoundDrawables().length; i++) {
            if (getCompoundDrawables()[i] != null) {
                pos = i;
                drawable = getCompoundDrawables()[i];
            }
        }
        if (drawable != null)
            drawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
        setDrawable(drawable, pos);
    }
}

From source file:org.xbmc.kore.ui.RemoteFragment.java

@TargetApi(21)
private void adjustRemoteButtons() {
    Resources.Theme theme = getActivity().getTheme();
    TypedArray styledAttributes = theme.obtainStyledAttributes(
            new int[] { R.attr.remoteButtonColorFilter, R.attr.contentBackgroundColor });
    //                R.attr.remoteBackgroundColorFilter});
    Resources resources = getResources();
    int remoteButtonsColor = styledAttributes.getColor(styledAttributes.getIndex(0),
            resources.getColor(R.color.white)),
            remoteBackgroundColor = styledAttributes.getColor(styledAttributes.getIndex(1),
                    resources.getColor(R.color.dark_content_background_dim_70pct));
    styledAttributes.recycle();/*from  w  w  w. j  a  v a 2s.c om*/

    leftButton.setColorFilter(remoteButtonsColor);
    rightButton.setColorFilter(remoteButtonsColor);
    upButton.setColorFilter(remoteButtonsColor);
    downButton.setColorFilter(remoteButtonsColor);

    selectButton.setColorFilter(remoteButtonsColor);
    backButton.setColorFilter(remoteButtonsColor);
    infoButton.setColorFilter(remoteButtonsColor);
    osdButton.setColorFilter(remoteButtonsColor);
    contextButton.setColorFilter(remoteButtonsColor);

    // On ICS the remote background isn't shown as the tinting isn't supported
    //int backgroundResourceId = R.drawable.remote_background_square_black_alpha;
    int backgroundResourceId = R.drawable.remote_background_square_black;
    if (Utils.isLollipopOrLater()) {
        remotePanel.setBackgroundTintList(ColorStateList.valueOf(remoteBackgroundColor));
        remotePanel.setBackgroundResource(backgroundResourceId);
    } else if (Utils.isJellybeanOrLater()) {
        BitmapDrawable background = new BitmapDrawable(getResources(),
                BitmapFactory.decodeResource(getResources(), backgroundResourceId));
        background.setColorFilter(new PorterDuffColorFilter(remoteBackgroundColor, PorterDuff.Mode.SRC_IN));
        remotePanel.setBackground(background);
    }
}

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

/**
 * TODO: DEPRECATE/* w  ww .  j av  a 2s. 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:co.ceryle.segmentedbutton.SegmentedButton.java

/**
 * If button has any drawable, it sets drawable's tint color without changing drawable's position.
 *
 * @param drawable is directly set to button's drawable
 * @param position specifies button's drawable position relative to text position.
 *                 These values can be given to position:
 *                 {@link #DRAWABLE_LEFT} sets drawable to the left of button's text
 *                 {@link #DRAWABLE_TOP} sets drawable to the top of button's text
 *                 {@link #DRAWABLE_RIGHT} sets drawable to the right of button's text
 *                 {@link #DRAWABLE_BOTTOM} sets drawable to the bottom of button's text
 * @param color    is used to set drawable's tint color
 *//*from w w  w .  j  a  va 2  s  .c om*/
public void setDrawableTint(Drawable drawable, int position, int color) {
    drawable.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    setDrawable(drawable, position);
}

From source file:me.lizheng.deckview.views.DeckChildView.java

/**
 * Returns the current dim./* w w  w  .j a  v  a  2  s  . c  o  m*/
 */
public void setDim(int dim) {
    mDimAlpha = dim;
    if (mConfig.useHardwareLayers) {
        // Defer setting hardware layers if we have not yet measured, or there is no dim to draw
        if (getMeasuredWidth() > 0 && getMeasuredHeight() > 0) {
            mDimColorFilter = new PorterDuffColorFilter(Color.argb(mDimAlpha, 0, 0, 0),
                    PorterDuff.Mode.SRC_ATOP);
            mDimLayerPaint.setColorFilter(mDimColorFilter);
            mContent.setLayerType(LAYER_TYPE_HARDWARE, mDimLayerPaint);
        }
    } else {
        float dimAlpha = mDimAlpha / 255.0f;
        if (mThumbnailView != null) {
            mThumbnailView.setDimAlpha(dimAlpha);
        }
    }
}

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;/* ww  w.  j  av a 2 s  .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.markupartist.sthlmtraveling.ViewOnMapActivity.java

BitmapDescriptor getColoredMarker(@ColorInt int colorInt, @DrawableRes int drawableRes) {
    Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), drawableRes);
    Bitmap bitmapCopy = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    Canvas canvas = new Canvas(bitmapCopy);
    Paint paint = new Paint();
    paint.setColorFilter(new PorterDuffColorFilter(colorInt, PorterDuff.Mode.SRC_ATOP));
    canvas.drawBitmap(bitmap, 0f, 0f, paint);
    return BitmapDescriptorFactory.fromBitmap(bitmapCopy);
}

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

/**
 * For screensavers to dim the lights if necessary.
 *//*from   w  w  w  .  j a  va2 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:net.bluehack.ui.WallpapersActivity.java

private void processSelectedBackground() {
    TLRPC.WallPaper wallPaper = wallpappersByIds.get(selectedBackground);
    if (selectedBackground != -1 && selectedBackground != 1000001 && wallPaper != null
            && wallPaper instanceof TLRPC.TL_wallPaper) {
        int width = AndroidUtilities.displaySize.x;
        int height = AndroidUtilities.displaySize.y;
        if (width > height) {
            int temp = width;
            width = height;/*www.  j  a  v  a 2s . c o m*/
            height = temp;
        }
        TLRPC.PhotoSize size = FileLoader.getClosestPhotoSizeWithSize(wallPaper.sizes, Math.min(width, height));
        if (size == null) {
            return;
        }
        String fileName = size.location.volume_id + "_" + size.location.local_id + ".jpg";
        File f = new File(FileLoader.getInstance().getDirectory(FileLoader.MEDIA_DIR_CACHE), fileName);
        if (!f.exists()) {
            int result[] = AndroidUtilities.calcDrawableColor(backgroundImage.getDrawable());
            progressViewBackground.getBackground()
                    .setColorFilter(new PorterDuffColorFilter(result[0], PorterDuff.Mode.MULTIPLY));
            loadingFile = fileName;
            loadingFileObject = f;
            doneButton.setEnabled(false);
            progressView.setVisibility(View.VISIBLE);
            loadingSize = size;
            selectedColor = 0;
            FileLoader.getInstance().loadFile(size, null, true);
            backgroundImage.setBackgroundColor(0);
        } else {
            if (loadingFile != null) {
                FileLoader.getInstance().cancelLoadFile(loadingSize);
            }
            loadingFileObject = null;
            loadingFile = null;
            loadingSize = null;
            try {
                backgroundImage.setImageURI(Uri.fromFile(f));
            } catch (Throwable e) {
                FileLog.e("tmessages", e);
            }
            backgroundImage.setBackgroundColor(0);
            selectedColor = 0;
            doneButton.setEnabled(true);
            progressView.setVisibility(View.GONE);
        }
    } else {
        if (loadingFile != null) {
            FileLoader.getInstance().cancelLoadFile(loadingSize);
        }
        if (selectedBackground == 1000001) {
            backgroundImage.setImageResource(R.drawable.background_hd);
            backgroundImage.setBackgroundColor(0);
            selectedColor = 0;
        } else if (selectedBackground == -1) {
            File toFile = new File(ApplicationLoader.getFilesDirFixed(), "wallpaper-temp.jpg");
            if (!toFile.exists()) {
                toFile = new File(ApplicationLoader.getFilesDirFixed(), "wallpaper.jpg");
            }
            if (toFile.exists()) {
                backgroundImage.setImageURI(Uri.fromFile(toFile));
            } else {
                selectedBackground = 1000001;
                processSelectedBackground();
            }
        } else {
            if (wallPaper == null) {
                return;
            }
            if (wallPaper instanceof TLRPC.TL_wallPaperSolid) {
                Drawable drawable = backgroundImage.getDrawable();
                backgroundImage.setImageBitmap(null);
                selectedColor = 0xff000000 | wallPaper.bg_color;
                backgroundImage.setBackgroundColor(selectedColor);
            }
        }
        loadingFileObject = null;
        loadingFile = null;
        loadingSize = null;
        doneButton.setEnabled(true);
        progressView.setVisibility(View.GONE);
    }
}

From source file:com.gigamole.library.NavigationTabBar.java

public void setInactiveColor(final int inactiveColor) {
    mInactiveColor = inactiveColor;// www  .  j  a va 2  s.co  m

    // Set color filter to wrap icons with inactive color
    mIconPaint.setColorFilter(new PorterDuffColorFilter(inactiveColor, PorterDuff.Mode.SRC_IN));
    mModelTitlePaint.setColor(mInactiveColor);
    postInvalidate();
}