下面列出了怎么用androidx.annotation.ColorInt的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onColorSelection(@NonNull ColorChooserDialog dialog, @ColorInt int color) {
int colorFrom = XposedApp.getColor(this);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, color);
colorAnimation.addUpdateListener(animator -> {
int color1 = (int) animator.getAnimatedValue();
toolbar.setBackgroundColor(color1);
int darkenColor = XposedApp.darkenColor(color1, getDarkenFactor());
getWindow().setStatusBarColor(darkenColor);
if (navBar != null && navBar.isChecked()) {
getWindow().setNavigationBarColor(darkenColor);
}
});
colorAnimation.setDuration(750);
colorAnimation.start();
if (!dialog.isAccentMode()) {
XposedApp.getPreferences().edit().putInt("colors", color).apply();
}
}
@ColorInt
public static int shiftColor(@ColorInt int color, @FloatRange(from = 0.0f, to = 2.0f) float by) {
if (by == 1f) return color;
int alpha = Color.alpha(color);
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= by; // value component
return (alpha << 24) + (0x00ffffff & Color.HSVToColor(hsv));
}
private static Paint createPaint(float width, @ColorInt int color) {
Paint paint = new Paint();
paint.setStrokeWidth(width);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
paint.setColor(color);
return paint;
}
public ShapeSelector setDefaultStrokeColor(@ColorInt int color) {
mDefaultStrokeColor = color;
if (!hasSetDisabledStrokeColor)
mDisabledStrokeColor = color;
if (!hasSetPressedStrokeColor)
mPressedStrokeColor = color;
if (!hasSetSelectedStrokeColor)
mSelectedStrokeColor = color;
if (!hasSetFocusedStrokeColor)
mFocusedStrokeColor = color;
return this;
}
public static void setTint(@NonNull Switch switchView, @ColorInt int color, boolean useDarker) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return;
if (switchView.getTrackDrawable() != null) {
switchView.setTrackDrawable(modifySwitchDrawable(switchView.getContext(),
switchView.getTrackDrawable(), color, false, false, useDarker));
}
if (switchView.getThumbDrawable() != null) {
switchView.setThumbDrawable(modifySwitchDrawable(switchView.getContext(),
switchView.getThumbDrawable(), color, true, false, useDarker));
}
}
/**
* 设置顶部标题栏标题文字颜色
*/
public void setTitleTextColor(@ColorInt int titleTextColor) {
if (null != titleView && titleView instanceof TextView) {
((TextView) titleView).setTextColor(titleTextColor);
} else {
this.titleTextColor = titleTextColor;
}
}
/**
* Set ripple color used for ripple effect on button press
*
* @param color color to set for the ripple effect for this button
*/
public void setRipple(@ColorInt int color)
{
rippleColor = color;
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP)
{
rippleDrawableLollipop = new RippleDrawable(ColorStateList.valueOf(rippleColor), null, null);
// setCallback on Drawable allows animations to be scheduled and the drawable to invalidate the view on
// animation
rippleDrawableLollipop.setCallback(this);
rippleDrawableLollipop.setBounds(0, 0, getWidth(), getHeight());
// Disable/nullify the pre-lollipop RippleDrawable backport
rippleDrawable = null;
}
else
{
rippleDrawable = new codetail.graphics.drawables.RippleDrawable(ColorStateList.valueOf(rippleColor), null,
null);
// setCallback on Drawable allows animations to be scheduled and the drawable to invalidate the view on
// animation
rippleDrawable.setCallback(this);
rippleDrawable.setBounds(0, 0, getWidth(), getHeight());
setOnTouchListener(new DrawableHotspotTouch(rippleDrawable));
// Disable/nullify the lollipop RippleDrawable
rippleDrawableLollipop = null;
}
invalidate();
}
/**
* Set the theme for this Navigation Drawer.
*
* @param primaryColor Color for header background
* @param backgroundColor Color for drawer background
* @param textColor Color for item text
* @param iconColor Color for icons
*/
public void setTheme(@ColorInt int primaryColor, @ColorInt int backgroundColor,
@ColorInt int textColor, @ColorInt int iconColor) {
setBackgroundColor(backgroundColor);
drawerHeader.setBackgroundColor(primaryColor);
for (NavigationEntry navigationEntry : navigationEntries) {
navigationEntry.setTextColor(textColor);
navigationEntry.setIconColor(iconColor);
}
}
/**
* Set the background to be a solid color when a button is not selected for each button
*
* This will create a ColorDrawable or modify the current background if it is a ColorDrawable.
*
* Note: This is a convenience function that sets the background for each individual button.
*
* @param color color to set the background to
*/
public void setBackground(@ColorInt int color)
{
if (backgroundDrawable instanceof ColorDrawable)
{
((ColorDrawable)backgroundDrawable.mutate()).setColor(color);
// Required to update background for the buttons
setBackground(backgroundDrawable);
}
else
{
setBackground(new ColorDrawable(color));
}
}
/**
* Retrieves the {@link Drawable} for the corresponding drawable resource id
* and applies the specified color to it.
*
* @param context the context
* @param drawableId the drawable resource id
* @param desiredColor the desired color to be applied to the retrieved drawable
* @return the retrieved and colored drawable or null.
*/
@Nullable
public static Drawable getColoredDrawable(@NonNull Context context,
@DrawableRes int drawableId,
@ColorInt int desiredColor) {
Preconditions.nonNull(context);
final Drawable drawable = ContextCompat.getDrawable(context, drawableId);
return ((drawable != null) ? getColoredDrawable(drawable, desiredColor) : null);
}
public static void setItemIconColors(@NonNull NavigationView navigationView, @ColorInt int normalColor, @ColorInt int selectedColor) {
final ColorStateList iconSl = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_checked},
new int[]{android.R.attr.state_checked}
},
new int[]{
normalColor,
selectedColor
});
navigationView.setItemIconTintList(iconSl);
}
/**
* Set the border for the perimeter of the button group.
*
* @param width Width of the border in pixels (default value is 0px or no border)
* @param color Color of the border (default color is black)
* @param dashWidth Width of the dash for border, in pixels. Value of 0px means solid line (default is 0px)
* @param dashGap Width of the gap for border, in pixels.
*/
public void setBorder(int width, @ColorInt int color, int dashWidth, int dashGap)
{
borderWidth = width;
borderColor = color;
borderDashWidth = dashWidth;
borderDashGap = dashGap;
// Border width of 0 indicates to hide borders
if (width > 0)
{
GradientDrawable borderDrawable = new GradientDrawable();
// Set background color to be transparent so that buttons and everything underneath the border view is
// still visible. This was an issue on API 16 Android where it would default to a black background
borderDrawable.setColor(Color.TRANSPARENT);
// Corner radius is the radius minus half of the border width because the drawable will draw the stroke
// from the center, so the actual corner radius is reduced
// If the half border width is left out, the border radius does not follow the curve of the background
borderDrawable.setCornerRadius(radius - width / 2.0f);
borderDrawable.setStroke(width, color, dashWidth, dashGap);
borderView.setBackground(borderDrawable);
}
else
{
borderView.setBackground(null);
}
}
/**
* 设置底部文字的颜色--------------
*/
public IOSDialog bottomTextColor(@ColorInt int leftColor, @ColorInt int rightColor) {
mLeftTextColor = leftColor;
mRightTextColor = rightColor;
if (mLeftView != null) {
mLeftView.setTextColor(mLeftTextColor);
}
if (mRightView != null) {
mRightView.setTextColor(mRightTextColor);
}
return this;
}
@CheckResult
@ColorInt
public static int navigationBarColor(@NonNull Context context) {
if (!coloredNavigationBar(context)) {
return Color.BLACK;
}
return prefs(context).getInt(KEY_NAVIGATION_BAR_COLOR, primaryColor(context));
}
@SuppressLint("PrivateResource")
@ColorInt
public static int getSecondaryDisabledTextColor(final Context context, boolean dark) {
if (dark) {
return ContextCompat.getColor(context, R.color.secondary_text_disabled_material_light);
}
return ContextCompat.getColor(context, R.color.secondary_text_disabled_material_dark);
}
public ShapeSelector setDefaultBgColor(@ColorInt int color) {
mDefaultBgColor = color;
if (!hasSetDisabledBgColor)
mDisabledBgColor = color;
if (!hasSetPressedBgColor)
mPressedBgColor = color;
if (!hasSetSelectedBgColor)
mSelectedBgColor = color;
if (!hasSetFocusedBgColor)
mFocusedBgColor = color;
return this;
}
@ColorInt
public static int adjustAlpha(@ColorInt int color, @FloatRange(from = 0.0, to = 1.0) float factor) {
int alpha = Math.round(Color.alpha(color) * factor);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
private static Drawable modifySwitchDrawable(@NonNull Context context, @NonNull Drawable from, @ColorInt int tint, boolean thumb, boolean compatSwitch, boolean useDarker) {
if (useDarker) {
tint = ColorUtil.shiftColor(tint, 1.1f);
}
tint = ColorUtil.adjustAlpha(tint, (compatSwitch && !thumb) ? 0.5f : 1.0f);
int disabled;
int normal;
if (thumb) {
disabled = ContextCompat.getColor(context, useDarker ? R.color.ate_switch_thumb_disabled_dark : R.color.ate_switch_thumb_disabled_light);
normal = ContextCompat.getColor(context, useDarker ? R.color.ate_switch_thumb_normal_dark : R.color.ate_switch_thumb_normal_light);
} else {
disabled = ContextCompat.getColor(context, useDarker ? R.color.ate_switch_track_disabled_dark : R.color.ate_switch_track_disabled_light);
normal = ContextCompat.getColor(context, useDarker ? R.color.ate_switch_track_normal_dark : R.color.ate_switch_track_normal_light);
}
// Stock switch includes its own alpha
if (!compatSwitch) {
normal = ColorUtil.stripAlpha(normal);
}
final ColorStateList sl = new ColorStateList(
new int[][]{
new int[]{-android.R.attr.state_enabled},
new int[]{android.R.attr.state_enabled, -android.R.attr.state_activated, -android.R.attr.state_checked},
new int[]{android.R.attr.state_enabled, android.R.attr.state_activated},
new int[]{android.R.attr.state_enabled, android.R.attr.state_checked}
},
new int[]{
disabled,
normal,
tint,
tint
}
);
return createTintedDrawable(from, sl);
}
/**
* Set the color of the scroll track.
*
* @param color The color for the scroll track
*/
public void setTrackColor(@ColorInt int color) {
@ColorInt int trackColor = color;
if (mTrackImage == null) {
Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.fastscroll_track);
if (drawable != null) {
mTrackImage = DrawableCompat.wrap(drawable);
}
}
DrawableCompat.setTint(mTrackImage, trackColor);
mTrackView.setImageDrawable(mTrackImage);
}
/**
* Determines whether to use dark icons on a background with given color by comparing the
* contrast ratio (https://www.w3.org/TR/WCAG20/#contrast-ratiodef) to a threshold.
* This criterion matches the one used by Chrome:
* https://chromium.googlesource.com/chromium/src/+/90ac05ba6cb9ab5d5df75f0cef62c950be3716c3/chrome/android/java/src/org/chromium/chrome/browser/util/ColorUtils.java#215
*/
private static boolean shouldUseDarkIconsOnBackground(@ColorInt int backgroundColor) {
float luminance = 0.2126f * luminanceOfColorComponent(Color.red(backgroundColor))
+ 0.7152f * luminanceOfColorComponent(Color.green(backgroundColor))
+ 0.0722f * luminanceOfColorComponent(Color.blue(backgroundColor));
float contrast = Math.abs((1.05f) / (luminance + 0.05f));
return contrast < 3;
}
/**
* Set the color for the scroll handle.
*
* @param color The color for the scroll handle
*/
public void setHandleColor(@ColorInt int color) {
mHandleColor = color;
if (mHandleImage == null) {
Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.fastscroll_handle);
if (drawable != null) {
mHandleImage = DrawableCompat.wrap(drawable);
}
}
DrawableCompat.setTint(mHandleImage, mHandleColor);
mHandleView.setImageDrawable(mHandleImage);
}
@CheckResult
@ColorInt
public static int textColorPrimary(@NonNull Context context) {
return prefs(context).getInt(KEY_TEXT_COLOR_PRIMARY, ATHUtil.resolveColor(context, android.R.attr.textColorPrimary));
}
public void setMaskColor(@ColorInt int maskColor) {
this.maskColor = maskColor;
invalidate();
}
public static void selectGif(Activity activity, int requestCode, boolean isForMms, @ColorInt int color) {
Intent intent = new Intent(activity, GiphyActivity.class);
intent.putExtra(GiphyActivity.EXTRA_IS_MMS, isForMms);
intent.putExtra(GiphyActivity.EXTRA_COLOR, color);
activity.startActivityForResult(intent, requestCode);
}
public ShapeSelector setFocusedBgColor(@ColorInt int color) {
mFocusedBgColor = color;
hasSetPressedBgColor = true;
return this;
}
@ColorInt
int getColor();
public void setPressedBackgroundColor(@ColorInt int pressedBackgroundColor) {
this.mPressedBackgroundColor = pressedBackgroundColor;
mPressedBackground.setColor(mPressedBackgroundColor);
}
@CheckResult
@ColorInt
public static int accentColor(@NonNull Context context) {
return prefs(context).getInt(KEY_ACCENT_COLOR, ATHUtil.resolveColor(context, R.attr.colorAccent, Color.parseColor("#263238")));
}
public void setUnableTextColor(@ColorInt int unableTextColor) {
this.mUnableTextColor = unableTextColor;
setTextColor();
}
@CheckResult
@ColorInt
public static int backgroundColor(@NonNull Context context) {
return prefs(context).getInt(KEY_BACKGROUND_COLOR, ATHUtil.resolveColor(context, android.R.attr.colorBackground));
}