滚动不流畅,太慢了。我已经从SD卡中检索了视频并显示它们列表视图



请告诉我如何进行光滑的滚动。当我尝试向下滚动时,它需要很多时间来加载视频缩略图。请检查我的代码,并给我一个简单的解决方案。非常感谢。

我尝试了很多方法,但无法正常工作。不知道如何处理这个问题。我希望专家能帮助我完成我的项目。

public class VideoAdapter extends BaseAdapter {
    private Context vContext;
    public VideoAdapter(Context c) {
        vContext = c;
    }
    public int getCount() {
        return count;
    }
        public Object getItem(int position) {
            return position;
        }
        public long getItemId(int position) {
            return position;
        }
        public View getView(int position, View convertView, ViewGroup parent) {

            System.gc();
                ViewHolder holder;
                String id = null;
                convertView = null;
            if (convertView == null) {
                try {
                convertView = LayoutInflater.from(vContext).inflate(
                        R.layout.itemlist, parent, false);
                }
                catch (Exception e)
                {
                    Toast.makeText(getContext(),"Permission Granted" + e, Toast.LENGTH_SHORT).show();
                }
                    holder = new ViewHolder();
                try {
                    holder.txtTitle = (TextView) convertView
                            .findViewById(R.id.txtTitle);
                    holder.txtSize = (TextView) convertView
                            .findViewById(R.id.txtSize);
                    holder.thumbImage = (ImageView) convertView
                            .findViewById(R.id.imgIcon);
                }
                catch (Exception e)
            {
                Toast.makeText(getContext(),"Permission Granted" + e, Toast.LENGTH_SHORT).show();
            }
try {
    video_column_index = songCursor
            .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
    songCursor.moveToPosition(position);
    id = songCursor.getString(video_column_index);
    video_column_index = songCursor
            .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
    songCursor.moveToPosition(position);
} catch (Exception e)
{
    Toast.makeText(getContext(),"Permission Granted" + e, Toast.LENGTH_SHORT).show();
}
                // id += " Size(KB):" +
                // videocursor.getString(video_column_index);
                holder.txtTitle.setText(id);
                holder.txtSize.setText(" Size(KB):"
                        + songCursor.getString(video_column_index));
try {
    String[] proj = {MediaStore.Video.Media._ID,
            MediaStore.Video.Media.DISPLAY_NAME,
            MediaStore.Video.Media.DATA};
    @SuppressWarnings("deprecation")
    Cursor cursor = getActivity().getContentResolver().query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj,
            MediaStore.Video.Media.DISPLAY_NAME + "=?",
            new String[]{id}, null);
    cursor.moveToFirst();
}
catch (Exception e)
{
    Toast.makeText(getContext(),"nO Permission Granted" + e, Toast.LENGTH_SHORT).show();
}
    long ids = songCursor.getLong(songCursor
            .getColumnIndex(MediaStore.Video.Media._ID));
    ContentResolver crThumb = getActivity().getContentResolver();
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 1;
    Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, ids, MediaStore.Video.Thumbnails.MICRO_KIND, options);
           ///     if (holder.thumbImage !=null) {
                    holder.thumbImage.setImageBitmap(curThumb);
              //  }
          //      else {
          //          Drawable a = getContext().getResources().getDrawable(R.drawable.index);
           ///         holder.thumbImage.setImageDrawable(a);
              //  }
     curThumb = null;
}

            return convertView;
         }
        }
    static class ViewHolder {
        TextView txtTitle;
        TextView txtSize;
        ImageView thumbImage;
    }

不执行getView()方法中的所有代码,因为它每次滚动列表时都可以运行,这是滚动缓慢的原因,您甚至不需要获取所有的位图视频可能会生成OutOfMemoryException,获取片段或活动中视频的所有路径,然后将路径列表传递给适配器的构造函数,然后使用Glide库使用路径显示视频的缩略图:将此行放入App Gradle

compile 'com.github.bumptech.glide:glide:4.1.1'

然后编写此代码以显示视频的缩略图:

Glide.with(vContext).load(paths[position]).into(holder.thumbImage);

您可以从光标获得路径:

protected String[] getPaths() {
    Cursor cursor = getActivity().getContentResolver()
            .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
                    null, null, null);
    if (cursor == null)
        return null;
    String[] paths = new String[cursor.getCount()];
    File file;
    int i = 0;
    if (cursor.moveToFirst()) {
        do {
            String path = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
                /*
                It may happen that some image file paths are still present in cache,
                though image file does not exist. These last as long as media
                scanner is not run again. To avoid get such image file paths, check
                if image file exists.
                 */
            file = new File(path);
            if (file.exists()) {
                paths[i] = path;
                i++;
            }
        } while (cursor.moveToNext());
    }
    cursor.close();
    return paths;
}

我也有同样的问题。存储很慢。

您必须懒惰加载图像。为此,我创建了一个自定义图像视图。

public void setImageLazy(Uri image) {
    new Thread(new Runnable() {
        Bitmap bitmap = get your image or default ;
        runOnUiThread(new Runnable() {
            setImage(bitmap);
        }
    }.start();
}

如果它用于大量资源,请使用looper线程并排队运行。

编辑:此尝试有效,但不好。考虑使用处理程序。

最新更新