下面列出了怎么用android.support.annotation.DrawableRes的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressLint("ResourceType")
public Wear remoteInput(@DrawableRes int icon, String title, PendingIntent pendingIntent) {
if (icon <= 0) {
throw new IllegalArgumentException("Resource ID Icon Should Not Be Less Than Or Equal To Zero!");
}
if (title == null) {
throw new IllegalArgumentException("Title Must Not Be Null!");
}
if (pendingIntent == null) {
throw new IllegalArgumentException("PendingIntent Must Not Be Null!");
}
this.remoteInput = new RemoteInput.Builder(ZadakNotification.mSingleton.mContext.getString(R.string.key_voice_reply))
.setLabel(ZadakNotification.mSingleton.mContext.getString(R.string.label_voice_reply))
.setChoices(ZadakNotification.mSingleton.mContext.getResources().getStringArray(R.array.reply_choices))
.build();
wearableExtender.addAction(new NotificationCompat.Action.Builder(icon,
title, pendingIntent)
.addRemoteInput(remoteInput)
.build());
return this;
}
/**
* 方便初始化的方法
* @param drawableRes 默认状态的图标
* @param checkedDrawableRes 选中状态的图标
* @param title 标题
*/
public void initialize(@DrawableRes int drawableRes, @DrawableRes int checkedDrawableRes, String title)
{
mDefaultDrawable = drawableRes;
mCheckedDrawable = checkedDrawableRes;
mTitle.setText(title);
}
protected Drawable getMessageSelector(@ColorInt int normalColor, @ColorInt int selectedColor, @ColorInt int pressedColor, @DrawableRes int shape) {
Drawable drawable = DrawableCompat.wrap(getVectorDrawable(shape)).mutate();
DrawableCompat.setTintList(
drawable,
new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_selected},
new int[]{android.R.attr.state_pressed},
new int[]{-android.R.attr.state_pressed, -android.R.attr.state_selected}
},
new int[]{selectedColor, pressedColor, normalColor}
));
return drawable;
}
private static RemoteViews simpleRemoteViews(String packageName, String remoteViewsText,
@DrawableRes int drawableId) {
RemoteViews presentation = new RemoteViews(packageName,
R.layout.multidataset_service_list_item);
presentation.setTextViewText(R.id.text, remoteViewsText);
presentation.setImageViewResource(R.id.icon, drawableId);
return presentation;
}
FieldMetadata(AutofillId autofillId, String autofillHint, @StringRes int labelRes,
@DrawableRes int iconRes, int inputType) {
mAutofillId = autofillId;
mAutofillHint = autofillHint;
mLabelRes = labelRes;
mIconRes = iconRes;
mInputType = inputType;
}
/**
* 设置图片
*
* @param resourceId 图片资源id
* @param align 对齐
* @return {@link Builder}
*/
public Builder setResourceId(@DrawableRes int resourceId, @Align int align) {
this.resourceId = resourceId;
this.align = align;
this.text = " " + this.text;
imageIsResourceId = true;
return this;
}
/**
* @param drawableId 定制分割线的drawable资源id
* @return
*/
public BaseBuilder redrawDividerDrawable(@DrawableRes int drawableId) {
this.mDividerDrawable = ContextCompat.getDrawable(mContext.getApplicationContext(), drawableId);
if (mOrientation == VERTICAL) {
if (this.mRedrawDividerHeight == 0) {
this.mRedrawDividerHeight = mDividerDrawable.getIntrinsicHeight();
}
} else {
if (mRedrawDividerWidth == 0) {
this.mRedrawDividerWidth = mDividerDrawable.getIntrinsicWidth();
}
}
return this;
}
/**
* 设置右边Icon的资源
* @param resId
* @param horizontalPadding
* @param marginRight 最右边Icon的右边距
* @param listeners
* @return
*/
public ToolbarEx setRightIcon(@DrawableRes int resId, int horizontalPadding, int marginRight, OnClickListener listeners) {
Toolbar.LayoutParams params = generateDefaultLayoutParams();
params.rightMargin = marginRight;
params.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
rightIcon = new ImageView(getContext());
rightIcon.setImageResource(resId);
rightIcon.setPadding(horizontalPadding, 0, horizontalPadding, 0);
rightIcon.setOnClickListener(listeners);
addView(rightIcon, params);
return this;
}
public static Drawable getDrawableCompact(@DrawableRes int resId) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
return getContext().getDrawable(resId);
} else {
return getContext().getResources().getDrawable(resId);
}
}
public Builder setStickyDrawable(@DrawableRes int drawableId) {
this.mStickyDrawable = ContextCompat.getDrawable(mContext.getApplicationContext(), drawableId);
;
if (this.mStickyHeightOrWidth == 0) {
this.mStickyHeightOrWidth = mStickyDrawable.getIntrinsicHeight();
}
return this;
}
public static Drawable getDrawable(@NonNull Context context, @DrawableRes int resId){
Drawable drawable;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M){
drawable = context.getResources().getDrawable(resId, context.getTheme());
} else {
drawable = context.getResources().getDrawable(resId);
}
return drawable;
}
@Nullable
@Override
public Drawable getDrawable(@DrawableRes int resId)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
return mActivity.getDrawable(resId);
}
else
{
//noinspection deprecation
return mActivity.getResources().getDrawable(resId);
}
}
/**
* ImageView 设置图片 res
*
* @param id
* @param drawable
* @return
*/
public BaseViewHolder setImageResource(@IdRes int id, @DrawableRes int drawable) {
View view = getViewById(id);
if (view instanceof ImageView) {
((ImageView) view).setImageResource(drawable);
}
return this;
}
public static Bitmap getBitmap(@DrawableRes final int resId) {
Drawable drawable = ContextCompat.getDrawable(UtilsApp.getApp(), resId);
Canvas canvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
private @DrawableRes
int getName2ResId(String resName) {
int imgRid = 0;
try {
imgRid = R.mipmap.class.getDeclaredField(resName).getInt(R.mipmap.class);
} catch (Exception e) {
Logger.e(e.getMessage());
}
return imgRid;
}
private void setIndicatorResource(@DrawableRes int selectRes, @DrawableRes int unSelectRes, boolean byUser) {
if (byUser) {
checkAdapter("setIndicatorResource");
}
Drawable selectDrawable = ContextCompat.getDrawable(getContext(), selectRes);
Drawable unSelectDrawable = ContextCompat.getDrawable(getContext(), unSelectRes);
this.mSelectDrawable = selectDrawable;
this.mUnSelectDrawable = unSelectDrawable;
}
public Item(@Type int type, @DrawableRes int res, String content,
@FloatRange(from = 0f, to = 1f) float weight) {
this.type = type;
this.res = res;
this.content = content;
this.weight = weight;
}
@DrawableRes
private static int getDrawable(State currentState, State newState, boolean animate) {
switch (newState) {
case OFF:
if (animate) {
if (currentState == State.ON) {
return R.drawable.fingerprint_draw_off_animation;
} else if (currentState == State.ERROR) {
return R.drawable.fingerprint_error_off_animation;
}
}
return 0;
case ON:
if (animate) {
if (currentState == State.OFF) {
return R.drawable.fingerprint_draw_on_animation;
} else if (currentState == State.ERROR) {
return R.drawable.fingerprint_error_state_to_fp_animation;
}
}
return R.drawable.fingerprint_fingerprint;
case ERROR:
if (animate) {
if (currentState == State.ON) {
return R.drawable.fingerprint_fp_to_error_state_animation;
} else if (currentState == State.OFF) {
return R.drawable.fingerprint_error_on_animation;
}
}
return R.drawable.fingerprint_error;
default:
throw new IllegalArgumentException("Unknown state: " + newState);
}
}
/**
* @param drawableId 最右边分割线的drawable资源id
* @return
*/
public BaseBuilder redrawRightDividerDrawable(@DrawableRes int drawableId) {
this.mRightDividerDrawable = ContextCompat.getDrawable(mContext.getApplicationContext(), drawableId);
if (this.mRightDividerWidth == 0) {
this.mRightDividerWidth = mRightDividerDrawable.getIntrinsicWidth();
}
return this;
}
public BaseItemDecoration redrawFooterDividerDrawable(@DrawableRes int drawableId) {
this.mFooterDividerDrawable = ContextCompat.getDrawable(mContext.getApplicationContext(), drawableId);
if (this.mFooterDividerHeight == 0) {
this.mFooterDividerHeight = this.mFooterDividerDrawable.getIntrinsicHeight();
}
return this;
}
/**
* 设置背景资源
*
* @param drawid
* @return
*/
public FragmentWrapper setBackgroundResource(@DrawableRes int drawid) {
View view = baseFragment.getView();
if (view != null) {
view.setBackgroundResource(drawid);
}
return this;
}
private void init(Context context, AttributeSet attrs) {
if (isInEditMode()) {
setImageResource(android.R.drawable.ic_media_play);
return;
}
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.MorphView,
0, 0);
@DrawableRes int avdFromRes;
@DrawableRes int avdToRes;
try {
avdFromRes = a.getResourceId(R.styleable.MorphView_avdFirst, -1);
avdToRes = a.getResourceId(R.styleable.MorphView_avdSecond, -1);
} finally {
a.recycle();
}
showingAvdFirst = true;
avdFirstToSecond = AnimatedVectorDrawableCompat.create(getContext(), avdFromRes);
avdSecondToFirst = AnimatedVectorDrawableCompat.create(getContext(), avdToRes);
if (avdSecondToFirst == null || avdFirstToSecond == null) {
throw new RuntimeException("Drawable is not a valid AnimatedVectorDrawable");
} else {
setImageDrawable(avdFirstToSecond);
}
}
private void setInfo(String txt, @DrawableRes int drawableId) {
Log.d(TAG, "setInfo " + txt);
centerInfo.setVisibility(View.VISIBLE);
centerInfo.setText(txt);
centerInfo.setTextColor(0xffffffff);
centerInfo.setCompoundDrawablesWithIntrinsicBounds(0, drawableId, 0, 0);
}
/**
* 设置背景资源
*
* @param fragment fragment
* @param resId 资源Id
*/
public static void setBackgroundResource(@NonNull final Fragment fragment, @DrawableRes final int resId) {
View view = fragment.getView();
if (view != null) {
view.setBackgroundResource(resId);
}
}
/**
* 设置频道正常状态下背景
*
* @see StyleAdapter#setNormalStyle(ViewHolder)
*/
@Deprecated
public void setChannelNormalBackground(@DrawableRes int channelNormalBackground) {
if (checkDefaultAdapter()) {
for (View view : allTextView) {
defaultStyleAdapter.setBackgroundResource(view, channelNormalBackground);
}
}
}
/**
* 设置固定频道的背景
*
* @param channelFixedBackground
* @see StyleAdapter#setFixedStyle(ViewHolder)
*/
@Deprecated
public void setChannelFixedBackground(@DrawableRes int channelFixedBackground) {
if (checkDefaultAdapter()) {
for (View view : fixedTextView) {
defaultStyleAdapter.setBackgroundResource(view, channelFixedBackground);
}
}
}
/**
* 设置编辑按键背景
*
* @param tipEditBackground
*/
public void setTipEditBackground(@DrawableRes int tipEditBackground) {
this.tipEditBackground = tipEditBackground;
if (channelLayout != null && channelLayout.tipEdit != null) {
channelLayout.tipEdit.setBackgroundResource(tipEditBackground);
}
}
/**
* 设置完成按键背景
*
* @param tipFinishBackground
*/
public void setTipFinishBackground(@DrawableRes int tipFinishBackground) {
this.tipFinishBackground = tipFinishBackground;
if (channelLayout != null && channelLayout.tipFinish != null) {
channelLayout.tipFinish.setBackgroundResource(tipFinishBackground);
}
}
@Override
public void setImageResource(@DrawableRes int resId) {
super.setImageResource(resId);
mBitmap = getBitmapFromDrawable(getDrawable());
setup();
}
@Override
public void setImageResource(@DrawableRes int resId) {
super.setImageResource(resId);
mBitmap = getBitmapFromDrawable(getDrawable());
setup();
}