【问题标题】:Is there any reason to preloading drawables from resources?是否有任何理由从资源中预加载可绘制对象?
【发布时间】:2012-04-13 04:26:38
【问题描述】:

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 caching drawable


    【解决方案1】:

    这听起来像是不必要的预优化。但是,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;
        }
    

    【讨论】:

    • 有什么办法可以关闭缓存吗?我多次获得一个可绘制对象并更改其阴影。它们都以相同的色调结束。非常令人沮丧。
    猜你喜欢
    • 2011-06-20
    • 2013-06-09
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多