gpt4 book ai didi

Android:Listview在滚动时显示错误的图像

转载 作者:IT老高 更新时间:2023-10-28 23:25:56 25 4
gpt4 key购买 nike

我正在使用通用图像加载器从互联网下载图像。有些图片被设置为头像,所以我将它们四舍五入,而其他图片则被拉伸(stretch)。问题是 ListView 在滚动时显示错误的图像,一段时间后它显示正确的图像。我已经在 stackoverflow 中搜索过类似的问题,但我无法让它正常工作。

我正在使用自定义适配器,getview代码和舍入和拉伸(stretch)图像的代码如下:

    @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();

}
}
});

}

最佳答案

试试这个例子 http://android.amberfog.com/?p=296

使用这个方法

public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}

并在getView()中获取类型如下

int type = getItemViewType(position);

我按照示例解决了我的问题

关于Android:Listview在滚动时显示错误的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19426558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com