下面列出了怎么用androidx.annotation.DrawableRes的API类实例代码及写法,或者点击链接到github查看源代码。
public TransportOption(@NonNull Type type,
@DrawableRes int drawable,
int backgroundColor,
@NonNull String text,
@NonNull String composeHint,
@NonNull CharacterCalculator characterCalculator,
@NonNull Optional<CharSequence> simName,
@NonNull Optional<Integer> simSubscriptionId)
{
this.type = type;
this.drawable = drawable;
this.backgroundColor = backgroundColor;
this.text = text;
this.composeHint = composeHint;
this.characterCalculator = characterCalculator;
this.simName = simName;
this.simSubscriptionId = simSubscriptionId;
}
@Override
public int getSwipeDirs(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
if (viewHolder instanceof ThreadOverviewAdapter.ThreadOverviewViewHolder) {
final ThreadOverviewAdapter.ThreadOverviewViewHolder threadOverviewViewHolder = (ThreadOverviewAdapter.ThreadOverviewViewHolder) viewHolder;
if (onQueryItemSwipe != null) {
final Swipable swipable = onQueryItemSwipe.onQueryItemSwipe(viewHolder.getAdapterPosition());
if (swipable == Swipable.NO) {
return 0;
}
@DrawableRes final int resource;
switch (swipable) {
case ARCHIVE:
resource = R.drawable.ic_archive_white_24dp;
break;
case REMOVE_FLAGGED:
resource = R.drawable.ic_star_border_white_24dp;
break;
default:
resource = R.drawable.ic_label_off_white_24dp;
break;
}
threadOverviewViewHolder.binding.endSwipeActionIndicator.setImageResource(resource);
threadOverviewViewHolder.binding.startSwipeActionIndicator.setImageResource(resource);
return ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT;
}
}
return 0;
}
/**
* Update the state of pause/resume action item in Picture-in-Picture mode.
*
* @param iconId The icon to be used.
* @param title The title text.
* @param controlType The type of the action. either {@link #CONTROL_TYPE_PLAY} or {@link
* #CONTROL_TYPE_PAUSE}.
* @param requestCode The request code for the {@link PendingIntent}.
*/
void updatePictureInPictureActions(
@DrawableRes int iconId, String title, int controlType, int requestCode) {
final ArrayList<RemoteAction> actions = new ArrayList<>();
// This is the PendingIntent that is invoked when a user clicks on the action item.
// You need to use distinct request codes for play and pause, or the PendingIntent won't
// be properly updated.
final PendingIntent intent =
PendingIntent.getBroadcast(
MainActivity.this,
requestCode,
new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, controlType),
0);
final Icon icon = Icon.createWithResource(MainActivity.this, iconId);
actions.add(new RemoteAction(icon, title, title, intent));
// Another action item. This is a fixed action.
actions.add(
new RemoteAction(
Icon.createWithResource(MainActivity.this, R.drawable.ic_info_24dp),
getString(R.string.info),
getString(R.string.info_description),
PendingIntent.getActivity(
MainActivity.this,
REQUEST_INFO,
new Intent(
Intent.ACTION_VIEW,
Uri.parse(getString(R.string.info_uri))),
0)));
mPictureInPictureParamsBuilder.setActions(actions);
// This is how you can update action items (or aspect ratio) for Picture-in-Picture mode.
// Note this call can happen even when the app is not in PiP mode. In that case, the
// arguments will be used for at the next call of #enterPictureInPictureMode.
setPictureInPictureParams(mPictureInPictureParamsBuilder.build());
}
public Setting(@DrawableRes int icon, String title, String subtitle, String content, String footer,
@NonNull Setting.Type type, @IntRange(from = -1, to = 1) int checkState) {
mIcon = icon;
mTitle = title;
mSubtitle = subtitle;
mContent = content;
mFooter = footer;
mType = type;
mCheckState = checkState;
}
public static AlertDialog.Builder createFor(@NonNull Context context, @NonNull String message, @DrawableRes int... drawables) {
View view = LayoutInflater.from(context).inflate(R.layout.permissions_rationale_dialog, null);
ViewGroup header = view.findViewById(R.id.header_container);
TextView text = view.findViewById(R.id.message);
for (int i=0;i<drawables.length;i++) {
Drawable drawable = ContextCompat.getDrawable(context, drawables[i]);
DrawableCompat.setTint(drawable, ContextCompat.getColor(context, R.color.white));
ImageView imageView = new ImageView(context);
imageView.setImageDrawable(drawable);
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
header.addView(imageView);
if (i != drawables.length - 1) {
TextView plus = new TextView(context);
plus.setText("+");
plus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 40);
plus.setTextColor(Color.WHITE);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(ViewUtil.dpToPx(context, 20), 0, ViewUtil.dpToPx(context, 20), 0);
plus.setLayoutParams(layoutParams);
header.addView(plus);
}
}
text.setText(message);
return new AlertDialog.Builder(context, ThemeUtil.isDarkTheme(context) ? R.style.Theme_Signal_AlertDialog_Dark_Cornered : R.style.Theme_Signal_AlertDialog_Light_Cornered)
.setView(view);
}
public TransportOption(@NonNull Type type,
@DrawableRes int drawable,
int backgroundColor,
@NonNull String text,
@NonNull String composeHint,
@NonNull CharacterCalculator characterCalculator)
{
this(type, drawable, backgroundColor, text, composeHint, characterCalculator,
Optional.<CharSequence>absent(), Optional.<Integer>absent());
}
private Bitmap createVerifiedBitmap(int width, int height, @DrawableRes int id) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Bitmap check = BitmapFactory.decodeResource(getResources(), id);
float offset = (width - check.getWidth()) / 2;
canvas.drawBitmap(check, offset, offset, null);
return bitmap;
}
public static NotificationController startForegroundTask(@NonNull Context context, @NonNull String task, @NonNull String channelId, @DrawableRes int iconRes) {
final int id = NEXT_ID.getAndIncrement();
Intent intent = new Intent(context, GenericForegroundService.class);
intent.setAction(ACTION_START);
intent.putExtra(EXTRA_TITLE, task);
intent.putExtra(EXTRA_CHANNEL_ID, channelId);
intent.putExtra(EXTRA_ICON_RES, iconRes);
intent.putExtra(EXTRA_ID, id);
ContextCompat.startForegroundService(context, intent);
return new NotificationController(context, id);
}
private Entry(@NonNull String title, @NonNull String channelId, @DrawableRes int iconRes, int id, int progressMax, int progress, boolean indeterminate) {
this.title = title;
this.channelId = channelId;
this.iconRes = iconRes;
this.id = id;
this.progress = progress;
this.progressMax = progressMax;
this.indeterminate = indeterminate;
}
public static GlideRequest<Drawable> getSelfAvatarOrFallbackIcon(@NonNull Context context, @DrawableRes int fallbackIcon) {
return GlideApp.with(context)
.asDrawable()
.load(new ProfileContactPhoto(Recipient.self(), Recipient.self().getProfileAvatar()))
.error(fallbackIcon)
.circleCrop()
.diskCacheStrategy(DiskCacheStrategy.ALL);
}
private static NotificationCompat.Action getActivityNotificationAction(@NonNull Context context, @NonNull String action,
@DrawableRes int iconResId, @StringRes int titleResId)
{
Intent intent = new Intent(context, WebRtcCallActivity.class);
intent.setAction(action);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
return new NotificationCompat.Action(iconResId, context.getString(titleResId), pendingIntent);
}
public AbstractSetting(String title, String description, @Nullable @DrawableRes Integer abstractIcon, int layoutId) {
this.title = title;
this.description = description;
this.layoutId = layoutId;
this.selectionAbstract = null;
this.abstractIconResource = abstractIcon;
}
public void setCloseButtonIcon(@DrawableRes int buttonIcon) {
if (closeBtn == null) {
return;
}
closeBtn.setImageResource(buttonIcon);
}
private static MaterialSimpleListItem getThemeDialogAdapterItem(@StringRes int title, @DrawableRes int icon, Activity activity) {
MaterialSimpleListItem.Builder builder = new MaterialSimpleListItem.Builder(activity)
.content(title)
.icon(icon);
return builder.build();
}
public void insertBlockImage(@DrawableRes int resourceId, @NonNull BlockImageSpanVm blockImageSpanVm,
OnImageClickListener onImageClickListener) {
try {
Drawable drawable = AppCompatResources.getDrawable(mActivity, resourceId);
insertBlockImage(drawable, blockImageSpanVm, onImageClickListener);
} catch (Exception e) {
LogUtil.e(TAG, "Unable to find resource: " + resourceId);
}
}
private Bitmap[] generateIconBitmaps(@DrawableRes int origin) {
if (origin == -1) {
return null;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), origin, options);
int size = Math.max(options.outWidth, options.outHeight);
options.inSampleSize = size > iconSize ? size / iconSize : 1;
options.inJustDecodeBounds = false;
return generateIconBitmaps(BitmapFactory.decodeResource(getResources(), origin, options));
}
public static ImageFragment newInstance(@DrawableRes int drawableRes) {
ImageFragment fragment = new ImageFragment();
Bundle argument = new Bundle();
argument.putInt(KEY_IMAGE_RES, drawableRes);
fragment.setArguments(argument);
return fragment;
}
private Drawable getColorPreviewFromTheme(String themeTitle) {
@DrawableRes int drawableRes = R.drawable.circle_theme_blue_chooser;
if (themeTitle.equals(getString(R.string.purple_theme_name))) {
drawableRes = R.drawable.circle_theme_purple_chooser;
} else if (themeTitle.equals(getString(R.string.black_theme_name))) {
drawableRes = R.drawable.circle_theme_black_chooser;
} else if (themeTitle.equals(getString(R.string.night_theme_name))) {
drawableRes = R.drawable.circle_theme_night_chooser;
}
return ContextCompat.getDrawable(getContext(), drawableRes);
}
public void setLevel(int level, boolean charged) {
percentage = level;
@DrawableRes int drawableRes = R.drawable.battery_unknown_on_background;
if (charged) {
if (level < 20) {
drawableRes = R.drawable.battery_20_c_on_background;
} else if (level < 30) {
drawableRes = R.drawable.battery_30_c_on_background;
} else if (level < 50) {
drawableRes = R.drawable.battery_50_c_on_background;
} else if (level < 60) {
drawableRes = R.drawable.battery_60_c_on_background;
} else if (level < 80) {
drawableRes = R.drawable.battery_80_c_on_background;
} else if (level < 90) {
drawableRes = R.drawable.battery_90_c_on_background;
} else if (level < 100) {
drawableRes = R.drawable.battery_100_charging;
} else
drawableRes = R.drawable.battery_full_on_background;
} else {
if (level < D.LOW_BATTERY_LEVEL) {
drawableRes = R.drawable.battery_20_on_background;
} else if (level < 30) {
drawableRes = R.drawable.battery_30_on_background;
} else if (level < 50) {
drawableRes = R.drawable.battery_50_on_background;
} else if (level < 60) {
drawableRes = R.drawable.battery_60_on_background;
} else if (level < 80) {
drawableRes = R.drawable.battery_80_on_background;
} else if (level < 90) {
drawableRes = R.drawable.battery_90_on_background;
} else if (level <= 100) {
drawableRes = R.drawable.battery_full_on_background;
}
}
setImageResource(drawableRes);
}
public static Drawable getVectorDrawable(@NonNull Resources res,
@DrawableRes int resId, @Nullable Resources.Theme theme) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return res.getDrawable(resId, theme);
} else {
return VectorDrawableCompat.create(res, resId, theme);
}
}
void setFlagResource(@DrawableRes final int flagResource) {
this.flagResource = flagResource;
}
@Nullable
@DrawableRes
public Integer getErrorDrawable() {
return errorDrawable;
}
public void setErrorDrawable(@Nullable @DrawableRes Integer errorDrawable) {
this.errorDrawable = errorDrawable;
}
private OnboardingResources(
@DrawableRes int icon, @StringRes int title, @StringRes int message) {
iconResId = icon;
titleResId = title;
messageResId = message;
}
public void setImageResource(@DrawableRes int resId) {
imageView.setImageResource(resId);
}
public PopupItem setIcon(@DrawableRes int icon) {
mIcon = icon;
return this;
}
@DrawableRes
public int getIcon() {
return mIcon;
}
BDBSettingsItem(@StringRes int textResId, BDB bdb, @DrawableRes int drawableResId) {
super(textResId, drawableResId);
this.bdb = bdb;
}
/**
* Channel Logo for published program
*/
private void createChannelLogo(Context context, long channelId,
@DrawableRes int drawableId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId);
ChannelLogoUtils.storeChannelLogo(context, channelId, bitmap);
}
public HelpRecyclerViewAdapter(@NonNull final Context context, @StringRes final int[] texts, @DrawableRes final int[] pics) {
inflater = LayoutInflater.from(context);
this.pics = pics;
this.texts = texts;
}