Android Viewpager 从 SD 卡加载图像
Android Viewpager to load images from SD Card
伙计们,我正在使用以下自定义代码从资源中加载 20 张图像并显示在 viewpager 中
public class CustomPagerAdapter extends PagerAdapter {
int[] mResources = {
R.drawable.slide1,
R.drawable.slide2,
R.drawable.slide3,
R.drawable.slide4,
R.drawable.slide5,
R.drawable.slide6,
R.drawable.slide7,
R.drawable.slide8,
R.drawable.slide9,
R.drawable.slide10,
R.drawable.slide11,
R.drawable.slide12,
R.drawable.slide13,
R.drawable.slide14,
R.drawable.slide15,
R.drawable.slide16,
R.drawable.slide17,
R.drawable.slide18,
R.drawable.slide19,
R.drawable.slide20,
};
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
这很好用,但我想将 jpg 文件放在设备上的一个目录中,这样就可以在不重新编译应用程序的情况下更改它们
我想我需要将图像放入 mResource 数组中。我可以得到路径但不确定代码应该是什么格式而不是使用可绘制的线
我已经阅读了这里的文章,但 none 对我来说很有意义,我对此很陌生,代码看起来与我正在使用的代码完全不同
谁能指出我正确的方向?
非常感谢任何帮助
马克
是的,你当然可以这样做。我将尝试逐步向您解释该过程,
步骤 1
有一个指向路径的 File
对象,例如,
File directory = new File("path-to-directory");
确保路径是包含图像的目录,
步骤 2
使用listFiles()
方法列出目录中的所有文件,如
File[] allImages = directory.listFiles();
现在您拥有所有文件的数组,就像 int[] mResources
一样。唯一的区别是,现在你有实际的文件引用,而以前你有资源 ID。
步骤 3
您可以像以前一样在 ViewPager 中显示图像。但这是一个 有点棘手 并且可能需要您花费大量时间和代码才能从文件中正确显示图像。
您还需要注意缓存,以便当您再次加载之前加载的图像时,它会从缓存中获取它。
要做到这一切,我推荐你使用这个库(Google 推荐),Glide。
设置一张图片,一行代码,
Glide.with(context).from(file).into(imageView);
就是这样。现在您的图像显示在设备目录中的 ViewPager 中。
伙计们,我正在使用以下自定义代码从资源中加载 20 张图像并显示在 viewpager 中
public class CustomPagerAdapter extends PagerAdapter {
int[] mResources = {
R.drawable.slide1,
R.drawable.slide2,
R.drawable.slide3,
R.drawable.slide4,
R.drawable.slide5,
R.drawable.slide6,
R.drawable.slide7,
R.drawable.slide8,
R.drawable.slide9,
R.drawable.slide10,
R.drawable.slide11,
R.drawable.slide12,
R.drawable.slide13,
R.drawable.slide14,
R.drawable.slide15,
R.drawable.slide16,
R.drawable.slide17,
R.drawable.slide18,
R.drawable.slide19,
R.drawable.slide20,
};
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mResources.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_item, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
}
这很好用,但我想将 jpg 文件放在设备上的一个目录中,这样就可以在不重新编译应用程序的情况下更改它们
我想我需要将图像放入 mResource 数组中。我可以得到路径但不确定代码应该是什么格式而不是使用可绘制的线
我已经阅读了这里的文章,但 none 对我来说很有意义,我对此很陌生,代码看起来与我正在使用的代码完全不同
谁能指出我正确的方向?
非常感谢任何帮助
马克
是的,你当然可以这样做。我将尝试逐步向您解释该过程,
步骤 1
有一个指向路径的 File
对象,例如,
File directory = new File("path-to-directory");
确保路径是包含图像的目录,
步骤 2
使用listFiles()
方法列出目录中的所有文件,如
File[] allImages = directory.listFiles();
现在您拥有所有文件的数组,就像 int[] mResources
一样。唯一的区别是,现在你有实际的文件引用,而以前你有资源 ID。
步骤 3
您可以像以前一样在 ViewPager 中显示图像。但这是一个 有点棘手 并且可能需要您花费大量时间和代码才能从文件中正确显示图像。
您还需要注意缓存,以便当您再次加载之前加载的图像时,它会从缓存中获取它。
要做到这一切,我推荐你使用这个库(Google 推荐),Glide。
设置一张图片,一行代码,
Glide.with(context).from(file).into(imageView);
就是这样。现在您的图像显示在设备目录中的 ViewPager 中。