在我的 android 应用程序中,我正在从 API 加载图像。但是在网格视图中加载图像后,滚动不流畅。我正在使用异步线程来获取图像,这样它就不会阻塞 UI。
现在有任何提高滚动性能的建议。
在我的 android 应用程序中,我正在从 API 加载图像。但是在网格视图中加载图像后,滚动不流畅。我正在使用异步线程来获取图像,这样它就不会阻塞 UI。
现在有任何提高滚动性能的建议。
在单独的线程中加载图像肯定会有所帮助,但是这里还有另一个重要的性能问题需要处理,那就是“视图可重用性”,一旦您在适配器中设置了图像,请确保您实际上是在重用提供的视图适配器的 getView 方法,并且每次调用该方法时都不会创建/膨胀新的 Layout(GridElement),这通常是导致加载所有图像后滚动变慢的原因,有几种模式可用于解决此问题,您应该阅读有关 ViewHolder 的信息,这是解决此问题的最常见且易于使用的模式...
希望这可以帮助...
问候
getView() 方法的代码 {{{
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final HashMap<String, String> song = songsList.get(position);
imageURL = song.get(VariablesList.TAG_ALBUM_IMAGE);
View v = null;
if (convertView != null)
v = convertView;
else
v = inflater.inflate(R.layout.gridlayout_item, parent, false);
final ImageView imageView = (ImageView) v.findViewById(R.id.icon_image);
final Thread startAlbum = new Thread() {
public void run() {
imageView.setImageBitmap(AlbumList
.LoadImagetoGridView(imageURL));
synchronized (this) {
this.notifyAll();
}
}
};
synchronized (startAlbum) {
startAlbum.start();
try {
startAlbum.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
imageView.setOnClickListener(new View.OnClickListener() {
// @Override
public void onClick(View v) {
albumUrl = new StringBuffer(resources.getString(R.string.songsListURL));
String albumIndex = song.get("id");
albumName = (song.get("name"));
imageURL = song.get(VariablesList.TAG_ALBUM_IMAGE);
SongsList albumList = new SongsList(imageURL, albumUrl,
albumName,albumIndex,resources);
Thread threadAlbumList = new Thread(albumList);
threadAlbumList.start();
synchronized (albumList) {
try {
albumList.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!NewMediaPlayer.mediaPlayer.isPlaying()) {
HashMap<String, String> playingSong = NewMediaPlayer.selectedSongs
.get(0);
if (playingSong != null) {
String url = playingSong.get("songUrl");
String songName = playingSong.get("songName");
if (songName != null)
{
NewMediaPlayer.songTitleLabel.setText(albumName
+ " - " + songName);
NewMediaPlayer.songTitle.setText(albumName+"-"+songName);
}
NewMediaPlayer.playSong(url);
}
}
}
});
TextView itemAlbumName = (TextView) v.findViewById(R.id.icon_text);
itemAlbumName.setText(song.get("name"));
itemAlbumName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
HashMap<String, String> song = songsList.get(position);
String songIndex = song.get("id");
String albumName = (song.get("name"));
Intent in = new Intent(context, SongListActivity.class);
in.putExtra("albumIndex", songIndex);
in.putExtra("albumName", albumName);
in.putExtra("AlbumImage", song.get(VariablesList.TAG_ALBUM_IMAGE));
context.startActivity(in);
}
});
return v;
}
}}}
与其花功夫使用通用图像加载器库。而且它的性能很好。在 onCreate 方法中 gridView.setAdapter(new ImageAdapter(this,ImagesArrayPath));
private static class ImageAdapter extends BaseAdapter {
private final String[] IMAGE_URLS ;
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageAdapter(Context context,String source[]) {
inflater = LayoutInflater.from(context);
IMAGE_URLS=source;
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ico_place_holder)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
}
@Override
public int getCount() {
return IMAGE_URLS.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ImageLoader.getInstance()
.displayImage(IMAGE_URLS[position], holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
});
return view;
}
}
static class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
愿这对你有所帮助。享受编码。