【问题标题】:Best way to display image in grid view is scrolling smooth在网格视图中显示图像的最佳方式是平滑滚动
【发布时间】:2012-04-25 08:38:09
【问题描述】:

首先,我已经做了一些事情来在gridview中显示图像以使滚动时平滑

1.在后台线程上从互联网加载图像

AsyncTask acyncTask ;
HandlerThread handlerThread ;

 URL imageUrl = new URL(link);<br>
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            final Bitmap bitmap = BitmapFactory.decodeStream(is);

2。将位图制作成样本大小以节省内存

final BitmapFactory.Options option = new BitmapFactory.Options();
    option.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mFile.getPath(),
            option);
    option.inSampleSize = calculateInSampleSize(option , mSize , mSize);
    option.inJustDecodeBounds = false;
    option.inPurgeable = true ;
    option.inDither = false ;
    option.inScaled = false ;
    option.inPreferredConfig = Bitmap.Config.ARGB_8888;

3.制作缓存以保存解码的位图

LruCache lruCache = new LruCache<String , Bitmap>();

baseadapter getView(); 我会lruCache.get(key)拿解码的位图

4.通过处理程序解码位图时的延迟加载

 Handler handler = new Handler();
        handler.post(new Runnable(){
           public void run(){
              imageView.setimageBitmap(bitmap);
          }
      });

现在我面临一个大问题,当我滚动时它仍然有点滞后 我有谷歌关于可以使 滚动更好 的东西,不知道真的可以改进或问题出在我检查每个 getView() 的地方只花了我大约 2 ~6ms,它会调用我的代码来异步加载图像表单工作线程,所以我真的不知道为什么有些应用可以加载非常流畅?我的情况是滚动时屏幕看起来不太流畅,是否可以应用一些建议?

已编辑:当我滚动时,在缓存中找到的位图并显示在屏幕上,如果我快速滚动,它看起来就像没有足够的滚动来显示图像,它会使我的滚动不那么流畅,即使我有在缓存中缓存位图

这是适配器代码:

if (convertView == null) {
        convertView = layoutInflater.inflate(
                R.layout.row_display_image_grid, null);
        viewHolder = new DisplayImageGridViewHolder();
        viewHolder.background = (RelativeLayout) convertView
                .findViewById(R.id.background);
        viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
        viewHolder.text = (TextView) convertView.findViewById(R.id.text);
        viewHolder.position = position;
        viewHolder.text.setEllipsize(TruncateAt.END);
        viewHolder.text.setTypeface(typeFace);
        viewHolder.image.setOnClickListener(this);
        viewHolder.image.setOnLongClickListener(this);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (DisplayImageGridViewHolder) convertView.getTag();
    }

viewHolder.position = position;
imageLoader.loadImage(imageLinkList.get(position), viewHolder.image);
return convertView;

【问题讨论】:

  • 您能否发布您的适配器代码,特别是 getView() 方法。这很可能是滚动延迟的来源。
  • 已经添加了,我只是在工作线程中调用了一个方法来启动加载任务,我在这个getview方法之前和之后检查过,它只花了我几秒钟,你认为setimagebitmap是什么东西滚动gridview时出现问题
  • 你的代码还是有点不清楚。你能发布你的整个 ImageLoader 类吗?我正在使用非常相似的代码,可能基于相同的示例,并且没有延迟。
  • 哦,我明白了,您使用的是类似的,您如何在 gridview 上显示图像?如果你快速滚动,会不会有点滞后?我只是将显示位图方法从 runonuithread 更改为处理程序 postdelayed ,否则相同。
  • 我只是使用 AsyncTasks 来加载图像。从 doInBackground 中的网络/本地存储/内存加载图像。然后在 onPostExecute 中设置图片。

标签: android performance bitmap


【解决方案1】:

下面是我使用的 LazyLoader 示例。注意我正在为位图使用 SoftReferences,现在使用 LruCache 更好地服务。

这将从 web / sdcard / memory 异步加载图像,并从占位符图像创建淡入效果。

public class ImageLoader {

private static MemoryCacheNew memoryCache=new MemoryCacheNew();
private static FileCache fileCache;

private static BitmapFactory.Options bitmapOptions;

private static int mInSampleSize;



public ImageLoader(Context context, int inSampleSize){


    fileCache=new FileCache(context);        

    context = null;

    bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = mInSampleSize = inSampleSize;
    bitmapOptions.inPreferredConfig = Config.RGB_565;
    bitmapOptions.inInputShareable = true;
    bitmapOptions.inDither = false;

}

final static int PLACEHOLDER_IMAGE = R.drawable.store_placeholder;

public void DisplayImage(String url, ImageView imageView,  boolean checkTags){

    try{

    new AsyncPhotoTask(imageView, url, checkTags).execute();

    }catch(Exception e){
        e.printStackTrace();
    }

}

public void DisplayImage(String url, ImageView imageView)
{
      DisplayImage(url, imageView, true); 

}


private static Bitmap getBitmap(String url) 
{
    File f=fileCache.getFile(url);


    if(f!= null){
    //from SD cache
    Bitmap b = decodeFile(f);
    if(b!=null)
        return b;
    }

    //from web
    try {
        Bitmap bitmap=null;

        URL imageUrl;

            imageUrl = new URL(url);


        HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        InputStream is=conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        is.close();
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Exception ex){
       ex.printStackTrace();
       return null;
    }
}

//decodes image and scales it to reduce memory consumption
private static Bitmap decodeFile(File f){
    try {
        return BitmapFactory.decodeStream(new FileInputStream(f), null, bitmapOptions);

    } catch (FileNotFoundException e) {

    } catch (OutOfMemoryError err){
        System.gc();
    } 

    return null;
}


private static class AsyncPhotoLoad extends AsyncTask<Void, Void, TransitionDrawable>{

    private Bitmap bmp;
    private ImageView imageView;
    private String url;
    private boolean checkTags;

    public AsyncPhotoLoad(ImageView imageView, String url, boolean checkTags
            ){
        this.imageView = imageView;
        this.url = url;
        this.checkTags = checkTags;
    }

    @Override
    protected TransitionDrawable doInBackground(Void... arg0) {

        //check that this is the correct imageview



        TransitionDrawable transition = null;

        try{
            if(checkTags){
            String tag = (String)imageView.getTag();    
            if(!tag.equals(url))
                return null;
            }

        bmp = getBitmap(url);

        if(bmp != null){
            memoryCache.put(url, bmp, mInSampleSize);    

            Drawable oldDrawable = imageView.getDrawable();

            if(!(oldDrawable instanceof TransitionDrawable)){

                Drawable layers[] = new Drawable[2];                    
                layers[0] = imageView.getDrawable();
                layers[1] = new BitmapDrawable(bmp);

               transition = new TransitionDrawable(layers);

            }



        }

        }catch(Exception e){
            e.printStackTrace();
        }

        return transition;
    }

    @Override
    protected void onPostExecute(TransitionDrawable result) {
        if(result != null){
            try{            
                if(checkTags){                      
                        String tag = (String)imageView.getTag();    
                        if(!tag.equals(url)){
                            return;
                        }
                }

                   imageView.setImageDrawable(result);
                   result.startTransition(300);



            } catch(Exception e){
                e.printStackTrace();
            }
        } else {
            if(checkTags){
            try{
                String tag = (String)imageView.getTag();    
                if(!tag.equals(url))
                    return;

            }catch(Exception e){
                e.printStackTrace();
            }
            }


        }
    }

}



private static class AsyncPhotoTask extends AsyncTask<Void, Void, Bitmap>{

    private ImageView imageView;
    private String url;
    private boolean checkTags;


    public AsyncPhotoTask(ImageView imageView, String url, boolean checkTags){

        this.imageView = imageView;
        this.url = url;
        this.checkTags = checkTags;
    } 


    @Override
    protected Bitmap doInBackground(Void... params) {
        try{
        if(checkTags)
            imageView.setTag(url);  

        }catch(Exception e){
            e.printStackTrace();
        }

        return memoryCache.get(url, mInSampleSize);
    }


    @Override
    protected void onPostExecute(Bitmap result) {
        try{
        if(result!=null && !result.isRecycled()){


            imageView.setImageBitmap(result);   
        }
        else
        {   

            imageView.setImageResource(PLACEHOLDER_IMAGE);            
            new AsyncPhotoLoad(imageView, url, checkTags).execute();      

        }    

        }catch(Exception e){
            e.printStackTrace();
        }
    }



}





public static void clearCache() {
    memoryCache.clear();
    fileCache.clear();
}

public static void clearMemory(){
    memoryCache.clear();
}

public static class MemoryCacheNew {
    private HashMap<String, CachedBitmap> cache=new HashMap<String, CachedBitmap>();

    public Bitmap get(String id, int sampleSize){
        if(!cache.containsKey(id))
            return null;

        if(cache.get(id) == null)
            return null;

        if(cache.get(id).sampleSize != sampleSize)
            return null;

        SoftReference<Bitmap> ref = cache.get(id).softBitmap;
        return ref.get();
    }

    public void put(String id, Bitmap bitmap, int sampleSize){
        cache.put(id, new CachedBitmap(bitmap, sampleSize));
    }

    public void clear() {
        cache.clear();
    }



    private static class CachedBitmap {
        public SoftReference<Bitmap> softBitmap;
        public int sampleSize;

        public CachedBitmap(Bitmap bitmap, int sampleSize){
            this.softBitmap = new SoftReference<Bitmap>(bitmap);
            this.sampleSize = sampleSize;
        }
    }
}


}


public class FileCache {

private File cacheDir;

public FileCache(Context context){
    //Find the dir to save cached images
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir=new File(context.getExternalCacheDir(),Consts.STORE_CACHE);
    else
        cacheDir=context.getCacheDir();
    if(!cacheDir.exists())
        cacheDir.mkdirs();
}

public File getFile(String url){
    //I identify images by hashcode. Not a perfect solution, good for the demo.
    String filename=String.valueOf(url.hashCode());
    File f = new File(cacheDir, filename);
    return f;

}

public void clear(){
    File[] files=cacheDir.listFiles();
    for(File f:files)
        f.delete();
}

}

你这样称呼它:

imageLoader.DisplayImage(url, holder.image);

【讨论】:

  • 是的,你也在使用类似的代码,但是它帮不了我,对不起
【解决方案2】:

尽量确保每个位图的大小都达到最佳...它们应该与容器的大小差不多。在通过 RemoteViews 发送它们之前,尽可能地缩小它们。您可以使用 onAppWidgetOptionsChanged 计算小部件大小。

另外,请尝试使用 setImageViewUri(使用内容提供程序)而不是 setImageViewBitmap 以获得最佳性能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多