ListView的滚动速度慢而从内部存储的图像加载速度慢、图像、加载、ListView

2023-09-06 15:42:27 作者:该心情不好不想显示

我已经创建了一个自定义的的ListView 我在其中显示图像作为列表项。我显示为一个列表项的图像保存在我的 InternalStorage 的设备,我注意到的是,当项目的数量更多的则是七列表视图滚动变得缓慢,在 ListView的项目,而滚动混蛋。什么错在我的code或错过的东西在我的code做。请帮我解决了这一点。

code显示列表视图项

 公共类ListViewAdapter延伸BaseAdapter    {        私人活动活动;        私人的ArrayList<串GT; fileNameArray;        私人LayoutInflater吹气= NULL;        公共ImageLoader的ImageLoader的;        公共ListViewAdapter(活动一,ArrayList的<串D 1和D)        {            活性=一个;            fileNameArray = D;            吹气=(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            ImageLoader的=新ImageLoader的(activity.getApplicationContext());        }        公众诠释getCount将()        {            返回fileNameArray.size();        }        公共对象的getItem(INT位置)        {            返回的位置;        }        众长getItemId(INT位置)        {            返回的位置;        }        公共类ViewHolder        {            ImageView的形象;        }        公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)        {            ViewHolder持有人;            如果(convertView == NULL)            {                持有人=新ViewHolder();                convertView = inflater.inflate(R.layout.page_list_item,NULL);                holder.image =(ImageView的)convertView.findViewById(R.id.image);                convertView.setTag(保持器);            }            其他            {                支架=(ViewHolder)convertView.getTag();            }            //从图像视图internalstoarage和显示加载图像            imageLoader.DisplayImage(m_DirectoryPath.getPath()+/+ fileNameArray.get(位置),holder.image,真);            返回convertView;        }} 

code图像装载机类

 公共类ImageLoader的{    的MemoryCache的MemoryCache =新的MemoryCache();    私人地图< ImageView的,字符串> imageViews = Collections.synchronizedMap(新的WeakHashMap< ImageView的,字符串>());    ExecutorService的ExecutorService的;    上下文m_context;    公共ImageLoader的(上下文的背景下)    {        m_context =背景;        ExecutorService的= Executors.newFixedThreadPool(5);    }    公共无效DisplayImage(字符串URL,ImageView的ImageView的,布尔useCachedImage)    {        imageViews.put(ImageView的,URL);        位图位图= memoryCache.get(URL);        如果(位图= NULL&放大器;!&安培; useCachedImage)        {            imageView.setImageBitmap(位图);        }        其他        {            queuePhoto(URL,ImageView的);        }    }    私人无效queuePhoto(字符串URL,ImageView的ImageView的)    {        PhotoToLoad P =新PhotoToLoad(URL,ImageView的);        executorService.submit(新PhotosLoader(P));    }    私人位图getBitmap(字符串URL)    {        文件文件路径=新的文件(URL);        //从内部存储缓存        位图B =去codeFILE(文件路径);        如果(B!= NULL)        {            返回b;        }        返回null;    }    //德$ C $连拍影像的鳞它来减少内存消耗    私人位图德codeFILE(文件文件路径)    {        尝试        {            //德code图像尺寸             INT Required_Height = 0;             BitmapFactory.Options选项=新BitmapFactory.Options();             位图位图= BitmapFactory.de codeStream(新的FileInputStream(文件路径),空,选件);             窗口管理WM =(窗口管理器)m_context.getSystemService(Context.WINDOW_SERVICE);             显示显示= wm.getDefaultDisplay();             如果(display.getWidth()== 480)             {                 Required_Height = 150;             }             否则如果(display.getWidth()== 800)             {                 Required_Height = 200;             }             位图= Bitmap.createBitmap(位图,0,0,option.outWidth,Required_Height);             返回位图;        }        赶上(FileNotFoundException异常五){        }        返回null;    }    //任务队列    私有类PhotoToLoad    {        公共字符串的URL;        公共ImageView的ImageView的;        公共PhotoToLoad(字符串U,ImageView的I)        {            URL = U;            ImageView的= I;        }    }    类PhotosLoader实现Runnable    {        PhotoToLoad photoToLoad;        PhotosLoader(PhotoToLoad photoToLoad)        {            this.photoToLoad = photoToLoad;        }        @覆盖        公共无效的run()        {            如果(imageViewReused(photoToLoad))                返回;            BMP位图= getBitmap(photoToLoad.url);            memoryCache.put(photoToLoad.url,BMP);            如果(imageViewReused(photoToLoad))                返回;            BitmapDisplayer BD =新BitmapDisplayer(BMP,photoToLoad);            活动一=(活动)photoToLoad.imageView.getContext();            a.runOnUiThread(BD);        }    }    布尔imageViewReused(PhotoToLoad photoToLoad)    {        字符串标记= imageViews.get(photoToLoad.imageView);        如果(标记== NULL ||!tag.equals(photoToLoad.url))            返回true;        返回false;    }    //用在UI线程来显示位图    类BitmapDisplayer实现Runnable    {        位图位图;        PhotoToLoad photoToLoad;        公共BitmapDisplayer(位图B,PhotoToLoad P){位= B; photoToLoad = P;}        公共无效的run()        {            如果(imageViewReused(photoToLoad))                返回;            如果(位图!= NULL)                photoToLoad.imageView.setImageBitmap(位图);        }    }    公共无效clearCache()    {        memoryCache.clear();    }} 

解决方案 手机qq的qq群里图片加载速度变得特别慢,但在空间图片加载正常,怎么回事

您必须对setPriority的 PhotosLoader 线程对象。我做它的构造是这样的。

  //使后台线程低优先级。这样也不会影响用户界面性能mImageRequstController.setPriority(Thread.NORM_PRIORITY  -  1); 

通过将优先小于正常,不会影响用户界面,并会继续在后台其工作。

I have created an custom ListView in which i display image as an list Item. The image i display as a list item is stored in my InternalStorage of the device, What i notice is when number of item is more then seven the list view scroll become slow and the items in the ListView jerk while scrolling. Is anything wrong in my code, or miss something to do in my code. Please help me to solve this out.

Code for displaying List View item

public class ListViewAdapter extends BaseAdapter 
    {
        private Activity activity;
        private ArrayList<String> fileNameArray;
        private LayoutInflater inflater=null;
        public  ImageLoader imageLoader; 

        public ListViewAdapter(Activity a,  ArrayList<String> d) 
        {
            activity = a;
            fileNameArray = d;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            imageLoader = new ImageLoader(activity.getApplicationContext());
        }

        public int getCount() 
        {
            return fileNameArray.size();
        }

        public Object getItem(int position) 
        {
            return position;
        }

        public long getItemId(int position) 
        {
            return position;
        }

        public class ViewHolder
        {
            ImageView image;
        }               

        public View getView(final int position, View convertView, ViewGroup parent) 
        {
            ViewHolder holder;

            if(convertView == null)
            {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.page_list_item, null);

                holder.image = (ImageView)convertView.findViewById(R.id.image); 

                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder)convertView.getTag();
            }           



            // Load image from internalstoarage and display in image view   

            imageLoader.DisplayImage(m_DirectoryPath.getPath() + "/" + fileNameArray.get(position), holder.image, true);

            return convertView;
        }
}

Code for image loader class

public class ImageLoader 
{
    MemoryCache memoryCache = new MemoryCache();
    private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
    ExecutorService executorService; 
    Context m_context;
    public ImageLoader(Context context)
    {
        m_context = context;
        executorService = Executors.newFixedThreadPool(5);
    }

    public void DisplayImage(String url, ImageView imageView, boolean useCachedImage)
    {
        imageViews.put(imageView, url);
        Bitmap bitmap = memoryCache.get(url);

        if(bitmap!=null && useCachedImage)
        {
            imageView.setImageBitmap(bitmap);
        }
        else
        {
            queuePhoto(url, imageView);
        }
    }

    private void queuePhoto(String url, ImageView imageView)
    {
        PhotoToLoad p = new PhotoToLoad(url, imageView);
        executorService.submit(new PhotosLoader(p));
    }

    private Bitmap getBitmap(String url) 
    {
        File filePath = new File(url);

        // from internal storage cache
        Bitmap b = decodeFile(filePath);
        if (b != null)
        {
            return b;
        }
        return null;
    }

    // decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File filePath)
    {
        try 
        {
            // decode image size
             int Required_Height = 0;

             BitmapFactory.Options option = new BitmapFactory.Options();            
             Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(filePath), null, option);

             WindowManager wm = (WindowManager) m_context.getSystemService(Context.WINDOW_SERVICE);
             Display display = wm.getDefaultDisplay();

             if(display.getWidth() == 480)
             {
                 Required_Height = 150;
             }
             else if(display.getWidth() == 800)
             {
                 Required_Height = 200;
             }

             bitmap = Bitmap.createBitmap(bitmap, 0, 0, option.outWidth, Required_Height);

             return bitmap;
        } 
        catch (FileNotFoundException e){            
        }
        return null;
    }

    // Task for the queue
    private class PhotoToLoad
    {
        public String url;
        public ImageView imageView;
        public PhotoToLoad(String u, ImageView i)
        {
            url = u; 
            imageView=i;
        }
    }

    class PhotosLoader implements Runnable 
    {
        PhotoToLoad photoToLoad;
        PhotosLoader(PhotoToLoad photoToLoad)
        {
            this.photoToLoad = photoToLoad;
        }

        @Override
        public void run() 
        {
            if(imageViewReused(photoToLoad))
                return;

            Bitmap bmp = getBitmap(photoToLoad.url);
            memoryCache.put(photoToLoad.url, bmp);

            if(imageViewReused(photoToLoad))
                return;

            BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
            Activity a = (Activity)photoToLoad.imageView.getContext();
            a.runOnUiThread(bd);
        }
    }

    boolean imageViewReused(PhotoToLoad photoToLoad)
    {
        String tag = imageViews.get(photoToLoad.imageView);

        if(tag == null || !tag.equals(photoToLoad.url))         
            return true;

        return false;
    }

    // Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable
    {
        Bitmap bitmap;
        PhotoToLoad photoToLoad;
        public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
        public void run()
        {
            if(imageViewReused(photoToLoad))
                return;

            if(bitmap != null)
                photoToLoad.imageView.setImageBitmap(bitmap);
        }
    }

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

}

解决方案

You have to setPriority for PhotosLoader thread object. I am doing it in constructor like this.

// Make the background thread low priority. This way it will not affect the UI performance
mImageRequstController.setPriority(Thread.NORM_PRIORITY - 1);

By make priority less then normal , it will not effect the UI and will continue its working in background.