本文介绍了有什么理由从资源preloading可绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难道Android的维护应用程序绘制资源,内存缓存和重新使用它们,或者它是一个很好的做法,preLOAD可动态地分配给不同的部件全部可绘制?

例如:

 公共静态最终诠释[]设置= {
        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};
市民可绘制[]套;

无效的init(){
    负载(套,套);
}

公共无效负载(可绘制[] D,INT [] IDS){
    的for(int i = 0; I< ids.length;我++){
        如果(IDS [我] == 0)
            D [I] =无效;
        其他
            。D [i] = context.getResources()getDrawable(IDS [I]);
    }
}
 

解决方案

这闻起来像一个不必要的pre-优化。然而,Android的做缓存可绘制,所以你不必pre-加载它们。从的ApplicationContext

相关code

  / *包* /可绘制loadDrawable(的TypedValue值,INT ID)
            抛出NotFoundException {
        。
        。
        。

        最终的长键=(((长)value.assetCookie)<< 32)| value.data;
        绘制对象博士= getCachedDrawable(密钥);

        如果(DR!= NULL){
            返回博士;
        }

        。
        。
        。

        如果(DR!= NULL){
            dr.setChangingConfigurations(value.changingConfigurations);
            CS = dr.getConstantState();
            如果(CS!= NULL){
                如果(M preloading){
                    US preloadedDrawables.put(键,CS);
                } 其他 {
                    同步(mTmpValue){
                        //Log.i(TAG,保存缓存绘制@#+
                        // Integer.toHexString(key.intValue())
                        // +中的+本+:+ CS);
                        mDrawableCache.put(重点,新的WeakReference< D​​rawable.ConstantState>(CS));
                    }
                }
            }
        }

        返回博士;
    }

    私人绘制对象getCachedDrawable(长键){
        同步(mTmpValue){
            WeakReference的< D​​rawable.ConstantState> WR = mDrawableCache.get(密钥);
            如果(WR!= NULL){//我们有钥匙
                Drawable.ConstantState条目= wr.get();
                如果(入门!= NULL){
                    //Log.i(TAG,返回缓存绘制@#+
                    // Integer.toHexString(((整数)键).intValue())
                    // +在+本+:+条目);
                    返回entry.newDrawable(本);
                }
                其他{//我们的项目已被清除
                    mDrawableCache.delete(键);
                }
            }
        }
        返回null;
    }
 

Does Android maintain a memory cache of application drawable resources and reuse them, or it is a good practice to preload all drawables that may be assigned dynamically to different widgets?

For instance:

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]);
    }
}
解决方案

This smells like an unnecessary pre-optimization. However, android does cache drawables so you don't have to pre-load them. Relevant code from 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;
    }

这篇关于有什么理由从资源preloading可绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:21