下面列出了android.content.Context#getDrawable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Extracts the information from a playlist model.
*
* @param playlist The current playlist model.
*/
public void setPlaylist(final PlaylistModel playlist) {
final Context context = getContext();
// title
String playlistTitle = playlist.getPlaylistName();
// get icon
Drawable icon = context.getDrawable(R.drawable.ic_queue_music_48dp);
if (icon != null) {
// get tint color
int tintColor = ThemeUtils.getThemeColor(context, R.attr.odyssey_color_text_background_secondary);
// tint the icon
DrawableCompat.setTint(icon, tintColor);
}
setTitle(playlistTitle);
setIcon(icon);
}
@Override
public Drawable getItemDrawable(int i) {
Context context = getContext();
switch (i) {
case HandMode.LEFT:
return context.getDrawable(R.drawable.ic_am_hand_left);
case HandMode.CENTER:
return context.getDrawable(R.drawable.ic_am_hand_center);
case HandMode.RIGHT:
return context.getDrawable(R.drawable.ic_am_hand_right);
default:
break;
}
return null;
}
public static Drawable getDrawable(Context context, int id) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
return context.getDrawable(id);
} else {
return context.getResources().getDrawable(id);
}
}
private void init(Context context) {
this.context = context;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
tickMark = context.getDrawable(R.drawable.seekbar_tickmark);
} else {
tickMark = context.getResources().getDrawable(R.drawable.seekbar_tickmark);
}
}
/**
* Creates a divider {@link RecyclerView.ItemDecoration} that can be used with a
* {@link LinearLayoutManager}.
*
* @param context Current context, it will be used to access resources.
* @param orientation Divider orientation. Should be {@link #HORIZONTAL} or {@link #VERTICAL}.
*/
public DividerItemDecoration(Context context, int orientation) {
// final TypedArray a = context.obtainStyledAttributes(ATTRS);
// mDivider = a.getDrawable(0);
// a.recycle();
// @By_syk
mDivider = context.getDrawable(R.drawable.app_list_divider);
setOrientation(orientation);
}
public void setDefaultComplicationDrawable(int resourceId) {
Context context = mWatchFaceArmsAndTicksView.getContext();
mDefaultComplicationDrawable = context.getDrawable(resourceId);
mLeftComplication.setImageDrawable(mDefaultComplicationDrawable);
mLeftComplicationBackground.setVisibility(View.INVISIBLE);
mRightComplication.setImageDrawable(mDefaultComplicationDrawable);
mRightComplicationBackground.setVisibility(View.INVISIBLE);
}
public static Drawable getDrawable(Context context, boolean rightHandle) {
int resId = StyledAttributesHelper.getResourceId(context, rightHandle ?
android.R.attr.textSelectHandleRight : android.R.attr.textSelectHandleLeft, -1);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
return context.getDrawable(resId);
else
return context.getResources().getDrawable(resId);
}
@Nullable
@SuppressWarnings("deprecation")
public static Drawable getDrawable(@NonNull Context context, @DrawableRes int drawableId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
return context.getDrawable(drawableId);
//noinspection deprecation
return context.getResources().getDrawable(drawableId);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static Drawable getDrawable(Context context, @DrawableRes int drawableRes) {
if (Build.VERSION.SDK_INT < 21) {
//noinspection deprecation
return context.getResources().getDrawable(drawableRes);
} else {
return context.getDrawable(drawableRes);
}
}
public Drawable loadGroupIcon(Context context, PackageManager pm) {
if (icon != 0) {
return loadUnbadgedIcon(pm);
} else {
return context.getDrawable(R.drawable.ic_perm_device_info);
}
}
static Drawable getDrawable(Context context, int drawableResId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return context.getDrawable(drawableResId);
} else {
try {
return VectorDrawableCompat.create(context.getResources(), drawableResId, null);
}catch (Resources.NotFoundException e){
return ContextCompat.getDrawable(context, drawableResId);
}
}
}
public static Drawable getMaskDrawable(Context context, int maskId) {
Drawable drawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable = context.getDrawable(maskId);
} else {
drawable = context.getResources().getDrawable(maskId);
}
if (drawable == null) {
throw new IllegalArgumentException("maskId is invalid");
}
return drawable;
}
public static Drawable getDrawable(Context context, int id) {
Drawable drawable = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
drawable = context.getDrawable(id);
} else {
drawable = AppCompatResources.getDrawable(context, id);
// drawable = context.getResources().getDrawable(id);
}
return drawable;
}
@TargetApi(LOLLIPOP)
@SuppressWarnings("deprecation")
public static Drawable getDrawable(Context context, @DrawableRes int id) {
if (App.isCompatible(LOLLIPOP)) {
return context.getDrawable(id);
} else {
return context.getResources().getDrawable(id);
}
}
public TintedDrawableSpan(Context context, int resourceId) {
super(ALIGN_BOTTOM);
mDrawable = context.getDrawable(resourceId);
mOldTint = 0;
}
private void updateStatus(String error) {
Context context = getContext();
Drawable icon = context.getDrawable(R.drawable.ic_status_error_24dp);
updateStatus(context.getString(R.string.status_cannot_connect_server_error, error), R.attr.colorAlert, icon);
}
public CustomKeyboardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
LayoutInflater inflate =
(LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int previewLayout = 0;
int keyTextSize = 0;
mKeyBackground = context.getDrawable(R.drawable.keyboard_key_background);
mKeyCapStartBackground = context.getDrawable(R.drawable.keyboard_key_background);
mVerticalCorrection = 0;
previewLayout = 0;
mPreviewOffset = 0;
mPreviewHeight = 80;
mKeyTextSize = context.getResources().getDimensionPixelSize(R.dimen.keyboard_key_text_size);
mKeyTextColor = 0xFFFFFFFF;
mLabelTextSize = context.getResources().getDimensionPixelSize(R.dimen.keyboard_key_longtext_size);
mPopupLayout = R.layout.keyboard;
mShadowColor = 0;
mShadowRadius = 0;
mBackgroundDimAmount = 0.5f;
clearHover();
mPreviewPopup = new PopupWindow(context);
if (previewLayout != 0) {
mPreviewText = (TextView) inflate.inflate(previewLayout, null);
mPreviewTextSizeLarge = (int) mPreviewText.getTextSize();
mPreviewPopup.setContentView(mPreviewText);
mPreviewPopup.setBackgroundDrawable(null);
} else {
mShowPreview = false;
}
mPreviewPopup.setTouchable(false);
mPopupKeyboard = new PopupWindow(context);
mPopupKeyboard.setBackgroundDrawable(null);
//mPopupKeyboard.setClippingEnabled(false);
mPopupParent = this;
//mPredicting = true;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(keyTextSize);
mPaint.setTextAlign(Align.CENTER);
mPaint.setAlpha(255);
mPaint.setTypeface(Typeface.create("sans-serif",Typeface.NORMAL));
mPadding = new Rect(0, 0, 0, 0);
mMiniKeyboardCache = new HashMap<>();
mKeyBackground.getPadding(mPadding);
mKeyboardHoveredPadding = getResources().getDimensionPixelSize(R.dimen.keyboard_key_hovered_padding);
mKeyboardPressedPadding = getResources().getDimensionPixelSize(R.dimen.keyboard_key_pressed_padding);
mSwipeThreshold = (int) (500 * getResources().getDisplayMetrics().density);
mDisambiguateSwipe = false;
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
resetMultiTap();
mForegroundColor = context.getColor(R.color.asphalt);
mSelectedForegroundColor = context.getColor(R.color.fog);
}
static Drawable getDrawable(@NonNull Context context, @DrawableRes int id) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
return context.getDrawable(id);
else
return context.getResources().getDrawable(id);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public DividerGridItemDecoration(Context context, @DrawableRes int widthDividerRes, @DrawableRes int heightDividerRes) {
mWidthDivider = context.getDrawable(widthDividerRes);
mHeightDivider = context.getDrawable(heightDividerRes);
}
/**
* Return a drawable object associated with a particular resource ID.
*
* <p>Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned drawable will be styled for the
* specified Context's theme.</p>
*
* @param id The desired resource identifier, as generated by the aapt tool.
* This integer encodes the package, type, and resource entry.
* The value 0 is an invalid identifier.
* @return Drawable An object that can be used to draw this resource.
*/
static Drawable getDrawable(Context context, int id) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return context.getDrawable(id);
}
return context.getResources().getDrawable(id);
}