gpt4 book ai didi

android - 在 listView 中加载异步图片

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:30:43 24 4
gpt4 key购买 nike

我想在 ListView 中加载数据后从服务器加载图像。我知道这个问题存在很多主题,但我还没有找到解决方案...

所以这是我的代码:

//asyncTackClass for loadingpictures
public class LoadImagesThread extends AsyncTask<Bundle, Void, Bitmap> {
private ImageView view;
private Bitmap bm;
private Context context;
private final WeakReference<ImageView> imageViewReference;

private final String BUNDLE_URL = "url";
private final String BUNDLE_NAME = "name";
private final String BUNDLE_BM = "bm";

public LoadImagesThread(Context context, ImageView view) {
this.context=context;
imageViewReference = new WeakReference<ImageView>(view);
}

@Override
protected Bitmap doInBackground(Bundle... b) {

Bitmap bm =null;
if (StorageHelper.getBitmap(b[0].getString(BUNDLE_NAME)) != null) { // Check the sdcard
bm = StorageHelper.getBitmap(b[0].getString(BUNDLE_NAME));
Log.w("LoadImagesThread", "Get image from sdcard : "+b[0].getString(BUNDLE_NAME));
} else { // Check the server
bm = ServiceHelper.getBitmapFromURL(b[0].getString(BUNDLE_URL));
StorageHelper.saveBitmap(bm, b[0].getString(BUNDLE_NAME)); // Save image on sdcard
Log.w("LoadImagesThread", "Get image from server : "+b[0].getString(BUNDLE_NAME));
}

return bm;
}

@Override
protected void onPostExecute(final Bitmap bm) {
super.onPostExecute(bm);
if (bm != null){ //if bitmap exists...
view = imageViewReference.get();
// Fade out
Animation fadeOutAnimation = AnimationUtils.loadAnimation(context, R.anim.fadeoutimage);
fadeOutAnimation.setAnimationListener(new AnimationListener() {

public void onAnimationStart(Animation animation) {

}

public void onAnimationRepeat(Animation animation) {

}

public void onAnimationEnd(Animation animation) {
// Fade in
view.setImageBitmap(bm);
Animation fadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fadeinimage);
view.startAnimation(fadeInAnimation);
}
});

// Launch the fadeout
view.startAnimation(fadeOutAnimation);


}else{ //if not picture, display the default ressource
view.setImageResource(R.drawable.productcarre);
}

}

}

代码用于在ImageView中显示Bitmap

这是适配器:

public class ListViewShoplistStoresAdapter extends BaseAdapter {

private ArrayList<Shop> shopList;
private Activity activity;

private HashMap<Integer, ImageView> views;
private final String BUNDLE_URL = "url";
private final String BUNDLE_NAME = "name";
private final String BUNDLE_POS = "pos";
private final String BUNDLE_ID = "id";


public ListViewShoplistStoresAdapter(Activity activity, ArrayList<Shop> shopList) {
super();
this.activity = activity;
this.shopList = shopList;
this.views = new HashMap<Integer, ImageView>();
}

public int getCount() {
return shopList.size();
}

public Object getItem(int position) {
return shopList.get(position);
}

public long getItemId(int position) {
return shopList.get(position).getId();
}

private class ViewHolder {
public TextView store;
public TextView name;
public ImageView ImageStore;
}

public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();

if(convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.listviewshops, null);

view.store = (TextView) convertView.findViewById(R.id.store);
view.name = (TextView) convertView.findViewById(R.id.name);
view.ImageStore = (ImageView) convertView.findViewById(R.id.imgstore);

convertView.setTag(view);
}else {
view = (ViewHolder) convertView.getTag();
}

Typeface regular=Typeface.createFromAsset(activity.getAssets(), "fonts/RobotoRegular.ttf");
view.store.setTypeface(regular);

Typeface light=Typeface.createFromAsset(activity.getAssets(), "fonts/RobotoLight.ttf");
view.store.setTypeface(light);
Brand brand = StorageHelper.getBrand(activity, shopList.get(position).getBrandId());
if (brand == null) {
Log.e("SetShopInAdapter","Brand null");
Toast.makeText(activity, "Impossible d'afficher la liste de magasins", Toast.LENGTH_LONG).show();
} else {
view.store.setText(brand.getName());
view.name.setText(shopList.get(position).getName());
view.ImageStore.setImageResource(R.drawable.productcarre);
}

Bundle b = new Bundle();

//url of the pict
b.putString(BUNDLE_URL, ServiceHelper.getImageUrl("brand", brand.getName()));

// name of image
b.putString(BUNDLE_NAME, ServiceHelper.getCleanImageName(brand.getName()));

//position in the listView
b.putInt(BUNDLE_POS, position);

//id of the current object
b.putInt(BUNDLE_ID, brand.getId());

//put info in the map in order to display in the onPostExecute
if(views.get(position)==null){
views.put(position, view.ImageStore);
// launch thread
new LoadImagesThread(activity.getApplicationContext(), view.ImageStore).execute(b);
}

return convertView;

}

}

所以,当我使用 GridView 时,没有任何问题,但是当我使用 ListView 时,图像仅在第一个项目中发生变化!

示例:我想显示“汽车”、“房子”和“苹果”商品的产品图片。代码将启动线程,所有图像(汽车然后是房子,最后是苹果)将显示在第一个项目(汽车项目)中......房子和苹果虽然没有图像!!

你知道我该怎么做吗?

谢谢

最佳答案

SO 上有很多关于这个的内容..像这样的异步加载称为“延迟加载”

https://github.com/thest1/LazyList

用列表来完整实现这样一个过程

对于一般的加载图像,我推荐这个:

https://github.com/nostra13/Android-Universal-Image-Loader

关于android - 在 listView 中加载异步图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12933783/

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