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

问题描述 投票:0回答:2

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

这里他使用了可绘制图像,但现在我想使用服务器图像

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
android json android-asynctask
2个回答
5
投票

下面的代码和解释复制并改编自https://web.archive.org/web/20130920044503/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.java
FileCache.java
MemoryCache.java
Utils.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 );
}

© www.soinside.com 2019 - 2024. All rights reserved.