gpt4 book ai didi

android - 如何在客户端使用 URL 从服务器下载多个图像

转载 作者:行者123 更新时间:2023-11-29 14:50:55 24 4
gpt4 key购买 nike

我正在使用本教程: http://manishkpr.webheavens.com/android-viewpager-as-image-slide-gallery-swipe-gallery/

这里他用的是drawable images,现在我想用Server Images

ImageAdapter.java:

public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}

我仍然使用这样的图片路径:

R.drawable.one,
R.drawable.two,
R.drawable.three

但现在我想使用如下的图片路径:

http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo1.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo2.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo3.png

最佳答案

您无需使用引用 http://www.androidhive.info/2012/07/android-loading-image-from-url-http/ 执行任何操作.

只需创建包含 url 的字符串数组

    String[] imagUrl={
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo1.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo2.png,
http://manishkpr.webheavens.com/wp-content/uploads/2012/10/bloglogo3.png };

创建数组后,只需复制文件 ImageLoader.javaFileCache.javaMemoryCache.javaUtils.java 在您的应用程序中。

然后在您的适配器类中执行以下操作。

@Override
public Object instantiateItem(ViewGroup container, int position) {
// Imageview to show
ImageView imageView = new ImageView(context);
// ImageLoader class instance
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
// Loader image - will be shown before loading image
int loader = R.drawable.loader;
// whenever you want to load an image from url
// call DisplayImage function
// url - image url to load
// loader - loader image, will be displayed before getting image
// image - ImageView
imgLoader.DisplayImage(imagUrl[position], loader, imageView );
}

关于android - 如何在客户端使用 URL 从服务器下载多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18146717/

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