下面列出了怎么用android.support.v7.widget.AppCompatDrawableManager的API类实例代码及写法,或者点击链接到github查看源代码。
private void renderMessageFromOthers(Message message) {
User user = message.getUser();
rootView.setGravity(Gravity.BOTTOM | Gravity.START);
displayNameTextView.setVisibility(View.VISIBLE);
avatarImageView.setVisibility(View.VISIBLE);
imageLoader.builder()
.load(user.getImageUrl())
.placeHolder(AppCompatDrawableManager.get().getDrawable(avatarImageView.getContext(),
R.drawable.ned_head_light))
.into(avatarImageView)
.circle()
.show();
displayNameTextView.setText(user.getName());
displayPayLoad(message.getPayload());
messageContainer.setBackgroundResource(R.drawable.background_message_from_others);
}
private void displayPayLoad(Payload payload) {
if (payload instanceof ImagePayload) {
ImagePayload imagePayload = (ImagePayload) payload;
imageLoader.builder()
.load(imagePayload.getImageMessage())
.placeHolder(AppCompatDrawableManager.get().getDrawable(avatarImageView.getContext(),
R.drawable.ned_head_light))
.into(messageImageView)
.show();
} else {
messageImageView.setVisibility(View.GONE);
}
TextPayLoad textPayload = (TextPayLoad) payload;
Spannable textMessage = textPayload.getTextMessage();
messageTextView.setText(textMessage);
}
@Override
public void render() {
Message message = getContent();
Uri sticker = ((StickerPayLoad) message.getPayload()).getSticker();
stickerImageView.setImageURI(sticker);
if (message.isFromMe()) {
avatarImageView.setVisibility(View.GONE);
rootView.setGravity(Gravity.BOTTOM | Gravity.END);
} else {
avatarImageView.setVisibility(View.VISIBLE);
rootView.setGravity(Gravity.BOTTOM | Gravity.START);
imageLoader.builder()
.load(message.getUser().getImageUrl())
.placeHolder(AppCompatDrawableManager.get().getDrawable(avatarImageView.getContext(),
R.drawable.ned_head_light))
.into(avatarImageView)
.circle()
.show();
}
}
public static void setNavButtonDrawable(Context context, ImageButton left, ImageButton right,
int monthTextAppearanceResId) {
// Retrieve the previous and next drawables
AppCompatDrawableManager dm = AppCompatDrawableManager.get();
Drawable prevDrawable = dm.getDrawable(context, R.drawable.ic_chevron_left_black_24dp);
Drawable nextDrawable = dm.getDrawable(context, R.drawable.ic_chevron_right_black_24dp);
// Proxy the month text color into the previous and next drawables.
final TypedArray ta = context.obtainStyledAttributes(null,
new int[]{android.R.attr.textColor}, 0, monthTextAppearanceResId);
final ColorStateList monthColor = ta.getColorStateList(0);
if (monthColor != null) {
DrawableCompat.setTint(DrawableCompat.wrap(prevDrawable), monthColor.getDefaultColor());
DrawableCompat.setTint(DrawableCompat.wrap(nextDrawable), monthColor.getDefaultColor());
}
ta.recycle();
// Set the previous and next drawables
left.setImageDrawable(prevDrawable);
right.setImageDrawable(nextDrawable);
}
private void init(Context context, AttributeSet attributeSet) {
mButtonSpacing = getResources().getDimensionPixelSize(R.dimen.fam_spacing);
mLabelsMargin = getResources().getDimensionPixelSize(R.dimen.fam_label_spacing);
mLabelsVerticalOffset = 0;
TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.FloatingActionMenu, 0, 0);
mExpandDirection = attr.getInt(R.styleable.FloatingActionMenu_fabMenuExpandDirection, EXPAND_UP);
mLabelsPosition = attr.getInt(R.styleable.FloatingActionMenu_fabMenuLabelPosition, LABELS_ON_LEFT_SIDE);
mLabelsStyle = attr.getResourceId(R.styleable.FloatingActionMenu_fabMenuLabelStyle, 0);
int mCloseDrawableResourceId = attr.getResourceId(R.styleable.FloatingActionMenu_fabMenuCloseIconSrc, 0);
mCloseDrawable = mCloseDrawableResourceId == 0 ? null : AppCompatDrawableManager.get().getDrawable(getContext(), mCloseDrawableResourceId);
mCloseAngle = attr.getFloat(R.styleable.FloatingActionMenu_fabMenuCloseIconAngle, 0);
mButtonSpacing = attr.getDimensionPixelSize(R.styleable.FloatingActionMenu_fabMenuSpacing, mButtonSpacing);
attr.recycle();
if (mLabelsStyle != 0 && expandsHorizontally()) {
throw new IllegalStateException("Action labels in horizontal expand orientation is not supported.");
}
// So we can catch the back button
setFocusableInTouchMode(true);
}
private Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
private void init(Context context, AttributeSet attrs) {
final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AnimatedPencil);
if (typedArray != null) {
color = typedArray.getColor(R.styleable.AnimatedPencil_pencil_color, color);
typedArray.recycle();
}
imageView = new AppCompatImageView(getContext());
addView(imageView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
drawable = AppCompatDrawableManager.get().getDrawable(getContext(), R.drawable.awsb_ic_edit_animated_24);
drawable = DrawableCompat.wrap(drawable).mutate();
DrawableCompat.setTint(drawable, color);
imageView.setImageDrawable(drawable);
}
/**
* Get mTickBitmap image from vector drawable defined in xml
* @param context - context to load drawable from resources
* @param drawableId - drawable id of the vector drawable
* @return - mTickBitmap image
*/
private Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < API_LEVEL_LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(topX, topY, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
@Nullable
static Drawable getDrawable(@NonNull Context context, @DrawableRes int res) {
try {
Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res);
return drawable.mutate();
} catch (OutOfMemoryError e) {
return null;
}
}
public static Drawable getDrawable(Context context, @DrawableRes int res) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return context.getResources().getDrawable(res, context.getTheme());
} else {
return AppCompatDrawableManager.get().getDrawable(context, res);
}
}
private static void setupCheckBox(CheckBox checkBox) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
checkBox.setButtonDrawable(R.drawable.btn_checkbox_circle);
checkBox.setBackgroundResource(R.drawable.btn_checkbox_circle_background);
} else {
Context context = checkBox.getContext();
AppCompatDrawableManager dm = AppCompatDrawableManager.get();
StateListDrawable button = new StateListDrawable();
button.addState(new int[]{android.R.attr.state_checked},
dm.getDrawable(context, R.drawable.ic_checkbox_circle_checked));
button.addState(new int[]{},
dm.getDrawable(context, R.drawable.ic_checkbox_circle_unchecked));
ColorStateList buttonTint = new ColorStateList(new int[][]{ // states
new int[]{android.R.attr.state_checked},
new int[]{} // state_default
}, new int[]{ // colors
ThemeUtils.getThemeAttrColor(context, R.attr.colorControlActivated),
ThemeUtils.getThemeAttrColor(context, R.attr.colorControlNormal)
});
Drawable buttonCompat = DrawableCompat.wrap(button);
DrawableCompat.setTintList(buttonCompat, buttonTint);
checkBox.setButtonDrawable(buttonCompat);
ShapeDrawable background = new ShapeDrawable(new OvalShape());
int backgroundTint = ThemeUtils.getThemeAttrColor(context, android.R.attr.colorBackground);
Drawable backgroundCompat = DrawableCompat.wrap(background);
DrawableCompat.setTint(backgroundCompat, backgroundTint);
ViewCompatUtils.setBackground(checkBox, backgroundCompat);
}
}
private void vector(CompositionAvatarView view) {
AppCompatDrawableManager drawableManager = AppCompatDrawableManager.get();
view.addDrawable(drawableManager.getDrawable(this, R.drawable.cloud_circle));
view.addDrawable(drawableManager.getDrawable(this, R.drawable.album));
view.addDrawable(drawableManager.getDrawable(this, R.drawable.group_work));
}
private View getCustomIcon(int resId, int alpha) {
return getCustomIcon(AppCompatDrawableManager.get().getDrawable(tabLayout.getContext(), resId), alpha);
}
private View getCustomIcon(int resId) {
return getCustomIcon(AppCompatDrawableManager.get().getDrawable(tabLayout.getContext(), resId), 255);
}
public Builder setPlaceholderImage(@DrawableRes int resId) {
return setPlaceholderImage(AppCompatDrawableManager.get().getDrawable(mContext, resId));
}
/**
* Retrieves the drawable specified with the <code>resourceId</code>. This
* is a helper method to deal with the API differences for retrieving drawables
*
* @param context The context to use when retrieving the drawable
* @param drawableResourceId The id for the drawable to retrieve
* @return The drawable associated with <code>resourceId</code>
*/
public static Drawable getDrawable(Context context, @DrawableRes int drawableResourceId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return context.getResources().getDrawable(drawableResourceId, context.getTheme());
}
return AppCompatDrawableManager.get().getDrawable(context, drawableResourceId);
}
/**
* Set a placeholder with an image. This will use the default circle color.
*
* @param resId The resource id of the placeholder drawable
*/
public void setPlaceholder(@DrawableRes int resId) {
setPlaceholder(AppCompatDrawableManager.get().getDrawable(getContext(), resId), mPlaceholderCircleDefaultColor);
}
/**
* Set a placeholder with an image and circle color.
*
* @param resId The resource id of the placeholder drawable
* @param circleColor Color to use for the circle
*/
public void setPlaceholder(@DrawableRes int resId, @ColorInt int circleColor) {
setPlaceholder(AppCompatDrawableManager.get().getDrawable(getContext(), resId), circleColor);
}