private void styleAccuracy(float accuracyAlpha, @ColorInt int accuracyColor) { layerMap.get(ACCURACY_LAYER).setProperties( circleColor(accuracyColor), circleOpacity(accuracyAlpha), circlePitchAlignment(Property.CIRCLE_PITCH_ALIGNMENT_MAP), circleStrokeWidth(0.5f), circleStrokeColor(accuracyColor) ); }
/** * 设置状态栏颜色 * * @param activity 需要设置的activity * @param color 状态栏颜色值 * @param statusBarAlpha 状态栏透明度 */ public static void setColor(Activity activity, @ColorInt int color, @IntRange(from = 0, to = 255) int statusBarAlpha) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha)); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); View fakeStatusBarView = decorView.findViewById(FAKE_STATUS_BAR_VIEW_ID); if (fakeStatusBarView != null) { if (fakeStatusBarView.getVisibility() == View.GONE) { fakeStatusBarView.setVisibility(View.VISIBLE); } fakeStatusBarView.setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); } else { decorView.addView(createStatusBarView(activity, color, statusBarAlpha)); } setRootView(activity); } }
/** * 动态创建带上分隔线或下分隔线的Drawable * * @param separatorColor * @param bgColor * @param top * @return */ public static LayerDrawable createItemSeparatorBg(@ColorInt int separatorColor, @ColorInt int bgColor, int separatorHeight, boolean top) { ShapeDrawable separator = new ShapeDrawable(); separator.getPaint().setStyle(Paint.Style.FILL); separator.getPaint().setColor(separatorColor); ShapeDrawable bg = new ShapeDrawable(); bg.getPaint().setStyle(Paint.Style.FILL); bg.getPaint().setColor(bgColor); Drawable[] layers = {separator, bg}; LayerDrawable layerDrawable = new LayerDrawable(layers); layerDrawable.setLayerInset(1, 0, top ? separatorHeight : 0, 0, top ? 0 : separatorHeight); return layerDrawable; }
/** * 设置snackbar文字和背景颜色 * * @param parent 父视图(CoordinatorLayout或者DecorView) * @param text 文本 * @param duration 显示时长 * @param textColor 文本颜色 * @param bgColor 背景色 * @param actionText 事件文本 * @param actionTextColor 事件文本颜色 * @param listener 监听器 */ private static void showSnackbar(View parent, CharSequence text, int duration, @ColorInt int textColor, @ColorInt int bgColor, CharSequence actionText, int actionTextColor, View.OnClickListener listener) { switch (duration) { default: case Snackbar.LENGTH_SHORT: case Snackbar.LENGTH_LONG: snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, text, duration)); break; case Snackbar.LENGTH_INDEFINITE: snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, text, Snackbar.LENGTH_INDEFINITE).setDuration(duration)); } View view = snackbarWeakReference.get().getView(); ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(textColor); view.setBackgroundColor(bgColor); if (actionText != null && actionText.length() > 0 && listener != null) { snackbarWeakReference.get().setActionTextColor(actionTextColor); snackbarWeakReference.get().setAction(actionText, listener); } snackbarWeakReference.get().show(); }
/** * 设置状态栏颜色 * * @param activity 需要设置的activity * @param color 状态栏颜色值 * @param statusBarAlpha 状态栏透明度 */ public static void setColor(Activity activity, @ColorInt int color, int statusBarAlpha) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); activity.getWindow().setStatusBarColor(calculateStatusColor(color, statusBarAlpha)); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); int count = decorView.getChildCount(); if (count > 0 && decorView.getChildAt(count - 1) instanceof StatusBarView) { decorView.getChildAt(count - 1).setBackgroundColor(calculateStatusColor(color, statusBarAlpha)); } else { StatusBarView statusView = createStatusBarView(activity, color, statusBarAlpha); decorView.addView(statusView); } setRootView(activity); } }
private int[] getColorShades(@ColorInt int color) { return new int[]{ shadeColor(color, 0.9), shadeColor(color, 0.7), shadeColor(color, 0.5), shadeColor(color, 0.333), shadeColor(color, 0.166), shadeColor(color, -0.125), shadeColor(color, -0.25), shadeColor(color, -0.375), shadeColor(color, -0.5), shadeColor(color, -0.675), shadeColor(color, -0.7), shadeColor(color, -0.775), }; }
/** * Get the dark color. * * @param color Original color. * @return Dark color. */ @ColorInt private static int getDarkColor(final int color) { final float factor = 0.6f; final int a = Color.alpha(color); final int r = Math.round(Color.red(color) * factor); final int g = Math.round(Color.green(color) * factor); final int b = Math.round(Color.blue(color) * factor); return Color.argb(a, Math.min(r, 255), Math.min(g, 255), Math.min(b, 255)); }
/** * Set a color to be drawn behind the circle-shaped drawable. Note that * this has no effect if the drawable is opaque or no drawable is set. * * @param fillColor The color to be drawn behind the drawable * * @deprecated Fill color support is going to be removed in the future */ @Deprecated public void setFillColor(@ColorInt int fillColor) { if (fillColor == mFillColor) { return; } mFillColor = fillColor; mFillPaint.setColor(fillColor); invalidate(); }
private void applySpan(Spannable spannable, @ColorInt int color, int start, int end) { spannable.setSpan( new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
/** * Gets color. * * @param context the context * @param colorRes the color res * @return the color */ @ColorInt public static int getColor(Context context, @ColorRes int colorRes) { if (Build.VERSION.SDK_INT < 21) { //noinspection deprecation return context.getResources().getColor(colorRes); } else { return context.getResources().getColor(colorRes, null); } }
/** * Returns the luminance of a color as a float between {@code 0.0} and {@code 1.0}. * <p>Defined as the Y component in the XYZ representation of {@code color}.</p> */ @FloatRange(from = 0.0, to = 1.0) public static double calculateLuminance(@ColorInt int color) { final double[] result = getTempDouble3Array(); colorToXYZ(color, result); // Luminance is the Y component return result[1] / 100; }
@ColorInt public static int getColorFromAttr(Context context, int attr, @ColorInt int defaultColor) { TypedArray a = context.obtainStyledAttributes(new TypedValue().data, new int[]{attr}); int intColor = a.getColor(0, defaultColor); a.recycle(); return intColor; }
public void setFillColor(@ColorInt int fillColor) { if (fillColor == mFillColor) { return; } mFillColor = fillColor; mFillPaint.setColor(fillColor); invalidate(); }
@ColorInt public static int setColorAlpha(@ColorInt int color, @FloatRange(from = 0.0D, to = 1.0D) float alpha) { int alpha2 = Math.round((float) Color.alpha(color) * alpha); int red = Color.red(color); int green = Color.green(color); int blue = Color.blue(color); return Color.argb(alpha2, red, green, blue); }
/** * Sets the color of the drawable. */ public void setColor(@ColorInt int color) { if (color != mPaint.getColor()) { mPaint.setColor(color); invalidateSelf(); } }
public void setDelegateAreaColor(@ColorInt int color, Rect rect) { paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setColor(color); this.rect = rect; invalidate(); }
/** * 设置状态栏颜色 * * @param fakeStatusBar 伪造状态栏 * @param color 状态栏颜色值 * @param alpha 状态栏透明度,此透明度并非颜色中的透明度 */ public static void setStatusBarColor(@NonNull final View fakeStatusBar, @ColorInt final int color, @IntRange(from = 0, to = 255) final int alpha) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return; fakeStatusBar.setVisibility(View.VISIBLE); transparentStatusBar((Activity) fakeStatusBar.getContext()); ViewGroup.LayoutParams layoutParams = fakeStatusBar.getLayoutParams(); layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT; layoutParams.height = BarUtils.getStatusBarHeight(); fakeStatusBar.setBackgroundColor(getStatusBarColor(color, alpha)); }
@ColorInt public static int shiftColor(@ColorInt int color, @FloatRange(from = 0.0f, to = 2.0f) float by) { if (by == 1f) return color; float[] hsv = new float[3]; Color.colorToHSV(color, hsv); hsv[2] *= by; // value component return Color.HSVToColor(hsv); }
/** * 设置背景色 * * @param fragment fragment * @param color 背景色 */ public static void setBackgroundColor(@NonNull Fragment fragment, @ColorInt int color) { View view = fragment.getView(); if (view != null) { view.setBackgroundColor(color); } }
@ColorInt public static int getHighlightColor(String className) { Matcher matcher = HIGHLIGHT_CLASS_PATTERN.matcher(className); int classNumber = 0; if (matcher.matches()) { classNumber = Integer.parseInt(matcher.group(1)); } return highlightColorMap.get(classNumber); }
/** * 生成一个和状态栏大小相同的彩色矩形条 * * @param activity 需要设置的 activity * @param color 状态栏颜色值 * @return 状态栏矩形条 */ private static StatusBarView createStatusBarView(Activity activity, @ColorInt int color) { // 绘制一个和状态栏一样高的矩形 StatusBarView statusBarView = new StatusBarView(activity); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); statusBarView.setLayoutParams(params); statusBarView.setBackgroundColor(color); return statusBarView; }
@Override public View createView(ViewGroup root, @ColorInt int textColor, float textSize, float textAlpha) { View view = LayoutInflater.from(root.getContext()).inflate(layoutResId, root, false); cpuUsageTextView = view.findViewById(R.id.debugoverlay_overlay_text); cpuUsageTextView.setTextColor(textColor); cpuUsageTextView.setTextSize(textSize); cpuUsageTextView.setAlpha(textAlpha); return view; }
private void setText(@Nullable CharSequence text, @ColorInt int color) { if (TextUtils.isEmpty(text)) { textView.setVisibility(GONE); } else { textView.setVisibility(VISIBLE); textView.setText(fromHtml(text.toString())); textView.setTextColor(color); } }
public void setBorderColor(@ColorInt int borderColor) { if (borderColor == mBorderColor) { return; } mBorderColor = borderColor; mBorderPaint.setColor(mBorderColor); invalidate(); }
/** * 设置分隔阴影颜色及透明度 */ public void setShadowColor(@ColorInt int color, @IntRange(from = 1, to = 255) int alpha) { if (null == dividerConfig) { dividerConfig = new WheelView.DividerConfig(); } dividerConfig.setShadowColor(color); dividerConfig.setShadowAlpha(alpha); }
@CheckResult @ColorInt public static int navigationViewSelectedBg(@NonNull Context context, @Nullable String key, boolean darkTheme) { final int defaultColor = ContextCompat.getColor(context, darkTheme ? R.color.ate_navigation_drawer_selected_dark : R.color.ate_navigation_drawer_selected_light); return prefs(context, key).getInt(KEY_NAVIGATIONVIEW_SELECTED_BG, defaultColor); }
public CFAlertActionButton(Context context, String buttonText, @ColorInt int textColor, @ColorInt int backgroundColor, CFAlertActionStyle style, CFAlertActionAlignment alignment, OnClickListener onClickListener) { this.context = context; this.buttonText = buttonText; this.textColor = textColor; this.backgroundColor = backgroundColor; this.style = style; this.backgroundDrawableId = getBackgroundDrawable(style); this.alignment = alignment; this.onClickListener = onClickListener; // default textColor if (textColor == -1) { this.textColor = getTextColor(style); } }
/** * Setup primary color for the arcs. * Basically initializes {@code mColorList} with provided color. * * @param color A {@link ColorInt}. */ protected void setPrimaryColor(@ColorInt int color) { // clear color list and make it null, so it's ignored when drawing and primary color will be used instead setColorList(null); if (mPrimaryColor != color) { mPrimaryColor = color; } }
MenuSecondaryItemsAdapter(Context context, @MenuRes int secondaryMenuId, View.OnClickListener onClickListener, @ColorInt int foregroundColour) { this.context = context; this.onClickListener = onClickListener; this.foregroundColour = foregroundColour; this.itemss = new ArrayList<>(); MenuParserHelper.parseMenu(context, secondaryMenuId, itemss); }
@ColorInt public static int getThemeAttrColor(@NonNull Context context, @AttrRes int attr) { TypedArray a = context.obtainStyledAttributes(null, new int[]{attr}); try { return a.getColor(0, 0); } finally { a.recycle(); } }
/** * Return a color-int from alpha, red, green, blue components. * * @param color color. * @param alpha alpha, alpha component [0..255] of the color. */ @ColorInt public static int getAlphaColor(@ColorInt int color, @IntRange(from = 0, to = 255) int alpha) { int red = Color.red(color); int green = Color.green(color); int blue = Color.blue(color); return Color.argb(alpha, red, green, blue); }
/** * 设置所有TextView 文本颜色 * * @param mLoadTextColor * @return */ public Builder setLoadTextColor(@ColorInt int mLoadTextColor) { setLoadingTextColor(mLoadTextColor); setLoadFailTextColor(mLoadTextColor); setLoadEndTextColor(mLoadTextColor); return this; }
public static NoRoot newInstance(String title, String description, @SuppressWarnings("SameParameterValue") @DrawableRes int drawable, @ColorInt int color) { NoRoot fragment = new NoRoot(); Bundle args = new Bundle(); args.putString(ARG_TITLE, title); args.putString(ARG_DESC, description); args.putInt(ARG_DRAWABLE, drawable); args.putInt(ARG_BG_COLOR, color); fragment.setArguments(args); return fragment; }
public void setTabOneTextColor(@ColorInt int tabOneTextColor) { if (!iconOnly) tabOneTextView.setTextColor(tabOneTextColor); else throw new IllegalArgumentException("You selected icons only."); }
/** * 设置顶部标题栏背景颜色 */ public void setTopBackgroundColor(@ColorInt int topBackgroundColor) { this.topBackgroundColor = topBackgroundColor; }
public static Drawable tintDrawable(Drawable d, @ColorInt int color) { Drawable wd = DrawableCompat.wrap(d); DrawableCompat.setTint(wd, color); return wd; }
@Override public void fillCircle(final float cx, final float cy, final float radius, @ColorInt final int color) { mCanvasParticlesView.fillCircle(cx, cy, radius, color); }
public void setTabThreeTextColor(@ColorInt int tabThreeTextColor) { if (!iconOnly) tabThreeTextView.setTextColor(tabThreeTextColor); else throw new IllegalArgumentException("You selected icons only."); }
public BadgeStyle setColorPressed(@ColorInt int colorPressed) { this.colorPressed = colorPressed; return this; }
@ColorInt public int getTextColor() { return this.textColorInt; }