使用Custom CursorAdapters延迟GridView性能

时间:2014-01-04 13:56:24

标签: java android android-gridview simplecursoradapter android-adapter

我目前正试图获得android developpemnt的支持,到目前为止一直很好(直到现在)。我目前正在为我的网格视图使用自定义光标适配器。我使用游标适配器读取主要UI线程的负载听起来有效,所以我试图实现我自己的游标适配器如下

public class VideoListAdapter extends SimpleCursorAdapter{

private Context ctx;
private int parent_layout_id;

ArrayList<VideoModel> videoList = new ArrayList<VideoModel>();

public VideoListAdapter(
        Context ctx,
        int parent_layout_id,
        Cursor c,
        String[] fromColumns,
        int[] toView)
{
    super(ctx,parent_layout_id,c,fromColumns,toView);
    this.ctx = ctx;
    this.parent_layout_id = parent_layout_id;
}

@Override
public void bindView(View v, Context context, Cursor c) {

    //gets title column
    int titleColumn = c.getColumnIndex(MediaStore.Video.Media.TITLE);
    String title = c.getString(titleColumn);
    TextView titleView =(TextView) v.findViewById(R.id.video_title);
    titleView.setText(title);

    //gets duration
    int durationColumn = c.getColumnIndex(MediaStore.Video.Media.DURATION);
    String duration = c.getString(durationColumn);
    TextView durationView = (TextView) v.findViewById(R.id.video_date);
    durationView.setText(duration);

    //gets Thumb
    int videoDataColumn = c.getColumnIndex(MediaStore.Video.Media.DATA);
    String videodataUrl = c.getString(videoDataColumn);
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(videodataUrl);
    Bitmap bm = mmr.getFrameAtTime(1000);
    ImageView imgv = (ImageView) v.findViewById(R.id.video_thumb);
    imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imgv.setImageBitmap(bm);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    Cursor c = getCursor();

    //get layout inflater
    final LayoutInflater li=LayoutInflater.from(context);
    View v = li.inflate(parent_layout_id,parent,false);

    //gets title column
    int titleColumn = c.getColumnIndex(MediaStore.Video.Media.TITLE);
    String title = c.getString(titleColumn);
    TextView titleView =(TextView) v.findViewById(R.id.video_title);
    titleView.setText(title);

    //gets duration
    int durationColumn = c.getColumnIndex(MediaStore.Video.Media.DURATION);
    String duration = c.getString(durationColumn);
    TextView durationView = (TextView) v.findViewById(R.id.video_date);
    durationView.setText(duration);

    //gets Thumb
    int videoDataColumn = c.getColumnIndex(MediaStore.Video.Media.DATA);
    String videodataUrl = c.getString(videoDataColumn);
    MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    mmr.setDataSource(videodataUrl);
    Bitmap bm = mmr.getFrameAtTime(1000);
    ImageView imgv = (ImageView) v.findViewById(R.id.video_thumb);
    imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imgv.setImageBitmap(bm);
    return v;
}

@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
    return super.runQueryOnBackgroundThread(constraint);
}

}

我想要实现的是从外部媒体存储中获取视频文件列表并将其显示在gridview中。我想到的标准缩略图对于布局样式来说太小了,所以我使用MediaMetadataRetriever从每个视频文件中获取帧并将其用作缩略图。像这样Grid View

当我运行应用程序时,我的gridView的滚动性能变差了。我错过了什么我知道SimpleCursorAdapter类有一个可覆盖的方法runQueryOnBackgroundThread,但我甚至不知道该放什么或如何使用它。任何指针都会非常感激并原谅我的涂抹脸;)

1 个答案:

答案 0 :(得分:1)

首先使用ViewHolder绑定Gridview,然后使用save your thumb(bitmap) in existing arrayList绑定第二个,这样滚动时就无法进行相同的处理。