我有一个带图像项的listView。我正在使用UniversalImageLoader从url获取图像并将其设置在ImageView上。 我试图在另一个线程中编写代码,但是由于imageView是在主线程中构建的,因此我的应用程序崩溃了。 问题是当我滚动listView时,加载图像会减慢滚动速度并使其缓慢。这是我的getView():
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final viewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (isGrid)
convertView = mInflater.inflate(R.layout.main_grid_item, null);
else
convertView = mInflater.inflate(R.layout.main_list_item, null);
holder = new viewHolder();
holder.ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
RelativeLayout.LayoutParams ivLPI;
if (isGrid) {
ivLPI = new RelativeLayout.LayoutParams(imageSize, imageSize);
ivLPI.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
ivLPI.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
ivLPI.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
} else {
ivLPI = new RelativeLayout.LayoutParams(Utils.convertDpToPx(context, 120), Utils.convertDpToPx(context, 120));
ivLPI.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
ivLPI.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
}
holder.ivImage.setLayoutParams(ivLPI);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.delayBeforeLoading(0)
.resetViewBeforeLoading(true)
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.EXACTLY)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.denyCacheImageMultipleSizesInMemory()
.defaultDisplayImageOptions(defaultOptions)
.diskCacheExtraOptions(480, 320, null)
.threadPoolSize(10)
.build();
ImageLoader.getInstance().init(config);
final ProgressBar spinner = (ProgressBar) convertView.findViewById(R.id.spinner);
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(data.get(position).getThumbURL(), holder.ivImage, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String s, View view) {
spinner.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String s, View view, FailReason failReason) {
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String s, View view, Bitmap bitmap) {
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String s, View view) {
spinner.setVisibility(View.GONE);
}
});
return convertView;
}
答案 0 :(得分:0)
从您的代码中,我看到您的DisplayImageOptions
和ImageLoaderConfiguration
并没有改变,而是您每次都创建它的原因。
因此将两个对象和ImageLoader imageLoader = ImageLoader.getInstance();
放在onCreate()
中。它将解决您的问题。