I am using this tutorial: http://manishkpr.webheavens.com/android-viewpager-as-image-slide-gallery-swipe-gallery/
here he has used drawable images, but now i want to use 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);
}
}
Still i am using Images path like this:
R.drawable.one,
R.drawable.two,
R.drawable.three
But now i want to use Images path like below:
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
The code and explanation below are copied and adapted from https://web.archive.org/web/20130920044503/http://www.androidhive.info/2012/07/android-loading-image-from-url-http/
Just create the Array of string which contains the urls as
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 };
After creating array just copy the files ImageLoader.java
,FileCache.java
,MemoryCache.java
and Utils.java
in your application.
And after that in your adapter class do as following.
@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 );
}