本文整理匯總了Java中android.graphics.drawable.Drawable.getConstantState方法的典型用法代碼示例。如果您正苦於以下問題:Java Drawable.getConstantState方法的具體用法?Java Drawable.getConstantState怎麽用?Java Drawable.getConstantState使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.graphics.drawable.Drawable
的用法示例。
在下文中一共展示了Drawable.getConstantState方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: addCachedDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private boolean addCachedDrawable(final int key, @NonNull final Drawable drawable) {
if (drawable instanceof FilterableStateListDrawable) {
return false;
}
final Drawable.ConstantState cs = drawable.getConstantState();
if (cs != null) {
synchronized (mDrawableCacheLock) {
if (mCacheDrawables == null) {
mCacheDrawables = new SparseArray<>();
}
mCacheDrawables.put(key, new WeakReference<>(cs));
}
return true;
}
return false;
}
示例2: containsNinePatch
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
public static boolean containsNinePatch(Drawable drawable) {
drawable = getWrapperDrawable(drawable);
if (drawable instanceof NinePatchDrawable
|| drawable instanceof InsetDrawable
|| drawable instanceof LayerDrawable) {
return true;
} else if (drawable instanceof StateListDrawable) {
final DrawableContainer.DrawableContainerState containerState = ((DrawableContainer.DrawableContainerState) drawable.getConstantState());
//can't getBaseApplication containState from drawable which is containing DrawableWrapperDonut
//https://code.google.com/p/android/issues/detail?id=169920
if (containerState == null) {
return true;
}
for (Drawable dr : containerState.getChildren()) {
dr = getWrapperDrawable(dr);
if (dr instanceof NinePatchDrawable
|| dr instanceof InsetDrawable
|| dr instanceof LayerDrawable) {
return true;
}
}
}
return false;
}
示例3: canSafelyMutateDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Some drawable implementations have problems with mutation. This method returns false if
* there is a known issue in the given drawable's implementation.
*/
public static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
if (Build.VERSION.SDK_INT < 15 && drawable instanceof InsetDrawable) {
return false;
} else if (Build.VERSION.SDK_INT < 15 && drawable instanceof GradientDrawable) {
// GradientDrawable has a bug pre-ICS which results in mutate() resulting
// in loss of color
return false;
} else if (Build.VERSION.SDK_INT < 17 && drawable instanceof LayerDrawable) {
return false;
}
if (drawable instanceof DrawableContainer) {
// If we have a DrawableContainer, let's traverse it's child array
final Drawable.ConstantState state = drawable.getConstantState();
if (state instanceof DrawableContainer.DrawableContainerState) {
final DrawableContainer.DrawableContainerState containerState =
(DrawableContainer.DrawableContainerState) state;
for (final Drawable child : containerState.getChildren()) {
if (!canSafelyMutateDrawable(child)) {
return false;
}
}
}
} else if (drawable instanceof android.support.v4.graphics.drawable.DrawableWrapper) {
return canSafelyMutateDrawable(
((android.support.v4.graphics.drawable.DrawableWrapper) drawable)
.getWrappedDrawable());
} else if (drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
return canSafelyMutateDrawable(
((android.support.v7.graphics.drawable.DrawableWrapper) drawable)
.getWrappedDrawable());
} else if (drawable instanceof ScaleDrawable) {
return canSafelyMutateDrawable(((ScaleDrawable) drawable).getDrawable());
}
return true;
}
示例4: cloneDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Clones the specified drawable.
* @param drawable the drawable to clone.
* @return a clone of the drawable or null if the drawable cannot be cloned.
*/
public static @Nullable Drawable cloneDrawable(Drawable drawable) {
if (drawable instanceof CloneableDrawable) {
return ((CloneableDrawable) drawable).cloneDrawable();
}
Drawable.ConstantState constantState = drawable.getConstantState();
return constantState != null ? constantState.newDrawable() : null;
}
示例5: setFilledDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
public void setFilledDrawable(Drawable drawable) {
if (drawable.getConstantState() == null) {
return;
}
ClipDrawable clipDrawable = new ClipDrawable(drawable.getConstantState().newDrawable(), Gravity.START, ClipDrawable.HORIZONTAL);
mFilledView.setImageDrawable(clipDrawable);
}
示例6: setEmptyDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
public void setEmptyDrawable(Drawable drawable) {
if (drawable.getConstantState() == null) {
return;
}
ClipDrawable clipDrawable = new ClipDrawable(drawable.getConstantState().newDrawable(), Gravity.END, ClipDrawable.HORIZONTAL);
mEmptyView.setImageDrawable(clipDrawable);
}
示例7: setUnderlineColor
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@ReactProp(name = "underlineColorAndroid", customType = "Color")
public void setUnderlineColor(ReactEditText view, @Nullable Integer underlineColor) {
// Drawable.mutate() can sometimes crash due to an AOSP bug:
// See https://code.google.com/p/android/issues/detail?id=191754 for more info
Drawable background = view.getBackground();
Drawable drawableToMutate = background.getConstantState() != null ?
background.mutate() :
background;
if (underlineColor == null) {
drawableToMutate.clearColorFilter();
} else {
drawableToMutate.setColorFilter(underlineColor, PorterDuff.Mode.SRC_IN);
}
}
示例8: icon
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Drawable icon(Drawable drawable) {
if (drawable != null) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
@SuppressWarnings("SuspiciousNameCombination")
Drawable resizeIcon = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, topIcon, topIcon, true));
Drawable.ConstantState state = resizeIcon.getConstantState();
resizeIcon = DrawableCompat.wrap(state == null ? resizeIcon : state.newDrawable().mutate());
return resizeIcon;
}
return null;
}
示例9: onBindViewHolder
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Override
protected void onBindViewHolder(final RecyclerView.ViewHolder holder, Cursor cursor) {
if (holder instanceof CaptureViewHolder) {
CaptureViewHolder captureViewHolder = (CaptureViewHolder) holder;
Drawable[] drawables = captureViewHolder.mHint.getCompoundDrawables();
TypedArray ta = holder.itemView.getContext().getTheme().obtainStyledAttributes(
new int[]{R.attr.capture_textColor});
int color = ta.getColor(0, 0);
ta.recycle();
for (int i = 0; i < drawables.length; i++) {
Drawable drawable = drawables[i];
if (drawable != null) {
final Drawable.ConstantState state = drawable.getConstantState();
if (state == null) {
continue;
}
Drawable newDrawable = state.newDrawable().mutate();
newDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
newDrawable.setBounds(drawable.getBounds());
drawables[i] = newDrawable;
}
}
captureViewHolder.mHint.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
} else if (holder instanceof MediaViewHolder) {
MediaViewHolder mediaViewHolder = (MediaViewHolder) holder;
final Item item = Item.valueOf(cursor);
mediaViewHolder.mMediaGrid.preBindMedia(new MediaGrid.PreBindInfo(
getImageResize(mediaViewHolder.mMediaGrid.getContext()),
mPlaceholder,
mSelectionSpec.countable,
holder
));
mediaViewHolder.mMediaGrid.bindMedia(item);
mediaViewHolder.mMediaGrid.setOnMediaGridClickListener(this);
setCheckStatus(item, mediaViewHolder.mMediaGrid);
}
}
示例10: setIcon
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Override
public void setIcon(Drawable icon) {
if (icon != null) {
Drawable.ConstantState state = icon.getConstantState();
icon = DrawableCompat.wrap(state == null ? icon : state.newDrawable()).mutate();
DrawableCompat.setTintList(icon, mIconTint);
}
mIcon.setImageDrawable(icon);
}
示例11: colorUnknownDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Tries to clone and simply color-filter the drawable. Uses {@link PorterDuff.Mode#SRC_ATOP}.
* <b>Note</b>: Use this when you don't know which drawable you have.
*
* @param drawable Which drawable to color
* @param color Which color to use
* @return A colored drawable ready for use
*/
@NonNull
public static Drawable colorUnknownDrawable(@NonNull final Drawable drawable, @ColorInt final int color) {
// check if this is a drawable wrapper, then do coloring by drawable wrapping
if (drawable instanceof DrawableWrapper || drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
final Drawable wrapResult = colorDrawableWrapped(drawable, color);
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2) {
// there is a bug for JellyBean MR2 when this won't work, so.. set the tint filter manually
wrapResult.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP));
}
return wrapResult;
}
// wrapping failed, do a plain constant state clone
try {
final Drawable.ConstantState state = drawable.getConstantState();
if (state == null) {
// well done android.
throw new IllegalStateException("Constant state is unavailable");
}
final Drawable copy = drawable.getConstantState().newDrawable().mutate();
copy.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
return copy;
} catch (Exception ignored) {
return drawable;
}
}
示例12: testCreateResponsiveDrawableBorders
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Tests the {@link Coloring#createResponsiveDrawable(Context, int, int, int, boolean, int, Rect)} method.
* <p>
* Unfortunately some Drawable properties are not shadowed by Robolectric yet, so we can test only the basic stuff here.
*/
@Test
public final void testCreateResponsiveDrawableBorders() {
final Drawable drawable = Coloring.createResponsiveDrawable(mActivityContext, Color.WHITE, Color.GRAY, Color.GREEN, true, 20, new Rect(0, 0, 20, 20));
assertNotNull("Responsive drawable is null", drawable);
assertTrue("Responsive drawable is of unknown type: " + drawable.getClass().getCanonicalName(), drawable instanceof StateListDrawable || drawable
instanceof RippleDrawable);
final Drawable.ConstantState constantState = drawable.getConstantState();
assertNotNull("Constant state is null", constantState);
}
示例13: convertDrawableToGrayScale
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
public static Drawable convertDrawableToGrayScale(Drawable drawable, int color) {
if (drawable == null || drawable.getConstantState() == null)
return null;
Drawable res = drawable.getConstantState().newDrawable().mutate();
res.setColorFilter(color, PorterDuff.Mode.SRC_IN);
return res;
}
示例14: setIcon
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
public void setIcon(Drawable icon) {
if (icon != null) {
ConstantState state = icon.getConstantState();
if (state != null) {
icon = state.newDrawable();
}
icon = DrawableCompat.wrap(icon).mutate();
icon.setBounds(0, 0, this.mIconSize, this.mIconSize);
DrawableCompat.setTintList(icon, this.mIconTintList);
}
TextViewCompat.setCompoundDrawablesRelative(this.mTextView, icon, null, null, null);
}
示例15: canSafelyMutateDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
static boolean canSafelyMutateDrawable(@NonNull Drawable drawable) {
if (drawable instanceof LayerDrawable) {
if (VERSION.SDK_INT >= 16) {
return true;
}
return false;
} else if (drawable instanceof InsetDrawable) {
if (VERSION.SDK_INT < 14) {
return false;
}
return true;
} else if (drawable instanceof StateListDrawable) {
if (VERSION.SDK_INT < 8) {
return false;
}
return true;
} else if (drawable instanceof GradientDrawable) {
if (VERSION.SDK_INT < 14) {
return false;
}
return true;
} else if (drawable instanceof DrawableContainer) {
ConstantState state = drawable.getConstantState();
if (!(state instanceof DrawableContainerState)) {
return true;
}
for (Drawable child : ((DrawableContainerState) state).getChildren()) {
if (!canSafelyMutateDrawable(child)) {
return false;
}
}
return true;
} else if (drawable instanceof DrawableWrapper) {
return canSafelyMutateDrawable(((DrawableWrapper) drawable).getWrappedDrawable());
} else {
if (drawable instanceof android.support.v7.graphics.drawable.DrawableWrapper) {
return canSafelyMutateDrawable(((android.support.v7.graphics.drawable.DrawableWrapper) drawable).getWrappedDrawable());
}
return true;
}
}