Java Code Examples for android.graphics.drawable.ColorDrawable#setColor()

The following examples show how to use android.graphics.drawable.ColorDrawable#setColor() . 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: MainActivity.java    From road-trip with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("PointlessBitwiseExpression")
private void changeBackgroundColor(View decorView, float alpha) {
    float srcR = ((mAccentColor >> 16) & 0xff) / 255.0f;
    float srcG = ((mAccentColor >>  8) & 0xff) / 255.0f;
    float srcB = ((mAccentColor >>  0) & 0xff) / 255.0f;

    float dstR = ((mAccentColor2 >> 16) & 0xff) / 255.0f;
    float dstG = ((mAccentColor2 >>  8) & 0xff) / 255.0f;
    float dstB = ((mAccentColor2 >>  0) & 0xff) / 255.0f;

    int r = (int) ((srcR + ((dstR - srcR) * alpha)) * 255.0f);
    int g = (int) ((srcG + ((dstG - srcG) * alpha)) * 255.0f);
    int b = (int) ((srcB + ((dstB - srcB) * alpha)) * 255.0f);

    ColorDrawable background = (ColorDrawable) decorView.getBackground();
    if (background != null) {
        background.setColor(0xff000000 | r << 16 | g << 8 | b);
    }
}
 
Example 2
Source File: BottomBar.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
private void setPaintColor(int alpha, int color)
{
    if (mAnimatedCircleDrawable != null)
    {
        mAnimatedCircleDrawable.setColor(color);
        mAnimatedCircleDrawable.setAlpha(alpha);
    } else if (mColorDrawable != null)
    {
        mColorDrawable.setColor(color);
        mColorDrawable.setAlpha(alpha);
    }

    if (mIntentReviewLayout != null)
    {
        ColorDrawable intentBackground = (ColorDrawable) mIntentReviewLayout
                .getBackground();
        intentBackground.setColor(color);
        intentBackground.setAlpha(alpha);
    }
}
 
Example 3
Source File: BottomBar.java    From Camera2 with Apache License 2.0 6 votes vote down vote up
private void setCancelBackgroundColor(int alpha, int color)
{
    LayerDrawable layerDrawable = (LayerDrawable) mCancelButton.getBackground();
    Drawable d = layerDrawable.getDrawable(0);
    if (d instanceof AnimatedCircleDrawable)
    {
        AnimatedCircleDrawable animatedCircleDrawable = (AnimatedCircleDrawable) d;
        animatedCircleDrawable.setColor(color);
        animatedCircleDrawable.setAlpha(alpha);
    } else if (d instanceof ColorDrawable)
    {
        ColorDrawable colorDrawable = (ColorDrawable) d;
        if (!ApiHelper.isLOrHigher())
        {
            colorDrawable.setColor(color);
        }
        colorDrawable.setAlpha(alpha);
    }
}
 
Example 4
Source File: MaterialSpinner.java    From MaterialSpinner with Apache License 2.0 6 votes vote down vote up
@Override public void setBackgroundColor(int color) {
  backgroundColor = color;
  Drawable background = getBackground();
  if (background instanceof StateListDrawable) { // pre-L
    try {
      Method getStateDrawable = StateListDrawable.class.getDeclaredMethod("getStateDrawable", int.class);
      if (!getStateDrawable.isAccessible()) getStateDrawable.setAccessible(true);
      int[] colors = { Utils.darker(color, 0.85f), color };
      for (int i = 0; i < colors.length; i++) {
        ColorDrawable drawable = (ColorDrawable) getStateDrawable.invoke(background, i);
        drawable.setColor(colors[i]);
      }
    } catch (Exception e) {
      Log.e("MaterialSpinner", "Error setting background color", e);
    }
  } else if (background != null) { // 21+ (RippleDrawable)
    background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
  }
  popupWindow.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
}
 
Example 5
Source File: AndroidUtils.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void setBackgroundShapeColor(View view, int color) {

        Drawable background = view.getBackground();
        if (background instanceof ShapeDrawable) {
            // cast to 'ShapeDrawable'
            ShapeDrawable shapeDrawable = (ShapeDrawable) background;
            shapeDrawable.getPaint().setColor(color);
        } else if (background instanceof GradientDrawable) {
            // cast to 'GradientDrawable'
            GradientDrawable gradientDrawable = (GradientDrawable) background;
            gradientDrawable.setColor(color);
        } else if (background instanceof ColorDrawable) {
            // alpha value may need to be set again after this call
            ColorDrawable colorDrawable = (ColorDrawable) background;
            colorDrawable.setColor(color);
        }
    }
 
Example 6
Source File: Drawable_Color_Util.java    From AGIKSwipeButton with MIT License 5 votes vote down vote up
public static void setDrawableColor(Drawable drawable, int color) {
    drawable.mutate();
    if (drawable instanceof ShapeDrawable) {
        ShapeDrawable shapeDrawable = (ShapeDrawable) drawable;
        shapeDrawable.getPaint().setColor(color);
    } else if (drawable instanceof GradientDrawable) {
        GradientDrawable gradientDrawable = (GradientDrawable) drawable;
        gradientDrawable.setColor(color);
    } else if (drawable instanceof ColorDrawable) {
        ColorDrawable colorDrawable = (ColorDrawable) drawable;
        colorDrawable.setColor(color);
    }

}
 
Example 7
Source File: PXColorUtil.java    From pixate-freestyle-android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the Saturation value on a view that has a colored background. In
 * case the view's background is not a {@link ColorDrawable}, or does not
 * contain one in a {@link LayerDrawable}, nothing will be applied.
 * 
 * @param view
 * @param saturation
 */
public static void setSaturation(View view, float saturation) {
    ColorDrawable colorDrawable = getColorDrawableBackground(view);
    if (colorDrawable != null) {
        int color = colorDrawable.getColor();
        float[] hsl = new float[3];
        PXColorUtil.colorToHsl(color, hsl);
        colorDrawable.setColor(PXColorUtil.hslToColor(Color.alpha(color), hsl[0], saturation,
                hsl[2]));
    }
}
 
Example 8
Source File: PXColorUtil.java    From pixate-freestyle-android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the Hue value on a view that has a colored background. In case the
 * view's background is not a {@link ColorDrawable}, or does not contain one
 * in a {@link LayerDrawable}, nothing will be applied.
 * 
 * @param view
 * @param hue
 */
public static void setHue(View view, float hue) {
    ColorDrawable colorDrawable = getColorDrawableBackground(view);
    if (colorDrawable != null) {
        int color = colorDrawable.getColor();
        float[] hsl = new float[3];
        PXColorUtil.colorToHsl(color, hsl);
        colorDrawable.setColor(PXColorUtil.hslToColor(Color.alpha(color), hue, hsl[1], hsl[2]));
    }
}
 
Example 9
Source File: PToolbar.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@PhonkMethod(description = "Changes the title bar color", example = "")
@PhonkMethodParam(params = {"r", "g", "b", "alpha"})
public PToolbar background(int r, int g, int b) {
    int c = Color.rgb(r, g, b);

    ColorDrawable d = new ColorDrawable();
    d.setColor(c);
    mToolbar.setBackgroundDrawable(d);

    return this;
}
 
Example 10
Source File: PToolbar.java    From PHONK with GNU General Public License v3.0 5 votes vote down vote up
@PhonkMethod(description = "Changes the title bar color", example = "")
@PhonkMethodParam(params = {"r", "g", "b", "alpha"})
public PToolbar background(int r, int g, int b, int alpha) {
    int c = Color.argb(alpha, r, g, b);

    ColorDrawable d = new ColorDrawable();
    d.setColor(c);
    mToolbar.setBackgroundDrawable(d);

    return this;
}
 
Example 11
Source File: Util.java    From slideview with MIT License 5 votes vote down vote up
static void setDrawableColor(Drawable drawable, int color) {
    drawable.mutate();
    if (drawable instanceof ShapeDrawable) {
        ShapeDrawable shapeDrawable = (ShapeDrawable) drawable;
        shapeDrawable.getPaint().setColor(color);
    } else if (drawable instanceof GradientDrawable) {
        GradientDrawable gradientDrawable = (GradientDrawable) drawable;
        gradientDrawable.setColor(color);
    } else if (drawable instanceof ColorDrawable) {
        ColorDrawable colorDrawable = (ColorDrawable) drawable;
        colorDrawable.setColor(color);
    }

}
 
Example 12
Source File: OneDrawable.java    From OneDrawable with Apache License 2.0 5 votes vote down vote up
private static Drawable createBgColor(Context context, @ColorInt int resBackgroundColor, @PressedMode.Mode int mode, @FloatRange(from = 0.0f, to = 1.0f) float alpha) {
    ColorDrawable colorDrawableNormal = new ColorDrawable();
    ColorDrawable colorDrawablePressed = new ColorDrawable();
    ColorDrawable colorDrawableUnable = new ColorDrawable();

    colorDrawableNormal.setColor(resBackgroundColor);
    colorDrawablePressed.setColor(resBackgroundColor);
    colorDrawableUnable.setColor(resBackgroundColor);
    Drawable pressed = getPressedStateDrawable(context, mode, alpha, colorDrawablePressed);
    Drawable unable = getUnableStateDrawable(context, colorDrawableUnable);

    return createStateListDrawable(colorDrawableNormal, pressed, unable);
}
 
Example 13
Source File: Recolor.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
        TransitionValues endValues) {
    if (startValues == null || endValues == null) {
        return null;
    }
    final View view = endValues.view;
    Drawable startBackground = (Drawable) startValues.values.get(PROPNAME_BACKGROUND);
    Drawable endBackground = (Drawable) endValues.values.get(PROPNAME_BACKGROUND);
    boolean changed = false;
    if (startBackground instanceof ColorDrawable && endBackground instanceof ColorDrawable) {
        ColorDrawable startColor = (ColorDrawable) startBackground;
        ColorDrawable endColor = (ColorDrawable) endBackground;
        if (startColor.getColor() != endColor.getColor()) {
            endColor.setColor(startColor.getColor());
            changed = true;
            return ObjectAnimator.ofArgb(endBackground, "color", startColor.getColor(),
                    endColor.getColor());
        }
    }
    if (view instanceof TextView) {
        TextView textView = (TextView) view;
        int start = (Integer) startValues.values.get(PROPNAME_TEXT_COLOR);
        int end = (Integer) endValues.values.get(PROPNAME_TEXT_COLOR);
        if (start != end) {
            textView.setTextColor(end);
            changed = true;
            return ObjectAnimator.ofArgb(textView, "textColor", start, end);
        }
    }
    return null;
}
 
Example 14
Source File: ToolbarActivity.java    From AndroidBleManager with Apache License 2.0 4 votes vote down vote up
public void setToolbarBackgroundColor(int colorRes) {
    if (mToolbar != null) {
        ColorDrawable background = (ColorDrawable) mToolbar.getBackground();
        background.setColor(getResources().getColor(colorRes));
    }
}
 
Example 15
Source File: UARTActivity.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Updates the ActionBar background color depending on whether we are in edit mode or not.
 * 
 * @param editMode
 *            <code>true</code> to show edit mode, <code>false</code> otherwise
 * @param change
 *            if <code>true</code> the background will change with animation, otherwise immediately
 */
@SuppressLint("NewApi")
private void setEditMode(final boolean editMode, final boolean change) {
	this.editMode = editMode;
	configurationListener.setEditMode(editMode);
	if (!change) {
		final ColorDrawable color = new ColorDrawable();
		int darkColor = 0;
		if (editMode) {
			color.setColor(ContextCompat.getColor(this, R.color.orange));
			darkColor = ContextCompat.getColor(this, R.color.dark_orange);
		} else {
			color.setColor(ContextCompat.getColor(this, R.color.actionBarColor));
			darkColor = ContextCompat.getColor(this, R.color.actionBarColorDark);
		}
		getSupportActionBar().setBackgroundDrawable(color);

		// Since Lollipop the status bar color may also be changed
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
			getWindow().setStatusBarColor(darkColor);
	} else {
		final TransitionDrawable transition = (TransitionDrawable) getResources().getDrawable(
				editMode ? R.drawable.start_edit_mode : R.drawable.stop_edit_mode);
		transition.setCrossFadeEnabled(true);
		getSupportActionBar().setBackgroundDrawable(transition);
		transition.startTransition(200);

		// Since Lollipop the status bar color may also be changed
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			final int colorFrom = ContextCompat.getColor(this, editMode ? R.color.actionBarColorDark : R.color.dark_orange);
			final int colorTo = ContextCompat.getColor(this, !editMode ? R.color.actionBarColorDark : R.color.dark_orange);

			final ValueAnimator anim = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
			anim.setDuration(200);
			anim.addUpdateListener(animation -> getWindow().setStatusBarColor((Integer) animation.getAnimatedValue()));
			anim.start();
		}

		if (slider != null && editMode) {
			slider.closePane();
		}
	}
}
 
Example 16
Source File: SimpleMountSpecTesterSpec.java    From litho with Apache License 2.0 4 votes vote down vote up
@OnMount
static void onMount(
    ComponentContext c, ColorDrawable drawable, @Prop(optional = true) int color) {
  drawable.setColor(color);
}
 
Example 17
Source File: LeftDrawerManager.java    From timecat with Apache License 2.0 4 votes vote down vote up
public void updateHeaderBackground(DBUser u) {
    currentUser = u;
    LayerDrawable layers = (LayerDrawable) headerResult.getHeaderBackgroundView().getDrawable();
    ColorDrawable color = (ColorDrawable) layers.findDrawableByLayerId(R.id.color_layer);
    color.setColor(ScreenUtil.equivalentNoAlpha(u.color(), 1f));
}
 
Example 18
Source File: Recolor.java    From Transitions-Everywhere with Apache License 2.0 4 votes vote down vote up
@Override
public void setValue(@NonNull ColorDrawable object, int value) {
    object.setColor(value);
}
 
Example 19
Source File: Recolor.java    From Transitions-Everywhere with Apache License 2.0 4 votes vote down vote up
@Override
public void setValue(@NonNull ColorDrawable object, int value) {
    object.setColor(value);
}
 
Example 20
Source File: PXColorUtil.java    From pixate-freestyle-android with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the color for a view with a colored background. In case the view's
 * background is not a {@link ColorDrawable}, or does not contain a
 * color-drawable in one of its layers, nothing happens.
 * 
 * @param view
 * @param color
 */
public static void setColor(View view, int color) {
    ColorDrawable colorDrawable = getColorDrawableBackground(view);
    if (colorDrawable != null) {
        colorDrawable.setColor(color);
    }
}