我是android的初学者,我有图片滑块但是源项目中的图片,如何从网址获取图片并在图片滑块中显示。
这是我的适配器:
public class adapter_image_slider extends PagerAdapter {
Context mContext;
public adapter_image_slider(Context context) {
this.mContext = context;
}
@Override
public int getCount() {
return sliderImagesId.length;
}
private int[] sliderImagesId = new int[]{
R.drawable.imghome1, R.drawable.imghome2, R.drawable.imghome3,
};
@Override
public boolean isViewFromObject(View v, Object obj) {
return v == ((ImageView) obj);
}
@Override
public Object instantiateItem(ViewGroup container, int i) {
ImageView mImageView = new ImageView(mContext);
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mImageView.setImageResource(sliderImagesId[i]);
((ViewPager) container).addView(mImageView, 0);
return mImageView;
}
@Override
public void destroyItem(ViewGroup container, int i, Object obj) {
((ViewPager) container).removeView((ImageView) obj);
}
}
这是我的main_activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_slider);
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewPageAndroid);
adapter_image_slider adapterView = new adapter_image_slider(this);
mViewPager.setAdapter(adapterView);
}
有人请帮忙......
答案 0 :(得分:0)
你可以使用Glide或Picasso
https://github.com/bumptech/glide
Glide是一款快速高效的Android开源媒体管理和图像加载框架,它将媒体解码,内存和磁盘缓存以及资源池包装成简单易用的界面。
https://github.com/square/picasso
Picasso:Android的强大图像下载和缓存库
答案 1 :(得分:-1)
您可以使用ArrayList ids创建构造函数。
public class ImagePagerAdapter extends PagerAdapter{
ArrayList<Integer> imageIds;
public ImagePagerAdapter(ArrayList<Integer> imageIds) {
this.imageIds = imageIds;
}
发送ID并使用它。
ArrayList<Integer> imageIds = new ArrayList<Integer>();
for(int i = 1 ; i < 15 ; i++) {
int id = getApplicationContext.getResources().getIdentifier("image" + i,"drawable" ,getApplicationContext.getPackageName() );
imageIds.add(id);
}
但是它不使用URL。只是从存储中读取