下面列出了怎么用androidx.appcompat.widget.AppCompatDrawableManager的API类实例代码及写法,或者点击链接到github查看源代码。
private void onApplySupportImageTint() {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (imageTint == null) {
DrawableCompat.clearColorFilter(drawable);
return;
}
int color = imageTint.getColorForState(getDrawableState(), Color.TRANSPARENT);
Mode mode = imageMode;
if (mode == null) {
mode = Mode.SRC_IN;
}
drawable
.mutate()
.setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(color, mode));
}
public static Drawable getDrawable(Context context, int res) {
try {
Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res);
return drawable.mutate();
} catch (OutOfMemoryError e) {
return null;
}
}
@SuppressLint("RestrictedApi")
public void initFab() {
menu = (FloatingActionMenu) findViewById(R.id.menu);
menu.setClosedOnTouchOutside(true);
AppCompatDrawableManager dm = AppCompatDrawableManager.get();
FloatingActionButton manualAddFAB = (FloatingActionButton) findViewById(R.id.fab_manual_add);
manualAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_content_create));
manualAddFAB.setOnClickListener(this);
final FloatingActionButton qrcodeAddFAB = (FloatingActionButton) findViewById(R.id.fab_qrcode_add);
qrcodeAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_image_camera_alt));
qrcodeAddFAB.setOnClickListener(this);
FloatingActionButton nfcAddFAB = (FloatingActionButton) findViewById(R.id.fab_nfc_add);
nfcAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_device_nfc));
nfcAddFAB.setOnClickListener(this);
FloatingActionButton importAddFAB = (FloatingActionButton) findViewById(R.id.fab_import_add);
importAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_content_paste));
importAddFAB.setOnClickListener(this);
FloatingActionButton ssrsubAddFAB = (FloatingActionButton) findViewById(R.id.fab_ssr_sub);
ssrsubAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_rss));
ssrsubAddFAB.setOnClickListener(this);
menu.setOnMenuToggleListener(new FloatingActionMenu.OnMenuToggleListener() {
@Override
public void onMenuToggle(boolean opened) {
if (opened) {
int visible = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA) ? View.VISIBLE : View.GONE;
qrcodeAddFAB.setVisibility(visible);
}
}
});
}
@SuppressLint("RestrictedApi")
public void initFab() {
menu = findViewById(R.id.menu);
menu.setClosedOnTouchOutside(true);
AppCompatDrawableManager dm = AppCompatDrawableManager.get();
FloatingActionButton manualAddFAB = findViewById(R.id.fab_manual_add);
manualAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_content_create));
manualAddFAB.setOnClickListener(this);
final FloatingActionButton qrcodeAddFAB = findViewById(R.id.fab_qrcode_add);
qrcodeAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_image_camera_alt));
qrcodeAddFAB.setOnClickListener(this);
FloatingActionButton nfcAddFAB = findViewById(R.id.fab_nfc_add);
nfcAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_device_nfc));
nfcAddFAB.setOnClickListener(this);
FloatingActionButton importAddFAB = findViewById(R.id.fab_import_add);
importAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_content_paste));
importAddFAB.setOnClickListener(this);
FloatingActionButton ssrsubAddFAB = findViewById(R.id.fab_ssr_sub);
ssrsubAddFAB.setImageDrawable(dm.getDrawable(this, R.drawable.ic_rss));
ssrsubAddFAB.setOnClickListener(this);
menu.setOnMenuToggleListener(opened -> {
if (opened) {
int visible = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA) ? View.VISIBLE : View.GONE;
qrcodeAddFAB.setVisibility(visible);
}
});
}
public static void tintDrawable(@NonNull Drawable drawable, @ColorInt int color, PorterDuff.Mode mode) {
if (DrawableUtils.canSafelyMutateDrawable(drawable)
&& drawable.mutate() != drawable) {
Log.e(TAG, "Mutated drawable is not the same instance as the input.");
return;
}
drawable.setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(color, mode == null ? PorterDuff.Mode.SRC_IN : mode));
}
public static Drawable tintDrawable(@NonNull Context context, @NonNull Drawable drawable, int colorAttr, int alpha, PorterDuff.Mode tintMode) {
if (DrawableUtils.canSafelyMutateDrawable(drawable)) {
drawable = drawable.mutate();
}
final int color = getThemeAttrColor(context, colorAttr);
drawable.setColorFilter(AppCompatDrawableManager.getPorterDuffColorFilter(color, tintMode));
if (alpha != -1) {
drawable.setAlpha(alpha);
}
return drawable;
}
private static PorterDuffColorFilter createTintFilter(ColorStateList tint, PorterDuff.Mode tintMode, final int[] state) {
if (tint == null || tintMode == null) {
return null;
}
final int color = tint.getColorForState(state, Color.TRANSPARENT);
return AppCompatDrawableManager.getPorterDuffColorFilter(color, tintMode);
}
void updateEditTextBackground() {
// Only update the color filter for the legacy text field, since we can directly change the
// Paint colors of the MaterialShapeDrawable box background without having to use color filters.
if (editText == null || boxBackgroundMode != BOX_BACKGROUND_NONE) {
return;
}
Drawable editTextBackground = editText.getBackground();
if (editTextBackground == null) {
return;
}
if (androidx.appcompat.widget.DrawableUtils.canSafelyMutateDrawable(editTextBackground)) {
editTextBackground = editTextBackground.mutate();
}
if (indicatorViewController.errorShouldBeShown()) {
// Set a color filter for the error color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
indicatorViewController.getErrorViewCurrentTextColor(), PorterDuff.Mode.SRC_IN));
} else if (counterOverflowed && counterView != null) {
// Set a color filter of the counter color
editTextBackground.setColorFilter(
AppCompatDrawableManager.getPorterDuffColorFilter(
counterView.getCurrentTextColor(), PorterDuff.Mode.SRC_IN));
} else {
// Else reset the color filter and refresh the drawable state so that the
// normal tint is used
DrawableCompat.clearColorFilter(editTextBackground);
editText.refreshDrawableState();
}
}
/**
* A utility method to retrieve a drawable that correctly abides by the
* theme in Lollipop (API 23) +
*
* @param resourceId The resource id for the drawable
* @return The drawable associated with {@code resourceId}
*/
@Nullable
@SuppressLint("RestrictedApi")
protected Drawable getDrawable(@DrawableRes int resourceId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return getResources().getDrawable(resourceId, getContext().getTheme());
}
return AppCompatDrawableManager.get().getDrawable(getContext(), resourceId);
}
/**
* Retrieves the specified image drawable in a manner that will correctly
* wrap VectorDrawables on platforms that don't natively support them
*
* @param typedArray The TypedArray containing the attributes for the view
* @param index The index in the {@code typedArray} for the drawable
*/
@Nullable
@SuppressLint("RestrictedApi")
protected Drawable getDrawable(@NonNull TypedArray typedArray, int index) {
int imageResId = typedArray.getResourceId(index, 0);
if (imageResId == 0) {
return null;
}
return AppCompatDrawableManager.get().getDrawable(getContext(), imageResId);
}