我目前正在将设备上的所有视频加载到一个自定义ListAdapter中,该ListAdapter显示Video+更多内容的缩略图。我注意到,当我添加越来越多的视频时,启动速度变得越来越慢。这就是我的列表适配器的样子:
public class VideoListAdapter extends ArrayAdapter<Video> {
private ArrayList<Video> allVideos;
private HashMap<String, Bitmap> bitmapCache;
public VideoListAdapter(Context context, ArrayList<Video> videos) {
super(context, R.layout.video_list_item, videos);
this.allVideos = videos;
/* Cache the thumbnails */
setUpBitmaps();
}
private void setUpBitmaps() {
bitmapCache = new HashMap<String, Bitmap>(allVideos.size());
for(Video video : allVideos){
bitmapCache.put(video.getDATA(), ThumbnailUtils.createVideoThumbnail(video.getDATA(), Thumbnails.MICRO_KIND));
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(R.layout.video_list_item, null);
} else {
row = convertView;
}
Video tmpVideo = allVideos.get(position);
String TITLE = tmpVideo.getTITLE();
long vidDur = Long.valueOf(tmpVideo.getDURATION());
String DURATION = String.format(Locale.getDefault(),"%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(vidDur) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(vidDur)),
TimeUnit.MILLISECONDS.toSeconds(vidDur) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(vidDur)));
String filepath = tmpVideo.getDATA();
Bitmap thumbnail = bitmapCache.get(filepath);
TextView tvTitle = (TextView) row.findViewById(R.id.tvListItemVideoTitle);
TextView tvDuration = (TextView) row.findViewById(R.id.tvListItemVideoDuration);
ImageView ivThumbnail = (ImageView) row.findViewById(R.id.ivListItemVideoThumbnail);
tvTitle.setText(TITLE);
tvDuration.setText(DURATION);
if(thumbnail != null){
ivThumbnail.setImageBitmap(thumbnail);
}
return row;
}
}
应该如何加载缩略图以减少加载列表适配器所需的时间?目前在我的设备上,显示适配器的活动需要3-4秒,我只有大约15个视频。
如有任何建议,我们将不胜感激。
Marcus
不要在列表视图中显示视频,而是通过传递视频路径来获取视频的缩略图,并将其显示在listview中。。使用此代码可以获取缩略图。也可以使用lazylist来正确显示图像。
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(path,
MediaStore.Images.Thumbnails.MINI_KIND);
You can use lazyloading for this,LazyLoading will make your list scroll fast.Follow this library it will help:
https://github.com/nostra13/Android-Universal-Image-Loader
Steps of using lazy loading :
1.)Download jar file from this link:
http://www.java2s.com/Code/Jar/u/Downloaduniversalimageloader161withsrcjar.htm
2.)Create Application File :
import android.app.Application;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// UNIVERSAL IMAGE LOADER SETUP
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheOnDisc(true).cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache())
.discCacheSize(100 * 1024 * 1024).build();
ImageLoader.getInstance().init(config);
// END - UNIVERSAL IMAGE LOADER SETUP
}
}
3.)On your activity in whcih you want to use ,write this it onCreate():
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.iclauncher) // resource or
// drawable
.showImageForEmptyUri(R.drawable.iclauncher) // resource or
// drawable
.showImageOnFail(R.drawable.iclauncher) // resource or
// drawable
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(1000).cacheInMemory(true) // default
.cacheOnDisc(true) // default
.considerExifParams(true) // default
.imageScaleType(ImageScaleType.IN_SAMPLE_INT) // default
.bitmapConfig(Bitmap.Config.ARGB_8888) // default
.displayer(new SimpleBitmapDisplayer()) // default
.handler(new Handler()) // default
.build();
imageLoader = ImageLoader.getInstance();
4.)on your adapter write this:
imageLoader.displayImage(alist.get(position).getThumbnails(),
holder.ivImage, options, null); // alist.get(position).getThumbnails() is the url of the thumbnail and holder.ivImage is the reference of the imageview where thumbnail is to be placed