android - 是否有任何理由从资源中预加载可绘制对象?

标签 android caching drawable

Android 是否维护应用程序可绘制资源的内存缓存并重用它们,或者预加载可能动态分配给不同小部件的所有可绘制资源是一种好的做法吗?

例如:

public static final int[] SETS = {
        R.drawable.set0, R.drawable.set1, R.drawable.set2,
        R.drawable.set3, R.drawable.set4, R.drawable.set5, R.drawable.set6,
        R.drawable.set7, R.drawable.set8, R.drawable.set9, R.drawable.set10};
public Drawable[] sets;

void init() {
    load(sets, SETS);
}

public void load(Drawable[] d, int[] ids) {
    for (int i = 0; i < ids.length; i++) {
        if (ids[i] == 0)
            d[i] = null;
        else
            d[i] = context.getResources().getDrawable(ids[i]);
    }
}

最佳答案

这听起来像是不必要的预优化。但是,android 会缓存可绘制对象,因此您不必预加载它们。相关代码来自ApplicationContext

  /*package*/ Drawable loadDrawable(TypedValue value, int id)
            throws NotFoundException {
        .
        .
        .

        final long key = (((long) value.assetCookie) << 32) | value.data;
        Drawable dr = getCachedDrawable(key);

        if (dr != null) {
            return dr;
        }

        .
        .
        .

        if (dr != null) {
            dr.setChangingConfigurations(value.changingConfigurations);
            cs = dr.getConstantState();
            if (cs != null) {
                if (mPreloading) {
                    sPreloadedDrawables.put(key, cs);
                } else {
                    synchronized (mTmpValue) {
                        //Log.i(TAG, "Saving cached drawable @ #" +
                        //        Integer.toHexString(key.intValue())
                        //        + " in " + this + ": " + cs);
                        mDrawableCache.put(key, new WeakReference<Drawable.ConstantState>(cs));
                    }
                }
            }
        }

        return dr;
    }

    private Drawable getCachedDrawable(long key) {
        synchronized (mTmpValue) {
            WeakReference<Drawable.ConstantState> wr = mDrawableCache.get(key);
            if (wr != null) {   // we have the key
                Drawable.ConstantState entry = wr.get();
                if (entry != null) {
                    //Log.i(TAG, "Returning cached drawable @ #" +
                    //        Integer.toHexString(((Integer)key).intValue())
                    //        + " in " + this + ": " + entry);
                    return entry.newDrawable(this);
                }
                else {  // our entry has been purged
                    mDrawableCache.delete(key);
                }
            }
        }
        return null;
    }

关于android - 是否有任何理由从资源中预加载可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135216/

相关文章:

android - 暂停后 MediaPlayer 完成

android - 加载图像时缓存包装器

java - Android 如何在首选项之间放置水平条

Spring 3.1 @Cacheable - 方法仍然执行

php - Magento 阻止 Varnish 缓存页面

XXHDPI 的 Android 背景图像大小

android - 如何使多个 url 链接显示在数组中?

android - 在 drawable 中绘制工具提示

android - 将自定义对象从一个 Activity 传递到另一个 Parcelable vs Bundle

c# - .NET Core 2.2 Razor Pages 网站模板以及如何缓存 MSFT Graph 调用