我正在使用通用图像加载器从互联网下载图像。有些图片被设置为头像,所以我将它们四舍五入,而其他图片则被拉伸。问题是列表视图在滚动时显示错误的图像,一段时间后它显示正确的图像。我已经在 stackoverflow 上搜索了类似的问题,但我无法让它正常工作。
我正在使用自定义适配器,getview 代码和舍入和拉伸图像的代码如下:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if(view == null)
{
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.whatsnew_listitem, null);
holder = new ViewHolder();
holder.playButton = (ImageButton) view.findViewById(R.id.btn_playVideo);
holder.mediaThumbnail = (ImageView) view.findViewById(R.id.imageview_mediaImage);
holder.avatar = (ImageView) view.findViewById(R.id.imageview_avatar);
holder.artistName = (TextView) view.findViewById(R.id.textview_artistName);
holder.timestamp = (TextView) view.findViewById(R.id.textview_timestamp);
holder.status = (TextView) view.findViewById(R.id.textview_title);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
pos = position;
Item item = items.get(position);
if(item.getArtistName() != "")
{
// set thumbnail
String thumbnailURL = "";
holder.mediaThumbnail.setVisibility(View.INVISIBLE);
if(item.getUpdateType() == Config.UPDATE_TYPE_1)
{
holder.playButton.setVisibility(View.GONE);
thumbnailURL = item.getPictureUri();
if(thumbnailURL != null)
{
ImageLoader.getInstance().loadImage(thumbnailURL, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
setImage(loadedImage, imageView, Config.MEDIA);
mediaImages[position] = true;
imageView.setVisibility(View.VISIBLE);
}
});
}
holder.status.setText(item.getMessage());
}
else if(item.getUpdateType() == Config.UPDATE_TYPE_2)
{
//play button
holder.playButton.setVisibility(View.VISIBLE);
//set media image
thumbnailURL = item.getVideoThumbnailUri();
if(thumbnailURL != null)
{
ImageLoader.getInstance().loadImage(thumbnailURL, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
setImage(loadedImage, imageView, Config.MEDIA);
mediaImages[position] = true;
imageView.setVisibility(View.VISIBLE);
}
});
}
//set youtube status
holder.status.setText(item.getTitle());
holder.playButton.bringToFront();
holder.playButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(item.getVideoId()!= null) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Config.YOUTUBE_URI + item.getVideoId() ));
context.startActivity(intent);
}
}
});
}
//set avatar
String avatarURL = item.getAvatarImageUri();
holder.avatar.setVisibility(View.INVISIBLE);
if(avatarURL != null)// && avatarImages[position] == false)
{
ImageLoader.getInstance().loadImage(avatarURL, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
setImage(loadedImage, imageView, Config.AVATAR);
avatarImages[position] = true;
imageView.setVisibility(View.VISIBLE);
}
});
}
//set artist name
holder.artistName.setText(item.getArtistName());
//set timestamp
if(timerList[position] == false)
{
updateTime(holder.timestamp, item);
}
}
view.setTag(holder);
return view;
}
//@Override
public void setImage(final Bitmap bitmap, final ImageView imageView, final String imagetype) {
// TODO Auto-generated method stub
Activity activity = (Activity) context;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Bitmap croppedBmp = null;
int imageHeight = bitmap.getHeight();
int imageWidth = bitmap.getWidth();
if(imagetype == Config.MEDIA)
{
if(imageHeight <imageWidth) {
croppedBmp = Bitmap.createScaledBitmap( bitmap, dp, dp, true);
imageView.setImageBitmap(croppedBmp);
}
}
else if(imagetype == Config.AVATAR)
{
int radius;
if(imageWidth > imageHeight)
{
croppedBmp = Bitmap.createBitmap(imageHeight, imageHeight, Bitmap.Config.ARGB_4444);
radius = imageHeight/2;
}
else
{
croppedBmp = Bitmap.createBitmap(imageWidth, imageWidth, Bitmap.Config.ARGB_4444);
radius = imageWidth/2;
}
Canvas canvas = new Canvas(croppedBmp);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, imageWidth, imageHeight);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(radius, radius, radius, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
imageView.setImageBitmap(croppedBmp);
ImageLoader.getInstance().displayImage(uri, imageView, listener)
notifyDataSetChanged();
}
}
});
}