android.graphics.drawable.Drawable.getConstantState()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.9k)|赞(0)|评价(0)|浏览(265)

本文整理了Java中android.graphics.drawable.Drawable.getConstantState()方法的一些代码示例,展示了Drawable.getConstantState()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Drawable.getConstantState()方法的具体详情如下:
包路径:android.graphics.drawable.Drawable
类名称:Drawable
方法名:getConstantState

Drawable.getConstantState介绍

暂无

代码示例

代码示例来源:origin: ZieIony/Carbon

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;
}

代码示例来源:origin: bumptech/glide

@SuppressWarnings("WeakerAccess")
public FixedSizeDrawable(Drawable wrapped, int width, int height) {
 this(new State(wrapped.getConstantState(), width, height), wrapped);
}

代码示例来源:origin: bumptech/glide

@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();
}

代码示例来源:origin: aa112901/remusic

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;
}

代码示例来源:origin: ZieIony/Carbon

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));
  }
}

代码示例来源:origin: ZieIony/Carbon

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));
  }
}

代码示例来源:origin: tyzlmjj/PagerBottomTabStrip

public static Drawable newDrawable(Drawable drawable) {
  Drawable.ConstantState constantState = drawable.getConstantState();
  return constantState != null ? constantState.newDrawable() : drawable;
}

代码示例来源:origin: square/assertj-android

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;
}

代码示例来源:origin: DreaminginCodeZH/MaterialProgressBar

/**
   * 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;
  }
}

代码示例来源:origin: JakeWharton/butterknife

@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());
 }
}

代码示例来源:origin: ankidroid/Anki-Android

@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;
}

代码示例来源:origin: prolificinteractive/material-calendarview

/**
 * @param drawable custom selection drawable
 */
public void setSelectionDrawable(Drawable drawable) {
 if (drawable == null) {
  this.selectionDrawable = null;
 } else {
  this.selectionDrawable = drawable.getConstantState().newDrawable(getResources());
 }
 regenerateBackground();
}

代码示例来源:origin: prolificinteractive/material-calendarview

/**
 * @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();
}

代码示例来源:origin: aa112901/remusic

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;
}

代码示例来源:origin: zhihu/Matisse

Drawable drawable = drawables[i];
if (drawable != null) {
  final Drawable.ConstantState state = drawable.getConstantState();
  if (state == null) {
    continue;

代码示例来源:origin: facebook/litho

@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;
}

代码示例来源:origin: aurelhubert/ahbottomnavigation

Drawable drawable = notificationBackgroundDrawable.getConstantState().newDrawable();
  notification.setBackground(drawable);
} else {

代码示例来源:origin: facebook/litho

@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());
}

代码示例来源:origin: ZieIony/Carbon

final Drawable clone;
if (dr != null) {
  final ConstantState cs = dr.getConstantState();
  if (res != null) {
    clone = cs.newDrawable(res);

代码示例来源:origin: facebook/litho

if (!equals(previousBackground.getConstantState(), nextBackground.getConstantState())) {
 return true;

相关文章