Android的ListView的图像滞后 [英] Android listview image lag

查看:137
本文介绍了Android的ListView的图像滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序包含一个ListView可以有选择地包含来自用户的图像SD卡。然而,当任何加载图像有明显的滞后,它工作正常。我有这个在我的getView:

  BitmapFactory.Options O =新BitmapFactory.Options();
             o.inJustDe codeBounds = TRUE;
             BitmapFactory.de codeFILE(图像,O);
             最终诠释REQUIRED_SIZE = 70;             //找到正确的比例值。它应该是2的幂。
             INT width_tmp = o.outWidth,height_tmp = o.outHeight;
             int标= 1;
             而(真){
                 如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2版; REQUIRED_SIZE)
                     打破;
                 width_tmp / = 2;
                 height_tmp / = 2;
                 规模++;
             }             //德code。与inSampleSize
             BitmapFactory.Options O2 =新BitmapFactory.Options();
             o2.inSampleSize =规模;
             位图MYBITMAP = BitmapFactory.de codeFILE(图像,O2);
             image_main.setImageBitmap(MYBITMAP);

有什么建议?

编辑:替换上面这个,但没有图像加载...

 类thumbnailer的扩展的AsyncTask<弦乐,太虚,位图> {
                    字符串图像;                    @覆盖
                    保护无效onPostExecute(位图结果){
                        image_main.setImageBitmap(结果);
                    }
                    @覆盖
                     保护无效onProgressUpdate(无效...进度){
                     }                    @覆盖
                    保护位图doInBackground(字符串... PARAMS){
                         BitmapFactory.Options O =新BitmapFactory.Options();
                         o.inJustDe codeBounds = TRUE;
                         BitmapFactory.de codeFILE(图像,O);
                         最终诠释REQUIRED_SIZE = 70;                         //找到正确的比例值。它应该是2的幂。
                         INT width_tmp = o.outWidth,height_tmp = o.outHeight;
                         int标= 1;
                         而(真){
                             如果(width_tmp / 2'; REQUIRED_SIZE || height_tmp / 2版; REQUIRED_SIZE)
                                 打破;
                             width_tmp / = 2;
                             height_tmp / = 2;
                             规模++;
                         }
                         //德code。与inSampleSize
                         BitmapFactory.Options O2 =新BitmapFactory.Options();
                         o2.inSampleSize =规模;
                         返回BitmapFactory.de codeFILE(图像,O2);
                    }
                }
                新thumbnailer的()执行(图像);


解决方案

显示一个图像占位符的行。不要在你的 BitmapFactory 工作的的AsyncTask 更新的ImageView 完成后(如果的ImageView 尚未回收的,因此仍然需要这种特殊的缩略图)。缓存你的工作,这样你就不必再重新做所有的缩放比例,至少在这个化身的ListView ,也许更长,如果图像不会被改变。

My app contains a listview which can optionally contain images from a users sd card. It works fine however when any image is loaded there is noticeable lag. I have this in my getView:

          BitmapFactory.Options o = new BitmapFactory.Options();
             o.inJustDecodeBounds = true;
             BitmapFactory.decodeFile(image, o);
             final int REQUIRED_SIZE=70;

             //Find the correct scale value. It should be the power of 2.
             int width_tmp=o.outWidth, height_tmp=o.outHeight;
             int scale=1;
             while(true){
                 if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                     break;
                 width_tmp/=2;
                 height_tmp/=2;
                 scale++;
             }

             //Decode with inSampleSize
             BitmapFactory.Options o2 = new BitmapFactory.Options();
             o2.inSampleSize=scale;
             Bitmap myBitmap = BitmapFactory.decodeFile(image, o2);
             image_main.setImageBitmap(myBitmap);

Any suggestions?

EDIT: Replaced the above with this but no images are loading...

            class Thumbnailer extends AsyncTask<String, Void, Bitmap> {
                    String image;

                    @Override
                    protected void onPostExecute(Bitmap result) {
                        image_main.setImageBitmap(result);
                    }
                    @Override
                     protected void onProgressUpdate(Void... progress) {
                     }

                    @Override
                    protected Bitmap doInBackground(String... params) {
                         BitmapFactory.Options o = new BitmapFactory.Options();
                         o.inJustDecodeBounds = true;
                         BitmapFactory.decodeFile(image, o);
                         final int REQUIRED_SIZE=70;

                         //Find the correct scale value. It should be the power of 2.
                         int width_tmp=o.outWidth, height_tmp=o.outHeight;
                         int scale=1;
                         while(true){
                             if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                                 break;
                             width_tmp/=2;
                             height_tmp/=2;
                             scale++;
                         }
                         //Decode with inSampleSize
                         BitmapFactory.Options o2 = new BitmapFactory.Options();
                         o2.inSampleSize=scale;                                  
                         return BitmapFactory.decodeFile(image, o2);
                    }
                } 
                new Thumbnailer().execute(image);

解决方案

Show the row with a placeholder image. Do your BitmapFactory work in an AsyncTask, updating the ImageView when done (if the ImageView hasn't been recycled and so still needs this particular thumbnail). Cache your work so you do not have to re-do all the scaling again, at least for this incarnation of the ListView, perhaps longer if the images will not be changing.

这篇关于Android的ListView的图像滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆