Video thumbnail arrayadopter is slow on scroll

前端 未结 2 1325
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 02:08

I have created an adopter to display images thumbnail of video form the specific folder but when I scroll it lags a little but why is that?

Below is the code:

相关标签:
2条回答
  • 2021-01-06 02:35

    The listview is lagging because you are creating new Thumbnails in getView() every time. There is just no caching or re-use. As the use scrolls up/down , getView() is called and time consuming process of creating thumbnails happen, hence the lag. You will also run Out of Memory soon.

    Create and keep an Array of Bitmaps, with the same length as the number of elements in the datasource and store your thumbnails in the array. Take out the bitmap from the array for that particular position inside getView(). This should considerably increase the performance.

    EDIT I have added a Hasmap of bitmaps to demonstrate the re-use.

     public MyAdapter(Activity context, String[] names) {
        super(context, R.layout.rowlayout, names);
        this.context = context;
        this.names = names;
        filePath = Environment.getExternalStorageDirectory() + dirNameSlash;
        formatter = new SimpleDateFormat(dateFormat);
    cacheBitmap = new HashMap<String, Bitmap>(names.length);
    initCacheBitmap();
    }
    
    private void initCacheBitmap() {
        for(String string:names)
            cacheBitmap.put(string, ThumbnailUtils.createVideoThumbnail(filePath+string, Thumbnails.MICRO_KIND));
    
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.rowlayout, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) rowView.findViewById(R.id.label);
            viewHolder.image = (ImageView) rowView.findViewById(R.id.icon);
            rowView.setTag(viewHolder);
        }
    
        ViewHolder holder = (ViewHolder) rowView.getTag();
        // Video file name
        String s = names[position];
    
        holder.text.setText(s);
    
    
        holder.image.setImageBitmap(cacheBitmap.get(s));
    
        return rowView;
    }}
    
    0 讨论(0)
  • 2021-01-06 02:48

    Since you are creating Thumnail for video inside getView() it slow down your scrolling.

    So you have to save your bitmap in Arrarys and list it i scroll.. Try this Sample for this will really helps you for fast scrolling. This Sample includes images , you have to change for Video in Media Content Providers. Fast Scroll Sample

    0 讨论(0)
提交回复
热议问题