Java源码示例:androidx.core.graphics.drawable.DrawableCompat
示例1
/**
* Calculates the chip icon's ChipDrawable-absolute bounds (top-left is <code>
* [ChipDrawable.getBounds().left, ChipDrawable.getBounds().top]</code>).
*/
private void calculateChipIconBounds(@NonNull Rect bounds, @NonNull RectF outBounds) {
outBounds.setEmpty();
if (showsChipIcon() || showsCheckedIcon()) {
float offsetFromStart = chipStartPadding + iconStartPadding;
float chipWidth = getCurrentChipIconWidth();
if (DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
outBounds.left = bounds.left + offsetFromStart;
outBounds.right = outBounds.left + chipWidth;
} else {
outBounds.right = bounds.right - offsetFromStart;
outBounds.left = outBounds.right - chipWidth;
}
float chipHeight = getCurrentChipIconHeight();
outBounds.top = bounds.exactCenterY() - chipHeight / 2f;
outBounds.bottom = outBounds.top + chipHeight;
}
}
示例2
/**
* Set the drawable to use for the status bar scrim from resources. Providing null will disable
* the scrim functionality.
*
* <p>This scrim is only shown when we have been given a top system inset.
*
* @param drawable the drawable to display
* @attr ref R.styleable#CollapsingToolbarLayout_statusBarScrim
* @see #getStatusBarScrim()
*/
public void setStatusBarScrim(@Nullable Drawable drawable) {
if (statusBarScrim != drawable) {
if (statusBarScrim != null) {
statusBarScrim.setCallback(null);
}
statusBarScrim = drawable != null ? drawable.mutate() : null;
if (statusBarScrim != null) {
if (statusBarScrim.isStateful()) {
statusBarScrim.setState(getDrawableState());
}
DrawableCompat.setLayoutDirection(statusBarScrim, ViewCompat.getLayoutDirection(this));
statusBarScrim.setVisible(getVisibility() == VISIBLE, false);
statusBarScrim.setCallback(this);
statusBarScrim.setAlpha(scrimAlpha);
}
ViewCompat.postInvalidateOnAnimation(this);
}
}
示例3
@UiThread // Implicit synchronization for use of shared resource VALUE.
public static Drawable getTintedDrawable(Context context,
@DrawableRes int id, @AttrRes int tintAttrId) {
boolean attributeFound = context.getTheme().resolveAttribute(tintAttrId, VALUE, true);
if (!attributeFound) {
throw new Resources.NotFoundException("Required tint color attribute with name "
+ context.getResources().getResourceEntryName(tintAttrId)
+ " and attribute ID "
+ tintAttrId
+ " was not found.");
}
Drawable drawable = ContextCompat.getDrawable(context, id);
drawable = DrawableCompat.wrap(drawable.mutate());
int color = ContextCompat.getColor(context, VALUE.resourceId);
DrawableCompat.setTint(drawable, color);
return drawable;
}
示例4
public void setCloseIcon(@Nullable Drawable closeIcon) {
Drawable oldCloseIcon = getCloseIcon();
if (oldCloseIcon != closeIcon) {
float oldCloseIconWidth = calculateCloseIconWidth();
this.closeIcon = closeIcon != null ? DrawableCompat.wrap(closeIcon).mutate() : null;
if (RippleUtils.USE_FRAMEWORK_RIPPLE) {
updateFrameworkCloseIconRipple();
}
float newCloseIconWidth = calculateCloseIconWidth();
unapplyChildDrawable(oldCloseIcon);
if (showsCloseIcon()) {
applyChildDrawable(this.closeIcon);
}
invalidateSelf();
if (oldCloseIconWidth != newCloseIconWidth) {
onSizeChange();
}
}
}
示例5
private void calculateCloseIconTouchBounds(@NonNull Rect bounds, @NonNull RectF outBounds) {
outBounds.setEmpty();
if (showsCloseIcon()) {
float offsetFromEnd =
chipEndPadding
+ closeIconEndPadding
+ closeIconSize
+ closeIconStartPadding
+ textEndPadding;
if (DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
outBounds.right = bounds.right;
outBounds.left = outBounds.right - offsetFromEnd;
} else {
outBounds.left = bounds.left;
outBounds.right = bounds.left + offsetFromEnd;
}
outBounds.top = bounds.top;
outBounds.bottom = bounds.bottom;
}
}
示例6
@SuppressLint("RestrictedApi")
protected boolean applyMenuItemTheme(Menu menu) {
if (customThemeWrapper != null) {
int size = Math.min(menu.size(), 2);
for (int i = 0; i < size; i++) {
MenuItem item = menu.getItem(i);
if (((MenuItemImpl) item).requestsActionButton()) {
Drawable drawable = item.getIcon();
if (drawable != null) {
DrawableCompat.setTint(drawable, customThemeWrapper.getToolbarPrimaryTextAndIconColor());
item.setIcon(drawable);
}
}
}
}
return true;
}
示例7
public void setChipIcon(@Nullable Drawable chipIcon) {
Drawable oldChipIcon = getChipIcon();
if (oldChipIcon != chipIcon) {
float oldChipIconWidth = calculateChipIconWidth();
this.chipIcon = chipIcon != null ? DrawableCompat.wrap(chipIcon).mutate() : null;
float newChipIconWidth = calculateChipIconWidth();
unapplyChildDrawable(oldChipIcon);
if (showsChipIcon()) {
applyChildDrawable(this.chipIcon);
}
invalidateSelf();
if (oldChipIconWidth != newChipIconWidth) {
onSizeChange();
}
}
}
示例8
/** Calculates the chip text's origin and alignment based on the ChipDrawable-absolute bounds. */
@NonNull
Align calculateTextOriginAndAlignment(@NonNull Rect bounds, @NonNull PointF pointF) {
pointF.set(0, 0);
Align align = Align.LEFT;
if (text != null) {
float offsetFromStart = chipStartPadding + calculateChipIconWidth() + textStartPadding;
if (DrawableCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_LTR) {
pointF.x = bounds.left + offsetFromStart;
align = Align.LEFT;
} else {
pointF.x = bounds.right - offsetFromStart;
align = Align.RIGHT;
}
pointF.y = bounds.centerY() - calculateTextCenterFromBaseline();
}
return align;
}
示例9
private void applyIconTint(
@NonNull CheckableImageButton iconView,
boolean hasIconTintList,
ColorStateList iconTintList,
boolean hasIconTintMode,
PorterDuff.Mode iconTintMode) {
Drawable icon = iconView.getDrawable();
if (icon != null && (hasIconTintList || hasIconTintMode)) {
icon = DrawableCompat.wrap(icon).mutate();
if (hasIconTintList) {
DrawableCompat.setTintList(icon, iconTintList);
}
if (hasIconTintMode) {
DrawableCompat.setTintMode(icon, iconTintMode);
}
}
if (iconView.getDrawable() != icon) {
iconView.setImageDrawable(icon);
}
}
示例10
/**
* Set the drawable to use for the status bar scrim from resources. Providing null will disable
* the scrim functionality.
* <p>
* <p>This scrim is only shown when we have been given a top system inset.
*
* @param drawable the drawable to display
* @attr ref R.styleable#SubtitleCollapsingToolbarLayout_statusBarScrim
* @see #getStatusBarScrim()
*/
public void setStatusBarScrim(@Nullable Drawable drawable) {
if (statusBarScrim != drawable) {
if (statusBarScrim != null) {
statusBarScrim.setCallback(null);
}
statusBarScrim = drawable != null ? drawable.mutate() : null;
if (statusBarScrim != null) {
if (statusBarScrim.isStateful()) {
statusBarScrim.setState(getDrawableState());
}
DrawableCompat.setLayoutDirection(statusBarScrim, ViewCompat.getLayoutDirection(this));
statusBarScrim.setVisible(getVisibility() == VISIBLE, false);
statusBarScrim.setCallback(this);
statusBarScrim.setAlpha(scrimAlpha);
}
ViewCompat.postInvalidateOnAnimation(this);
}
}
示例11
public SwipeToDeleteCallback(ListMessageAdapter adapter) {
super(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
this.adapter = adapter;
int backgroundColorId =
ContextCompat.getColor(MessagesActivity.this, R.color.swipeBackground);
int iconColorId = ContextCompat.getColor(MessagesActivity.this, R.color.swipeIcon);
Drawable drawable =
ContextCompat.getDrawable(MessagesActivity.this, R.drawable.ic_delete);
icon = null;
if (drawable != null) {
icon = DrawableCompat.wrap(drawable.mutate());
DrawableCompat.setTint(icon, iconColorId);
}
background = new ColorDrawable(backgroundColorId);
}
示例12
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_server_list, menu);
MenuItem qrCodeItem = menu.findItem(R.id.action_scan_qr_code).setVisible(!mBatchOperationMode);
menu.findItem(R.id.action_import_from_file).setVisible(!mBatchOperationMode);
menu.findItem(R.id.action_export_to_file).setVisible(!mBatchOperationMode);
menu.findItem(R.id.action_enter_batch_mode).setVisible(!mBatchOperationMode);
menu.findItem(R.id.action_exit_batch_operation).setVisible(mBatchOperationMode);
menu.findItem(R.id.action_select_all_servers).setVisible(mBatchOperationMode);
menu.findItem(R.id.action_deselect_all_servers).setVisible(mBatchOperationMode);
menu.findItem(R.id.action_batch_delete_servers).setVisible(mBatchOperationMode);
// Tint scan QRCode icon to white.
if (qrCodeItem.getIcon() != null) {
Drawable drawable = qrCodeItem.getIcon();
Drawable wrapper = DrawableCompat.wrap(drawable);
drawable.mutate();
DrawableCompat.setTint(wrapper, ContextCompat.getColor(mContext, android.R.color.white));
qrCodeItem.setIcon(drawable);
}
}
示例13
private void initializeView() {
photoDrawable = ContextCompat.getDrawable(context, R.drawable.ic_photo_camera_white_24dp);
photoDrawable = DrawableCompat.wrap(photoDrawable);
DrawableCompat.setTintList(photoDrawable.mutate(), ContextCompat.getColorStateList(context, R.drawable.switch_camera_mode_selector));
videoDrawable = ContextCompat.getDrawable(context, R.drawable.ic_videocam_white_24dp);
videoDrawable = DrawableCompat.wrap(videoDrawable);
DrawableCompat.setTintList(videoDrawable.mutate(), ContextCompat.getColorStateList(context, R.drawable.switch_camera_mode_selector));
setBackgroundResource(R.drawable.circle_frame_background_dark);
// setBackgroundResource(R.drawable.circle_frame_background);
setOnClickListener(new MediaActionClickListener());
setIcons();
padding = Utils.convertDpToPixel(padding);
setPadding(padding, padding, padding, padding);
}
示例14
public void bind(TetheringDialog.DialogListAdapter.ViewModel model) {
this.setEnabled(model.enabled);
textView.setText(model.text);
textView.setEnabled(model.enabled);
Drawable checkIcon = DrawableCompat.wrap(getResources().getDrawable(R.drawable.ic_check_bold)).mutate();
if (model.enabled) {
DrawableCompat.setTint(checkIcon, ContextCompat.getColor(getContext(), R.color.colorSuccess));
} else {
DrawableCompat.setTint(checkIcon, ContextCompat.getColor(getContext(), R.color.colorDisabled));
}
iconView.setImageDrawable(model.image);
checkedIcon.setImageDrawable(checkIcon);
setChecked(model.checked);
}
示例15
/**
* Initialize the options menu.
* Be sure to call {@link #setHasOptionsMenu} before.
*
* @param menu The container for the custom options menu.
* @param menuInflater The inflater to instantiate the layout.
*/
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.options_menu_playlist_tracks_fragment, menu);
// get tint color
int tintColor = ThemeUtils.getThemeColor(getContext(), R.attr.odyssey_color_text_accent);
Drawable drawable = menu.findItem(R.id.action_add_playlist_tracks).getIcon();
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, tintColor);
menu.findItem(R.id.action_add_playlist_tracks).setIcon(drawable);
super.onCreateOptionsMenu(menu, menuInflater);
}
示例16
/**
* Set the color of the scroll handle.
*
* @param color The color for the scroll handle
*/
public void setHandleColor(@ColorInt int color) {
handleColor = color;
if (handleImage == null) {
Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.fastscroll_handle);
if (drawable != null) {
handleImage = DrawableCompat.wrap(drawable);
handleImage.mutate();
}
}
DrawableCompat.setTint(handleImage, handleColor);
handleView.setImageDrawable(handleImage);
}
示例17
@Override
public void setIcon(@Nullable Drawable icon) {
if (icon != null) {
if (hasIconTintList) {
Drawable.ConstantState state = icon.getConstantState();
icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
DrawableCompat.setTintList(icon, iconTintList);
}
icon.setBounds(0, 0, iconSize, iconSize);
} else if (needsEmptyIcon) {
if (emptyDrawable == null) {
emptyDrawable =
ResourcesCompat.getDrawable(
getResources(), R.drawable.navigation_empty_icon, getContext().getTheme());
if (emptyDrawable != null) {
emptyDrawable.setBounds(0, 0, iconSize, iconSize);
}
}
icon = emptyDrawable;
}
TextViewCompat.setCompoundDrawablesRelative(textView, icon, null, null, null);
}
示例18
/**
* Initialize the options menu.
* Be sure to call {@link #setHasOptionsMenu} before.
*
* @param menu The container for the custom options menu.
* @param menuInflater The inflater to instantiate the layout.
*/
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.options_menu_artist_albums_fragment, menu);
// get tint color
int tintColor = ThemeUtils.getThemeColor(getContext(), R.attr.odyssey_color_text_accent);
Drawable drawable = menu.findItem(R.id.action_add_artist_albums).getIcon();
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, tintColor);
menu.findItem(R.id.action_add_artist_albums).setIcon(drawable);
super.onCreateOptionsMenu(menu, menuInflater);
}
示例19
/**
* Set the drawable to use for the status bar foreground drawable. Providing null will disable the
* scrim functionality.
*
* <p>This scrim is only shown when we have been given a top system inset.
*
* @param drawable the drawable to display
* @attr ref R.styleable#AppBarLayout_statusBarForeground
* @see #getStatusBarForeground()
*/
public void setStatusBarForeground(@Nullable Drawable drawable) {
if (statusBarForeground != drawable) {
if (statusBarForeground != null) {
statusBarForeground.setCallback(null);
}
statusBarForeground = drawable != null ? drawable.mutate() : null;
if (statusBarForeground != null) {
if (statusBarForeground.isStateful()) {
statusBarForeground.setState(getDrawableState());
}
DrawableCompat.setLayoutDirection(statusBarForeground, ViewCompat.getLayoutDirection(this));
statusBarForeground.setVisible(getVisibility() == VISIBLE, false);
statusBarForeground.setCallback(this);
}
updateWillNotDraw();
ViewCompat.postInvalidateOnAnimation(this);
}
}
示例20
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
if (Daedalus.isDarkTheme()) {
setTheme(R.style.AppTheme_Dark_NoActionBar);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_filter);
Toolbar toolbar = findViewById(R.id.toolbar_filter);
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_clear);
RecyclerView recyclerView = findViewById(R.id.recyclerView_app_filter_list);
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
Drawable wrappedDrawable = DrawableCompat.wrap(Objects.requireNonNull(drawable));
DrawableCompat.setTint(wrappedDrawable, Color.WHITE);
toolbar.setNavigationIcon(drawable);
toolbar.setNavigationOnClickListener(v -> onBackPressed());
toolbar.setTitle(R.string.settings_app_filter);
adapter = new RecyclerViewAdapter();
recyclerView.setAdapter(adapter);
new Thread(() -> adapter.updateList(getAppList())).start();
}
示例21
public static void applyBrandToLayerDrawable(@NonNull LayerDrawable check, @IdRes int areaToColor, @ColorInt int mainColor) {
final Drawable drawable = check.findDrawableByLayerId(areaToColor);
if (drawable == null) {
Log.e(TAG, "Could not find areaToColor (" + areaToColor + "). Cannot apply brand.");
} else {
DrawableCompat.setTint(drawable, mainColor);
}
}
示例22
/**
* Adapts the tint of the preference's icon.
*/
private void adaptIconTint() {
Drawable icon = getIcon();
if (icon != null) {
DrawableCompat.setTintList(icon, tintList);
DrawableCompat.setTintMode(icon, tintMode);
}
}
示例23
/**
* Sets the checked icon's color tint using the specified {@link
* android.content.res.ColorStateList}.
*
* @param checkedIconTint ColorStateList to tint the checked icon.
* @attr ref com.google.android.material.R.styleable#Chip_checkedIconTint
*/
public void setCheckedIconTint(@Nullable ColorStateList checkedIconTint) {
if (this.checkedIconTint != checkedIconTint) {
this.checkedIconTint = checkedIconTint;
if (canShowCheckedIcon()) {
DrawableCompat.setTintList(checkedIcon, checkedIconTint);
}
onStateChange(getState());
}
}
示例24
/**
* Initialize the options menu.
* Be sure to call {@link #setHasOptionsMenu} before.
*
* @param menu The container for the custom options menu.
* @param menuInflater The inflater to instantiate the layout.
*/
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.options_menu_my_music, menu);
mOptionMenu = menu;
// get tint color
int tintColor = ThemeUtils.getThemeColor(getContext(), R.attr.odyssey_color_text_accent);
Drawable drawable = menu.findItem(R.id.action_search).getIcon();
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, tintColor);
mOptionMenu.findItem(R.id.action_search).setIcon(drawable);
mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
// Check if a search string is saved from before
if (mSearchString != null) {
// Expand the view
mSearchView.setIconified(false);
menu.findItem(R.id.action_search).expandActionView();
// Set the query string
mSearchView.setQuery(mSearchString, false);
OdysseyFragment fragment = mMyMusicPagerAdapter.getRegisteredFragment(mMyMusicViewPager.getCurrentItem());
// Notify the adapter
fragment.applyFilter(mSearchString);
}
mSearchView.setOnQueryTextListener(new SearchTextObserver());
// show recents options only for the albums fragment
menu.findItem(R.id.action_show_recent_albums).setVisible(mMyMusicViewPager.getCurrentItem() == 1);
super.onCreateOptionsMenu(menu, menuInflater);
}
示例25
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
// This demonstrates how to programmatically tint a drawable
MenuItem item = menu.findItem(R.id.action_more);
Drawable drawableWrap = DrawableCompat.wrap(item.getIcon()).mutate();
DrawableCompat.setTint(drawableWrap, ColorUtils.getThemeColor(this, R.attr.colorOnPrimary));
item.setIcon(drawableWrap);
return true;
}
示例26
@Synthetic
void onVoted(Boolean successful) {
if (successful == null) {
Toast.makeText(this, R.string.vote_failed, Toast.LENGTH_SHORT).show();
} else if (successful) {
Drawable drawable = DrawableCompat.wrap(mVoteButton.getDrawable());
DrawableCompat.setTint(drawable, ContextCompat.getColor(this, R.color.greenA700));
Toast.makeText(this, R.string.voted, Toast.LENGTH_SHORT).show();
} else {
AppUtils.showLogin(this, mAlertDialogBuilder);
}
}
示例27
public void bind(ServerListAdapter adapter, ServerConfigData data) {
mConfigData = data;
Drawable d = DrawableCompat.wrap(mIconBg.getBackground());
DrawableCompat.setTint(d, adapter.mColorInactive);
mIconBg.setBackgroundDrawable(d);
mName.setText(data.name);
}
示例28
@Override
protected Drawable getTopFabDrawable() {
Drawable drawable = DrawableCompat.wrap(
ContextCompat.getDrawable(getActivity(), R.drawable.ic_add));
DrawableCompat.setTint(drawable, Color.WHITE);
return drawable;
}
示例29
@SuppressWarnings("deprecation")
public BubbleView(Context context, Paint paint) {
super(context);
setImageResource(R.drawable.v_slider_bubble);
mDrawable = DrawableCompat.wrap(getDrawable());
setImageDrawable(mDrawable);
mTextPaint = paint;
}
示例30
private void initializeView() {
frontCameraDrawable = ContextCompat.getDrawable(context, R.drawable.ic_camera_front_white_24dp);
frontCameraDrawable = DrawableCompat.wrap(frontCameraDrawable);
DrawableCompat.setTintList(frontCameraDrawable.mutate(), ContextCompat.getColorStateList(context, R.drawable.switch_camera_mode_selector));
rearCameraDrawable = ContextCompat.getDrawable(context, R.drawable.ic_camera_rear_white_24dp);
rearCameraDrawable = DrawableCompat.wrap(rearCameraDrawable);
DrawableCompat.setTintList(rearCameraDrawable.mutate(), ContextCompat.getColorStateList(context, R.drawable.switch_camera_mode_selector));
setBackgroundResource(R.drawable.circle_frame_background_dark);
setOnClickListener(new CameraTypeClickListener());
setIcons();
padding = Utils.convertDpToPixel(padding);
setPadding(padding, padding, padding, padding);
}