Java Code Examples for android.graphics.drawable.BitmapDrawable#setColorFilter()

The following examples show how to use android.graphics.drawable.BitmapDrawable#setColorFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Styler.java    From StyleImageView with Apache License 2.0 6 votes vote down vote up
/**
 * Method to add style to bitmap
 *
 * @param context
 * @param bitmap Bitmap object to change, we don't operate on this bitmap because it's immutable, you should use the returned bitmap object
 * @param mode
 * @param brightness if you don't want to change brightness, pass 0
 * @param contrast if you don't want to change contrast, pass 1
 * @param saturation if you don't want to change saturation, pass 1. If saturation is set, then the mode must be Styler.Mode.SATURATION
 * @return
 */
public static Bitmap addStyleToBitmap(Context context, Bitmap bitmap, int mode, int brightness, float contrast, float saturation) {
    if (saturation != 1 && mode != Mode.SATURATION && mode != Mode.NONE) {
        throw new IllegalArgumentException("saturation must be 1.0 when mode is not Styler.Mode.SATURATION");
    }
    if (brightness > 255) {
        throw new IllegalArgumentException("brightness can't be bigger than 255");
    } else if (brightness < -255) {
        throw new IllegalArgumentException("brightness can't be smaller than -255");
    }
    if (contrast < 0) {
        throw new IllegalArgumentException("contrast can't be smaller than 0");
    }
    if (saturation < 0) {
        throw new IllegalArgumentException("saturation can't be smaller than 0");
    }
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    context = context.getApplicationContext();
    BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap);
    drawable.setColorFilter(new ColorMatrixColorFilter(calculateMatrix(mode, brightness, contrast, saturation)));
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
    drawable.draw(canvas);
    return newBitmap;
}
 
Example 2
Source File: ControlPad.java    From Kore with Apache License 2.0 6 votes vote down vote up
@TargetApi(21)
private void setBackgroundImage() {
    Resources.Theme theme = getContext().getTheme();
    TypedArray styledAttributes = theme.obtainStyledAttributes(new int[] {R.attr.contentBackgroundColor});
    int remoteBackgroundColor = styledAttributes.getColor(styledAttributes.getIndex(0),
                                                          getResources().getColor(R.color.dark_content_background_dim));
    styledAttributes.recycle();

    // 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()) {
        setBackgroundTintList(ColorStateList.valueOf(remoteBackgroundColor));
        setBackgroundResource(backgroundResourceId);
    } else if (Utils.isJellybeanOrLater()) {
        BitmapDrawable background = new BitmapDrawable(getResources(),
                                                       BitmapFactory.decodeResource(getResources(), backgroundResourceId));
        background.setColorFilter(new PorterDuffColorFilter(remoteBackgroundColor, PorterDuff.Mode.SRC_IN));
        setBackground(background);
    }
}
 
Example 3
Source File: ColorTransformation.java    From AndroidDemoProjects with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(Bitmap source) {
    if( color == 0 ) {
        return source;
    }

    BitmapDrawable drawable = new BitmapDrawable(Resources.getSystem(), source );
    Bitmap result = Bitmap.createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888 );
    Canvas canvas = new Canvas( result );
    drawable.setBounds( 0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight() );
    drawable.setColorFilter( color, PorterDuff.Mode.SRC_IN );
    drawable.draw(canvas);
    drawable.setColorFilter(null);
    drawable.setCallback(null);

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

    return result;
}
 
Example 4
Source File: BlurBehind.java    From RhymeMusic with Apache License 2.0 5 votes vote down vote up
public void setBackground(Activity activity) {
    if (mImageCache.size() != 0) {
        BitmapDrawable bd = new BitmapDrawable(activity.getResources(), mImageCache.get(KEY_CACHE_BLURRED_BACKGROUND_IMAGE));
        bd.setAlpha(mAlpha);
        if (mFilterColor != -1) {
            bd.setColorFilter(mFilterColor, PorterDuff.Mode.DST_ATOP);
        }
        activity.getWindow().setBackgroundDrawable(bd);
        mImageCache.remove(KEY_CACHE_BLURRED_BACKGROUND_IMAGE);
        cacheBlurBehindAndExecuteTask = null;
    }
}
 
Example 5
Source File: BackgroundLinearLayout.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public void setCustomBackground(Drawable d) {
    mCustomBackground = d;
    if (d != null && d instanceof BitmapDrawable) {
        // This is to add a tint of the background color to the image.
        // It prevents overly exposed or bright backgrounds from ruining the ambiance.
        BitmapDrawable bd = (BitmapDrawable) d;
        bd.getPaint().setColor(backgroundColor);
        ColorFilter filter = new LightingColorFilter(backgroundColor, 1);
        bd.setColorFilter(filter);
    }
    setBackground(mBackgroundDrawable);
    computeCustomBackgroundBounds();
    invalidate();
}
 
Example 6
Source File: BackgroundLinearLayout.java    From Noyze with Apache License 2.0 5 votes vote down vote up
public void setCustomBackground(Drawable d) {
    mCustomBackground = d;
    if (d != null && d instanceof BitmapDrawable) {
        // This is to add a tint of the background color to the image.
        // It prevents overly exposed or bright backgrounds from ruining the ambiance.
        BitmapDrawable bd = (BitmapDrawable) d;
        bd.getPaint().setColor(backgroundColor);
        ColorFilter filter = new LightingColorFilter(backgroundColor, 1);
        bd.setColorFilter(filter);
    }
    setBackground(mBackgroundDrawable);
    computeCustomBackgroundBounds();
    invalidate();
}
 
Example 7
Source File: BackgroundPatterns.java    From Faceless with GNU General Public License v3.0 5 votes vote down vote up
public BitmapDrawable getBackgroundDrawable(final Context context, final int patternID, final int color) {
	BitmapDrawable out = new BackgroundBitmapDrawable(context.getResources(), mBitmaps[validatePatternID(patternID)]);
	out.setAntiAlias(true);
	out.setDither(true);
	out.setTileModeXY(android.graphics.Shader.TileMode.REPEAT, android.graphics.Shader.TileMode.REPEAT);
	out.setColorFilter(new PorterDuffColorFilter(color, android.graphics.PorterDuff.Mode.MULTIPLY));
	return out;
}
 
Example 8
Source File: IconLabelItem.java    From openlauncher with Apache License 2.0 4 votes vote down vote up
@Override
public void bindView(@NonNull ViewHolder holder, @NonNull List<Object> payloads) {
    if (_width == Integer.MAX_VALUE) {
        holder.itemView.getLayoutParams().width = RecyclerView.LayoutParams.MATCH_PARENT;
    } else {
        holder.itemView.getLayoutParams().width = _width;
    }

    if (_height == Integer.MAX_VALUE) {
        holder.itemView.getLayoutParams().height = RecyclerView.LayoutParams.WRAP_CONTENT;
    } else {
        holder.itemView.getLayoutParams().height = _height;
    }

    // only run all this code if a label should be shown
    if (_label != null && _textVisibility) {
        holder.textView.setText(_label);
        holder.textView.setGravity(_textGravity);
        holder.textView.setMaxLines(1);
        holder.textView.setEllipsize(TextUtils.TruncateAt.END);
        // no default text color since it will be set by the theme
        if (_textColor != Integer.MAX_VALUE)
            holder.textView.setTextColor(_textColor);
    }

    // icon specific padding
    holder.textView.setCompoundDrawablePadding(_iconPadding);
    if (_iconSize != Integer.MAX_VALUE) {
        _icon = new BitmapDrawable(Setup.appContext().getResources(), Bitmap.createScaledBitmap(Tool.drawableToBitmap(_icon), _iconSize, _iconSize, true));
        _icon.setColorFilter(_iconColor, PorterDuff.Mode.SRC_ATOP);
        if (_isAppLauncher) {
            _icon.setBounds(0, 0, _iconSize, _iconSize);
        }
    }

    switch (_iconGravity) {
        case Gravity.START:
            if (_isAppLauncher) {
                holder.textView.setCompoundDrawables(_icon, null, null, null);
            } else {
                holder.textView.setCompoundDrawablesWithIntrinsicBounds(_icon, null, null, null);
            }
            break;
        case Gravity.END:
            if (_isAppLauncher) {
                holder.textView.setCompoundDrawables(null, null, _icon, null);
            } else {
                holder.textView.setCompoundDrawablesWithIntrinsicBounds(null, null, _icon, null);
            }
            break;
        case Gravity.TOP:
            if (_isAppLauncher) {
                holder.textView.setCompoundDrawables(null, _icon, null, null);
            } else {
                holder.textView.setCompoundDrawablesWithIntrinsicBounds(null, _icon, null, null);
            }
            break;
        case Gravity.BOTTOM:
            if (_isAppLauncher) {
                holder.textView.setCompoundDrawables(null, null, null, _icon);
            } else {
                holder.textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, _icon);
            }
            break;
    }

    // most items will not use a long click
    if (!_onClickAnimate)
        holder.itemView.setBackgroundResource(0);
    if (_onClickListener != null)
        holder.itemView.setOnClickListener(_onClickListener);
    if (_onLongClickListener != null)
        holder.itemView.setOnLongClickListener(_onLongClickListener);

    super.bindView(holder, payloads);
}