下面列出了怎么用android.support.annotation.ColorRes的API类实例代码及写法,或者点击链接到github查看源代码。
@Nullable
public ColorStateList getColorStateList(@ColorRes int resId) {
if (resId == 0) return null;
final Context context = mContextRef.get();
if (context == null) return null;
ColorStateList colorStateList = mCacheTintList != null ? mCacheTintList.get(resId) : null;
if (colorStateList == null) {
colorStateList = ColorStateListUtils.createColorStateList(context, resId);
if (colorStateList != null) {
if (mCacheTintList == null) {
mCacheTintList = new SparseArray<>();
}
mCacheTintList.append(resId, colorStateList);
}
}
return colorStateList;
}
public static int getColor(@NonNull Context context, @ColorRes int id) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return context.getColor(id);
} else {
return context.getResources().getColor(id);
}
}
public static int getColor(@NonNull Context context, @ColorRes int resId){
int color;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
color = context.getResources().getColor(resId, context.getTheme());
} else {
color = context.getResources().getColor(resId);
}
return color;
}
/**
* 解决布局与状态栏重叠问题,支持侧滑返回
* Fits system windows immersion bar.
*
* @param fits the fits
* @param statusBarColorContentView the status bar color content view 状态栏颜色
* @param statusBarColorContentViewTransform the status bar color content view transform 状态栏变色后的颜色
* @param statusBarContentViewAlpha the status bar content view alpha 透明度
* @return the immersion bar
*/
public ImmersionBar fitsSystemWindows(boolean fits, @ColorRes int statusBarColorContentView
, @ColorRes int statusBarColorContentViewTransform, @FloatRange(from = 0f, to = 1f) float statusBarContentViewAlpha) {
mBarParams.fits = fits;
mBarParams.statusBarColorContentView = ContextCompat.getColor(mActivity, statusBarColorContentView);
mBarParams.statusBarColorContentViewTransform = ContextCompat.getColor(mActivity, statusBarColorContentViewTransform);
mBarParams.statusBarContentViewAlpha = statusBarContentViewAlpha;
mBarParams.statusBarColorContentView = ContextCompat.getColor(mActivity, statusBarColorContentView);
mContentView.setBackgroundColor(ColorUtils.blendARGB(mBarParams.statusBarColorContentView,
mBarParams.statusBarColorContentViewTransform, mBarParams.statusBarContentViewAlpha));
return this;
}
/**
* 获取加透明度的资源颜色
*/
@ColorInt
public static int getResColor(@IntRange(from = 0, to = 255) int alpha, @ColorRes int colorId) {
int color = getResColor(colorId);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
@Override
public int replaceColorById(Context context, @ColorRes int colorId) {
if (ThemeManager.isDefaultTheme(context)) {
return context.getResources().getColor(colorId);
}
String theme = getTheme(context);
if (theme != null) {
colorId = getThemeColorId(context, colorId, theme);
}
return context.getResources().getColor(colorId);
}
@Nullable
private Integer colorResOrInt(Context context, @Nullable Integer value, @ColorRes int resource) {
if (resource != -1) {
return ContextCompat.getColor(context, resource);
}
return value;
}
@ColorRes
private int getColorResAt(int position) {
Integer item = getItem(position);
if (item == null) {
return usage.getDefaultColor();
} else {
return item.intValue();
}
}
private @ColorRes
int getThemeColorId(Context context, int colorId, String theme) {
switch (colorId) {
case R.color.theme_color_primary:
return context.getResources().getIdentifier(theme, "color", getPackageName());
case R.color.theme_color_primary_dark:
return context.getResources().getIdentifier(theme + "_dark", "color", getPackageName());
case R.color.playbarProgressColor:
return context.getResources().getIdentifier(theme + "_trans", "color", getPackageName());
}
return colorId;
}
private void setTextColor(@ColorRes int resId) {
if (mTextColorId != resId) {
resetTextColorTintResource(resId);
if (resId != 0) {
setSupportTextColorTint(resId);
}
}
}
public static @ColorInt int getColorCompact(@ColorRes int colorRes) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return PalmApp.getContext().getColor(colorRes);
} else {
return PalmApp.getContext().getResources().getColor(colorRes);
}
}
@Override
public void setTextColorById(@ColorRes int colorId) {
if (mTextHelper != null) {
mTextHelper.setTextColorById(colorId);
}
}
public void setBorderColorResource(@ColorRes int borderColorRes) {
setBorderColor(getContext().getResources().getColor(borderColorRes));
}
public void setSecondaryLineColor(@ColorRes int color) {
GREY = color;
}
public NavigationItem(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.NavigationItem,
defStyleAttr, defStyleRes);
int activityMinSdk = typedArray.getInteger(R.styleable.NavigationItem_minSdk, 26);
// TODO: Remove BuildCompat.isAtLeastP() check when API 28 is finalized.
int deviceMinSdk = BuildCompat.isAtLeastP() ? 28 : Build.VERSION.SDK_INT;
if (deviceMinSdk < activityMinSdk) {
// If device's SDK is lower than the minSdk specified by the NavigationItem, hide it.
setVisibility(View.GONE);
return;
}
String labelText = typedArray.getString(R.styleable.NavigationItem_labelText);
String infoText = typedArray.getString(R.styleable.NavigationItem_infoText);
Drawable logoDrawable = typedArray.getDrawable(R.styleable.NavigationItem_itemLogo);
@ColorRes int colorRes = typedArray.getResourceId(R.styleable.NavigationItem_imageColor, 0);
String launchingActivityName = typedArray.getString(R.styleable.NavigationItem_destinationActivityName);
int imageColor = ContextCompat.getColor(getContext(), colorRes);
typedArray.recycle();
View rootView = LayoutInflater.from(context).inflate(R.layout.navigation_item, this);
TextView buttonLabel = rootView.findViewById(R.id.buttonLabel);
buttonLabel.setText(labelText);
if (logoDrawable != null) {
Drawable mutatedLogoDrawable = logoDrawable.mutate();
mutatedLogoDrawable.setColorFilter(imageColor, PorterDuff.Mode.SRC_IN);
buttonLabel.setCompoundDrawablesRelativeWithIntrinsicBounds(mutatedLogoDrawable, null,
null, null);
}
InfoButton infoButton = rootView.findViewById(R.id.infoButton);
infoButton.setInfoText(infoText);
infoButton.setColorFilter(imageColor);
CardView outerView = rootView.findViewById(R.id.cardView);
outerView.setOnClickListener((view) -> {
if (launchingActivityName != null) {
Intent intent = new Intent();
intent.setClassName(getContext().getPackageName(), launchingActivityName);
context.startActivity(intent);
} else {
Log.w(TAG, "Launching Activity name not set.");
}
});
}
@ColorInt
public static int getColor(@ColorRes int id) {
return UtilsApp.getApp().getResources().getColor(id);
}
public FeedBackDialog setBackgroundColor(@ColorRes int mBackgroundColor) {
this.mBackgroundColor = mBackgroundColor;
return this;
}
public FeedBackDialog setIconColor(@ColorRes int mIconColor)
{
this.mIconColor = mIconColor;
return this;
}
ThemeColor(@ColorRes int colorRes, @ColorRes int darkColorRes, String identifyName, String displayName) {
this.colorRes = colorRes;
this.darkColorRes = darkColorRes;
this.identifyName = identifyName;
this.displayName = displayName;
}
public Builder setLeftBackgroundColorResource(@ColorRes int resId) {
mLeftBackgroundColor = mContext.getResources().getColor(resId);
return this;
}
public Builder setRightBackgroundColorResource(@ColorRes int resId) {
mRightBackgroundColor = mContext.getResources().getColor(resId);
return this;
}
public SwipeRefreshWidget colorSchemeResources(@ColorRes int... colorResIds) {
view.setColorSchemeResources(colorResIds);
return self();
}
/**
* @deprecated Use {@link #setBorderColor(int)} instead
*/
@Deprecated
public void setBorderColorResource(@ColorRes int borderColorRes) {
setBorderColor(getContext().getResources().getColor(borderColorRes));
}
public MaterialButtonWidget iconTintResource(@ColorRes int iconTintResourceId) {
view.setIconTintResource(iconTintResourceId);
return self();
}
public H setTextColorRes(@ColorRes int color, @NonNull Context context){
mProgress.setTextColor(context.getResources().getColor(color));
return (H) this;
}
public Builder setBottomColorRes(@ColorRes int ColorRes, Context context) {
mProgress.mCircleBottomColor = context.getResources().getColor(ColorRes);
return this;
}
public Builder setProgressColorRes(@ColorRes int ColorRes, Context context) {
mProgress.mCircleProgressColor = context.getResources().getColor(ColorRes);
return this;
}
public int getColor(@ColorRes int resId) {
return ContextCompat.getColor(getContext(), resId);
}
@ColorInt
public static int getResColor(Context context, @ColorRes int colorId){
return ContextCompat.getColor(context, colorId);
}
public int getColor(@ColorRes int resId) {
return ContextCompat.getColor(getContext(), resId);
}