下面列出了android.support.v4.content.ContextCompat#getDrawable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public MessageInternalUserTextViewHolder(View itemView, CustomSettings customSettings) {
super(itemView, customSettings);
avatar = (ImageView) itemView.findViewById(R.id.message_user_text_image_view_avatar);
carrot = (ImageView) itemView.findViewById(R.id.message_user_text_image_view_carrot);
initials = (TextView) itemView.findViewById(R.id.message_user_text_text_view_initials);
text = (TextView) itemView.findViewById(R.id.message_user_text_text_view_text);
timestamp = (TextView) itemView.findViewById(R.id.message_user_text_text_view_timestamp);
avatarContainer = (ViewGroup) itemView.findViewById(R.id.message_user_text_view_group_avatar);
bubble = (FrameLayout) itemView.findViewById(R.id.message_user_text_view_group_bubble);
Drawable drawable = ContextCompat.getDrawable(itemView.getContext(), R.drawable.shape_rounded_rectangle_white);
// Drawable drawable = itemView.getContext().getDrawable();
drawable.setColorFilter(customSettings.localBubbleBackgroundColor, PorterDuff.Mode.SRC_ATOP);
bubble.setBackground(drawable);
carrot.setColorFilter(customSettings.localBubbleBackgroundColor);
text.setTextColor(customSettings.localBubbleTextColor);
timestamp.setTextColor(customSettings.timestampColor);
}
private void initializeList() {
dataStore = new DataStore();
Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.post_item_divider_view);
ViewItemDecoration viewItemDecoration = new ViewItemDecoration(drawable);
viewItemDecoration.setWantTopOffset(false, 0);
competitionsListRecyclerAdapter = new CompetitionsListRecyclerAdapter(mContext);
competitionsListRecyclerAdapter.setDeleteListener(this);
competitionsListRecyclerAdapter.setLoadMoreCallback(this);
competitionList.setLayoutManager(new LinearLayoutManager(mContext));
competitionList.addItemDecoration(viewItemDecoration);
competitionList.setAdapter(competitionsListRecyclerAdapter);
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshCompetitions();
}
});
}
private void init(Context context, AttributeSet attributeSet) {
mButtonSpacing = (int) (getResources().getDimension(R.dimen.fab_actions_spacing) - getResources().getDimension(R.dimen.fab_shadow_radius) - getResources().getDimension(R.dimen.fab_shadow_offset));
mLabelsMargin = getResources().getDimensionPixelSize(R.dimen.fab_labels_margin);
mLabelsVerticalOffset = getResources().getDimensionPixelSize(R.dimen.fab_shadow_offset);
mTouchDelegateGroup = new TouchDelegateGroup(this);
setTouchDelegate(mTouchDelegateGroup);
TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.FloatingActionsMenu, 0, 0);
mAddButtonPlusColor = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonPlusIconColor, getColor(android.R.color.white));
mAddButtonColorNormal = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonColorNormal, getColor(android.R.color.holo_blue_dark));
mAddButtonColorPressed = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonColorPressed, getColor(android.R.color.holo_blue_light));
mAddButtonSize = attr.getInt(R.styleable.FloatingActionsMenu_fab_addButtonSize, FloatingActionButton.SIZE_NORMAL);
mAddButtonStrokeVisible = attr.getBoolean(R.styleable.FloatingActionsMenu_fab_addButtonStrokeVisible, true);
mExpandDirection = attr.getInt(R.styleable.FloatingActionsMenu_fab_expandDirection, EXPAND_UP);
mLabelsStyle = attr.getResourceId(R.styleable.FloatingActionsMenu_fab_labelStyle, 0);
mLabelsPosition = attr.getInt(R.styleable.FloatingActionsMenu_fab_labelsPosition, LABELS_ON_LEFT_SIDE);
attr.recycle();
if (mLabelsStyle != 0 && expandsHorizontally()) {
throw new IllegalStateException("Action labels in horizontal expand orientation is not supported.");
}
mCloseDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_close_drawable);
createSendButton(context);
}
/**
* 给图标染上当前提示文本的颜色并且转出Bitmap
* @param resources
* @param context
* @return
*/
public Bitmap createBitmap(int resources,Context context) {
final Drawable drawable = ContextCompat.getDrawable(context, resources);
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
return drawableToBitamp(wrappedDrawable);
}
@android.support.annotation.NonNull
public Drawable getIcon (Context context) {
PackageManager pm = context.getPackageManager();
try {
return pm.getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES).loadIcon(pm);
} catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
return ContextCompat.getDrawable(context, android.R.mipmap.sym_def_app_icon);
}
}
public static Bitmap getVectorBitmap(Context context, @DrawableRes int id) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
try {
return drawableToBitmap(ContextCompat.getDrawable(context, id));
} catch (Resources.NotFoundException e) {
return drawableToBitmap(null);
}
}
Drawable drawable;
try {
drawable = VectorDrawableCompat.create(context.getResources(), id, context.getTheme());
} catch (Resources.NotFoundException e1) {
try {
drawable = ContextCompat.getDrawable(context, id);
} catch (Resources.NotFoundException e2) {
return drawableToBitmap(null);
}
}
if (drawable != null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
drawable = (DrawableCompat.wrap(drawable)).mutate();
Bitmap result = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return result;
}
return null;
}
private Drawable getDrawable(WeakReference<Drawable> weak, int res){
Drawable drawable = null;
if (weak == null || weak.get() == null){
drawable = ContextCompat.getDrawable(mContext,res);
weak = new WeakReference<Drawable>(drawable);
}
else {
drawable = weak.get();
}
return drawable;
}
private void initFrameAnimDrawable(boolean isLoadFromXml) {
if (isLoadFromXml) {
this.mFrameAnimDrawable = (AnimationDrawable) ContextCompat.getDrawable(getBaseContext(), R.drawable.sdd_frameanim);
} else {
this.mFrameAnimDrawable = new AnimationDrawable();
for (int i=0; i< mFramwAnimDrawableResIDs.length; i++) {
this.mFrameAnimDrawable.addFrame(ContextCompat.getDrawable(getBaseContext(), mFramwAnimDrawableResIDs[i]), 100);
}
}
this.mFrameAnimDrawable.setOneShot(false);
this.mShowFrameAnimIV.setBackground(mFrameAnimDrawable);
}
public Builder setStickyDrawable(@DrawableRes int drawableId) {
this.mStickyDrawable = ContextCompat.getDrawable(mContext.getApplicationContext(), drawableId);
;
if (this.mStickyHeightOrWidth == 0) {
this.mStickyHeightOrWidth = mStickyDrawable.getIntrinsicHeight();
}
return this;
}
private void initDefaultConfig() {
setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER);
passCodeViewsList = new ArrayList<>();
activeDrawable = ContextCompat.getDrawable(getContext(), R.drawable.bg_active);
inActiveDrawable = ContextCompat.getDrawable(getContext(), R.drawable.bg_in_active);
textColor = android.R.color.transparent;
drawableSize = 72;
passcodeItemMargin = 8;
passcodeItems = new StringBuilder();
}
public void onConfigurationChanged(Configuration newConfig) {
if (!this.mHasCustomUpIndicator) {
this.mHomeAsUpIndicator = getThemeUpIndicator();
}
this.mDrawerImage = ContextCompat.getDrawable(this.mActivity, this.mDrawerImageResource);
syncState();
}
private void init(final Context context) {
final Drawable drawable = ContextCompat.getDrawable(context, R.drawable.icon_delete_32);
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable); //Wrap the drawable so that it can be tinted pre Lollipop
DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
mClearTextIcon = wrappedDrawable;
// mClearTextIcon= context.getResources().getDrawable(R.drawable.icon_delete_32);
mClearTextIcon.setBounds(0, 0, mClearTextIcon.getIntrinsicHeight(), mClearTextIcon.getIntrinsicHeight());
setClearIconVisible(false);
super.setOnTouchListener(this);
super.setOnFocusChangeListener(this);
addTextChangedListener(this);
}
@Override
public void tint() {
if (mCompoundButtonTintResId == 0 || !setSupportButtonDrawableTint(mCompoundButtonTintResId)) {
Drawable drawable = mTintManager.getDrawable(mCompoundButtonResId);
if (drawable == null) {
drawable = mCompoundButtonResId == 0 ? null : ContextCompat.getDrawable(mView.getContext(), mCompoundButtonResId);
}
setButtonDrawable(drawable);
}
}
public DividerItemDecoration(Context context, int orientation, int resId) {
mDivider = ContextCompat.getDrawable(context, resId);
setOrientation(orientation);
}
public DividerItemDecoration(Context context, int resId) {
this.divider = ContextCompat.getDrawable(context, resId);
}
static Bitmap getBitmap(Context context, @DrawableRes int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
return getBitmap(drawable);
}
void setGraphData(JSONArray currencyRateTrends) {
ArrayList<Entry> values = new ArrayList<>();
for (int i = 0; i < currencyRateTrends.length(); i++) {
try {
values.add(new Entry(i, (float) currencyRateTrends.getDouble(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
LineDataSet lineDataSet = new LineDataSet(values, GRAPH_LABEL_NAME);
lineDataSet.setDrawIcons(false);
lineDataSet.setColor(Color.RED);
lineDataSet.setCircleColor(Color.BLUE);
lineDataSet.setCircleRadius(1f);
lineDataSet.setLineWidth(1f);
lineDataSet.setCircleRadius(3f);
lineDataSet.setDrawCircleHole(true);
lineDataSet.setValueTextSize(10f);
lineDataSet.setValueTextColor(Color.BLACK);
lineDataSet.setDrawFilled(true);
lineDataSet.setFormSize(10.f);
if (Utils.getSDKInt() >= 18) {
// fill drawable only supported on api level 18 and above
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_green);
lineDataSet.setFillDrawable(drawable);
} else {
lineDataSet.setFillColor(Color.BLACK);
}
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(lineDataSet);
// create a data object with the datasets
LineData data = new LineData(dataSets);
// set data
graph.setData(data);
}
protected final Drawable getDrawable(@DrawableRes int drawable) {
return ContextCompat.getDrawable(context, drawable);
}
/**
* Inflate the view according to attributes
*
* @param attrs the attributes
*/
private void init(AttributeSet attrs) {
// inflate layout
View rootView = inflate(getContext(), R.layout.chip_view, this);
// butter knife
ButterKnife.bind(this, rootView);
// letter tile provider
mLetterTileProvider = new LetterTileProvider(mContext);
// attributes
if(attrs != null) {
TypedArray a = mContext.getTheme().obtainStyledAttributes(
attrs,
R.styleable.ChipView,
0, 0);
try {
// label
mLabel = a.getString(R.styleable.ChipView_label);
mLabelColor = a.getColorStateList(R.styleable.ChipView_labelColor);
// avatar icon
mHasAvatarIcon = a.getBoolean(R.styleable.ChipView_hasAvatarIcon, false);
int avatarIconId = a.getResourceId(R.styleable.ChipView_avatarIcon, NONE);
if(avatarIconId != NONE) mAvatarIconDrawable = ContextCompat.getDrawable(mContext, avatarIconId);
if(mAvatarIconDrawable != null) mHasAvatarIcon = true;
// delete icon
mDeletable = a.getBoolean(R.styleable.ChipView_deletable, false);
mDeleteIconColor = a.getColorStateList(R.styleable.ChipView_deleteIconColor);
int deleteIconId = a.getResourceId(R.styleable.ChipView_deleteIcon, NONE);
if(deleteIconId != NONE) mDeleteIcon = ContextCompat.getDrawable(mContext, deleteIconId);
// background color
mBackgroundColor = a.getColorStateList(R.styleable.ChipView_backgroundColor);
}
finally {
a.recycle();
}
}
// inflate
inflateWithAttributes();
}
/**
* 获取drawable资源
*
* @param id 资源id
* @return
*/
public static Drawable getDrawable(int id) {
return ContextCompat.getDrawable(getContext(), id);
}