public final boolean canConstantState() { final ChildDrawable[] array = mChildren; final int N = mNum; for (int i = 0; i < N; i++) { final Drawable dr = array[i].mDrawable; if (dr != null && dr.getConstantState() == null) { return false; } } // Don't cache the result, this method is not called very often. return true; }
@SuppressWarnings("WeakerAccess") public FixedSizeDrawable(Drawable wrapped, int width, int height) { this(new State(wrapped.getConstantState(), width, height), wrapped); }
@NonNull @SuppressWarnings("unchecked") @Override public final T get() { @Nullable ConstantState state = drawable.getConstantState(); if (state == null) { return drawable; } // Drawables contain temporary state related to how they're being displayed // (alpha, color filter etc), so return a new copy each time. // If we ever return the original drawable, it's temporary state may be changed // and subsequent copies may end up with that temporary state. See #276. return (T) state.newDrawable(); }
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; }
private void cacheDrawable(TypedValue value, Resources.Theme theme, boolean isColorDrawable, long key, Drawable drawable, LongSparseArray<WeakReference<Drawable.ConstantState>> caches) { Drawable.ConstantState cs = drawable.getConstantState(); if (cs == null) { return; } synchronized (mAccessLock) { caches.put(key, new WeakReference<>(cs)); } }
private static void cacheDrawable(TypedValue value, Resources resources, Resources.Theme theme, boolean isColorDrawable, long key, Drawable drawable, LongSparseArray<WeakReference<Drawable.ConstantState>> caches) { Drawable.ConstantState cs = drawable.getConstantState(); if (cs == null) { return; } synchronized (mAccessLock) { caches.put(key, new WeakReference<>(cs)); } }
public static Drawable newDrawable(Drawable drawable) { Drawable.ConstantState constantState = drawable.getConstantState(); return constantState != null ? constantState.newDrawable() : drawable; }
public S hasConstantState(Drawable.ConstantState state) { isNotNull(); Drawable.ConstantState actualState = actual.getConstantState(); assertThat(actualState) // .overridingErrorMessage("Expected constant state <%s> but was <%s>.", state, actualState) // .isEqualTo(state); return myself; }
/** * If all child drawables have a constant state */ public synchronized boolean canConstantState() { if (mCheckedConstantState) { return mCanConstantState; } createAllFutures(); mCheckedConstantState = true; final int count = mNumChildren; final Drawable[] drawables = mDrawables; for (int i = 0; i < count; i++) { if (drawables[i].getConstantState() == null) { mCanConstantState = false; return false; } } mCanConstantState = true; return true; } }
@Test public void asDrawable() { Target target = new Target(); Drawable expected = context.getResources().getDrawable(R.drawable.circle); Unbinder unbinder = new BindDrawableTest$Target_ViewBinding(target, context); assertThat(target.actual.getConstantState()).isEqualTo(expected.getConstantState()); unbinder.unbind(); assertThat(target.actual.getConstantState()).isEqualTo(expected.getConstantState()); } }
@SuppressWarnings("deprecation") @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { super.onPreferenceTreeClick(preferenceScreen, preference); if (preference instanceof PreferenceScreen && ((PreferenceScreen) preference).getDialog() != null) { ((PreferenceScreen) preference).getDialog().getWindow().getDecorView().setBackgroundDrawable( this.getWindow().getDecorView().getBackground().getConstantState().newDrawable()); } return false; }
/** * @param drawable custom selection drawable */ public void setSelectionDrawable(Drawable drawable) { if (drawable == null) { this.selectionDrawable = null; } else { this.selectionDrawable = drawable.getConstantState().newDrawable(getResources()); } regenerateBackground(); }
/** * @param drawable background to draw behind everything else */ public void setCustomBackground(Drawable drawable) { if (drawable == null) { this.customBackground = null; } else { this.customBackground = drawable.getConstantState().newDrawable(getResources()); } invalidate(); }
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 get 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; }
Drawable drawable = drawables[i]; if (drawable != null) { final Drawable.ConstantState state = drawable.getConstantState(); if (state == null) { continue;
@Nullable public Drawable get(int resId, Resources resources, @Nullable Resources.Theme theme) { SimplePoolWithCount<Drawable> drawablesPool = mDrawableCache.get(resId); if (drawablesPool == null) { drawablesPool = new SimplePoolWithCount<>(DRAWABLES_POOL_MAX_ITEMS); mDrawableCache.put(resId, drawablesPool); } Drawable drawable = drawablesPool.acquire(); if (drawable == null) { drawable = ResourcesCompat.getDrawable(resources, resId, theme); } // We never want this pool to remain empty otherwise we would risk to resolve a new drawable // when get is called again. So if the pool is about to drain we just put a new Drawable in it // to keep it warm. if (drawable != null && drawablesPool.getPoolSize() == 0) { drawablesPool.release(drawable.getConstantState().newDrawable()); } return drawable; }
Drawable drawable = notificationBackgroundDrawable.getConstantState().newDrawable(); notification.setBackground(drawable); } else {
@Test public void testPoolIsNeverEmpty() { Resources resources = application.getResources(); // This being null is ok since we are only using this drawable to test the cache. // We still need to declare the variable though otherewise the call to the constructor would be // ambiguous. Bitmap bitmap = null; BitmapDrawable drawable = new BitmapDrawable(resources, bitmap); mCache.release(drawable, 1); mCache.release(new ColorDrawable(), 2); Drawable first = mCache.get(1, resources, null); Drawable second = mCache.get(1, resources, null); Drawable third = mCache.get(2, resources, null); assertThat(first).isNotNull(); assertThat(second).isNotNull(); assertThat(third).isNotNull(); assertThat(second.getConstantState()).isEqualTo(first.getConstantState()); assertNotEquals(first.getConstantState(), third.getConstantState()); }
final Drawable clone; if (dr != null) { final ConstantState cs = dr.getConstantState(); if (res != null) { clone = cs.newDrawable(res);
if (!equals(previousBackground.getConstantState(), nextBackground.getConstantState())) { return true;