通用图像加载器SLOW加载图像

时间:2013-11-05 19:24:17

标签: android performance gridview

首先,我在这里尝试所有解决方案,但一切都很糟糕。

我有一个网格视图,其中有382张图片来自Universal Image Loader。我试着在没有UIL和ViewHolder的情况下使用,并且瞬间加载图像会使gridview滚动速度太慢。

当我应用UIL时,滚动速度很快,但图像加载速度太慢......

任何人都可以帮助我吗?

在onCreate()上执行的config()方法:

private void config(){
    config = new ImageLoaderConfiguration.Builder(this.mContext.getApplicationContext())
    .memoryCacheSize(41943040)
    .discCacheSize(104857600)
    .threadPoolSize(6)//10
    .build();
    this.imageLoader.init(config);

    DisplayImageOptions.Builder dB = new Builder();
    dB.cacheOnDisc();
    try{
        dB.showStubImage(R.drawable.onloading);
    }catch(Exception e){            
        dB.showImageOnLoading(R.drawable.onloading);
    }
    dB.showImageForEmptyUri(R.drawable.onloadingerror); 
    dB.showImageOnFail(R.drawable.onloadingerror); 
    dB.bitmapConfig(Bitmap.Config.RGB_565);
    dB.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2);
    options = dB.build();
}

getView()方法:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        //final ImageView imageView;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater vi = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            //imageView = (ImageView) vi.inflate(R.layout.element_grid, parent, false);
            convertView = vi.inflate(R.layout.element_grid, null);

            holder.elemImg = (ImageView) convertView.findViewById(R.id.imagenGrid);
            convertView.setTag(holder);
        } else {
            //imageView = (ImageView) convertView;
            holder = (ViewHolder) convertView.getTag();
        }

        try {
            this.imageLoader.displayImage("drawable://"+Utils.getIntDrawableByName(this.coleccion.get(position).getImagen(), this.mContext), holder.elemImg, this.options,new ImageLoadingListener() {
                boolean cacheFound;
                @Override
                public void onLoadingStarted(String imageUri, View view) {
                    List<String> memCache = MemoryCacheUtil.findCacheKeysForImageUri(imageUri, ImageLoader.getInstance().getMemoryCache());
                    cacheFound = !memCache.isEmpty();
                    if (!cacheFound) {
                        File discCache = DiscCacheUtil.findInCache(imageUri, ImageLoader.getInstance().getDiscCache());
                        if (discCache != null) {
                            cacheFound = discCache.exists();
                        }
                    }
                }
                @Override
                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {}
                @Override
                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                    if (cacheFound) {
                        MemoryCacheUtil.removeFromCache(imageUri, ImageLoader.getInstance().getMemoryCache());
                        DiscCacheUtil.removeFromCache(imageUri, ImageLoader.getInstance().getDiscCache());

                        ImageLoader.getInstance().displayImage(imageUri, (ImageView) view);
                    }
                }
                @Override
                public void onLoadingCancelled(String imageUri, View view) {}
            });     

            this.carta = this.coleccion.get(position);
           // this.nombreImgCarta = this.carta.getImagen();
           // this.imageView.setImageResource(Utils.getIntDrawableByName(this.nombreImgCarta, this.mContext));
            holder.elemImg.setScaleType(ImageView.ScaleType.CENTER_CROP);

            //Detectar si el view del grid, que corresponde a una carta, ya tiene un id asignado.
            //En tal caso se le asigna el mismo id.
            this.id = 0;
            if (this.mapIdsGrid.containsKey(this.carta.getId())){
                this.id = this.mapIdsGrid.get(this.carta.getId());
            }else{
                this.id = Utils.getRandomId(this.mContext);
                this.mapIdsGrid.put(this.carta.getId(), this.id);
            }
            holder.elemImg.setId(this.id);

            //Detectar la trasparencia de la carta si hay cartas disponibles en la colección.
            if (this.coleccionPersonal && this.carta.getCantidad() <= 0){
                Utils.setAlphaForView(holder.elemImg, Constantes.VISIBLE_050);
            }

            //Métrica del Grid
            if (this.metrics == null){
                this.metrics = parent.getResources().getDisplayMetrics();
                this.hCarta = holder.elemImg.getDrawable().getMinimumHeight();
                this.wCarta = holder.elemImg.getDrawable().getMinimumWidth();
                this.cartaWFinal = (this.metrics.widthPixels / this.modificarCantidadColumnas) - MARGEN_CARTA;
                this.proporcion = this.cartaWFinal.doubleValue() / this.wCarta.doubleValue();
                this.cartaHFinal = (int) (this.hCarta * this.proporcion);
                this.gridView = new GridView.LayoutParams(this.cartaWFinal, this.cartaHFinal);
            }
            holder.elemImg.setLayoutParams(this.gridView);


        } catch (Exception e) {
            Utils.logErrorStart(log, "AdapterImage");
            Utils.logErrorIni(this.log, "getView");
            Utils.appendLog(this.log, e, null);
        }
        holder.elemImg.setImageDrawable(parent.getResources().getDrawable(Utils.getIntDrawableByName(this.coleccion.get(position).getImagen(), this.mContext)));
        return convertView;
    }

我不知道如何解决它...请帮助我!

0 个答案:

没有答案