本文整理匯總了Java中android.graphics.drawable.Drawable.ConstantState方法的典型用法代碼示例。如果您正苦於以下問題:Java Drawable.ConstantState方法的具體用法?Java Drawable.ConstantState怎麽用?Java Drawable.ConstantState使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類android.graphics.drawable.Drawable
的用法示例。
在下文中一共展示了Drawable.ConstantState方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getActivityIconWithCache
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Gets the activity or application icon for an activity.
* Uses the local icon cache for fast repeated lookups.
*
* @param component Name of an activity.
* @return A drawable, or {@code null} if neither the activity nor the application
* has an icon set.
*/
private Drawable getActivityIconWithCache(ComponentName component) {
// First check the icon cache
String componentIconKey = component.flattenToShortString();
// Using containsKey() since we also store null values.
if (mOutsideDrawablesCache.containsKey(componentIconKey)) {
Drawable.ConstantState cached = mOutsideDrawablesCache.get(componentIconKey);
return cached == null ? null : cached.newDrawable(mProviderContext.getResources());
}
// Then try the activity or application icon
Drawable drawable = getActivityIcon(component);
// Stick it in the cache so we don't do this lookup again.
Drawable.ConstantState toCache = drawable == null ? null : drawable.getConstantState();
mOutsideDrawablesCache.put(componentIconKey, toCache);
return drawable;
}
示例2: testCreateMultiStateDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Tests the {@link Coloring#createMultiStateDrawable(Drawable, Drawable, Drawable, boolean)} 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 testCreateMultiStateDrawable() {
// noinspection deprecation - can't enforce Lollipop here
final BitmapDrawable normal = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_on);
assertNotNull("Normal drawable is null", normal);
// noinspection deprecation - can't enforce Lollipop here
final BitmapDrawable clicked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_off);
assertNotNull("Clicked drawable is null", clicked);
// noinspection deprecation - can't enforce Lollipop here
final BitmapDrawable checked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.star_off);
assertNotNull("Checked drawable is null", checked);
final StateListDrawable stateList = Coloring.createMultiStateDrawable(normal, clicked, checked, true);
assertNotNull("Contrast state drawable is null", stateList);
assertTrue("Contrast state drawable is not stateful", stateList.isStateful());
final Drawable.ConstantState constantState = stateList.getConstantState();
assertNotNull("Constant state is null", constantState);
}
示例3: testReturnsNewDrawableOnGet
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
@Test
public void testReturnsNewDrawableOnGet() {
GifDrawable expected = mock(GifDrawable.class);
Drawable.ConstantState constantState = mock(Drawable.ConstantState.class);
when(constantState.newDrawable()).thenReturn(expected);
when(drawable.getConstantState()).thenReturn(constantState);
assertEquals(expected, resource.get());
verify(drawable).getConstantState();
verify(constantState).newDrawable();
}
示例4: copyDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
public static Drawable copyDrawable(final ImageView imageView) {
Drawable result = null, drawable = imageView == null ? null : imageView.getDrawable();
if (drawable != null) {
Drawable.ConstantState constantState = drawable.mutate().getConstantState();
if (constantState != null) {
result = constantState.newDrawable();
}
}
return result;
}
示例5: 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 its 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;
}
示例6: checkIconCache
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
private Drawable checkIconCache(String resourceUri) {
Drawable.ConstantState cached = mOutsideDrawablesCache.get(resourceUri);
if (cached == null) {
return null;
}
if (DBG) Log.d(LOG_TAG, "Found icon in cache: " + resourceUri);
return cached.newDrawable();
}
示例7: 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, leftIcon, leftIcon, true));
Drawable.ConstantState state = resizeIcon.getConstantState();
resizeIcon = DrawableCompat.wrap(state == null ? resizeIcon : state.newDrawable().mutate());
return resizeIcon;
}
return null;
}
示例8: 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);
}
}
示例9: 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);
}
示例10: 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;
}
}
示例11: 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;
}
示例12: testCreateRippleDrawableSingle
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Tests the {@link Coloring#createRippleDrawable(int)} method.
* <p>
* Unfortunately {@link android.graphics.drawable.LayerDrawable}'s constructor is not shadowed by Robolectric yet.
*/
@Test
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public final void testCreateRippleDrawableSingle() {
try {
final RippleDrawable ripple = Coloring.createRippleDrawable(Color.GREEN);
assertNotNull("RippleDrawable is null", ripple);
final Drawable.ConstantState constantState = ripple.getConstantState();
assertNotNull("Constant state is null", constantState);
} catch (NullPointerException ignored) {
// Robolectric shadowing error, expected in the current version
}
}
示例13: testCreateResponsiveDrawableBorderless
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Tests the {@link Coloring#createResponsiveDrawable(Context, int, int, int, boolean, int)} 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 testCreateResponsiveDrawableBorderless() {
final Drawable drawable = Coloring.createResponsiveDrawable(mActivityContext, Color.WHITE, Color.GRAY, Color.GREEN, true, 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);
}
示例14: 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);
}
示例15: testCreateContrastStateDrawable
import android.graphics.drawable.Drawable; //導入方法依賴的package包/類
/**
* Tests the {@link Coloring#createContrastStateDrawable(Context, int, int, boolean, Drawable)} 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 testCreateContrastStateDrawable() {
// noinspection deprecation - can't enforce Lollipop here
final BitmapDrawable original = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_on);
assertNotNull("Original drawable is null", original);
final StateListDrawable stateList = Coloring.createContrastStateDrawable(mActivityContext, Color.WHITE, Color.BLACK, true, original);
assertNotNull("Contrast state drawable is null", stateList);
assertTrue("Contrast state drawable is not stateful", stateList.isStateful());
final Drawable.ConstantState constantState = stateList.getConstantState();
assertNotNull("Constant state is null", constantState);
}