【发布时间】:2014-01-04 13:56:23
【问题描述】:
我目前正在尝试掌握 android developmentemnt 的窍门,到目前为止它进展顺利(直到现在)。我目前正在为我的网格视图使用自定义光标适配器。我读到使用光标适配器承担了听起来很有效的主 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);
}
}
我想要实现的是从外部媒体存储中获取视频文件列表并将它们显示在网格视图中。标准缩略图对于我想到的布局风格来说太小了,所以我使用 MediaMetadataRetriever 从每个视频文件中获取帧并将其用作缩略图。喜欢这样
当我运行应用程序时,我的 gridView 的滚动性能变差了。我错过了什么。我知道 SimpleCursorAdapter 类有一个可覆盖的方法 runQueryOnBackgroundThread 但我什至不知道放在那里或如何使用它。任何指点都将不胜感激并原谅我的污迹;)
【问题讨论】:
标签: java android android-gridview simplecursoradapter android-adapter