修改 Android 幻灯片以接受 R.drawable 中的字符串数组输入?
Modify Android Slideshow to accept String array input in R.drawable?
这里有一段很棒的简单 Android 幻灯片代码 http://moorandroid.blogspot.com/p/image-slide-show.html
如果使用 R.drawable,效果很好,但我有一组从 Cursor _DATA 获得的图像,需要知道如何转换
这些适合,因为它们在 String[] 数组中,而不是 Integer[]?
Integer[] imageIDs = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4
};
String[] images = {
"/storage/extSdCard/image1.jpg",
"/storage/extSdCard/image2.jpg",
"/storage/extSdCard/image3.jpg",
"/storage/extSdCard/image4.jpg"
};
....
private void animatedSlideShow() {
slideShow = (ImageView)findViewById(R.id.slider1);
slideShow.setImageResource(imageIDs[index%imageIDs.length]); //--- Need to modify
index ++;
Animation showImage = AnimationUtils.makeInAnimation(this,true);
slideShow.startAnimation(showImage);
}
然后我的问题是:如何调整 slideShow.setImageResource 以接受字符串数组?
how to I adapt the slideShow.setImageResource to accept a string
array?
因为 images
数组包含 sdcard 内的图像路径,imageIDs
包含应用程序可绘制文件夹内的图像 ID。
要显示来自 images
的图像:
1. 从sdcard读取图片作为文件:
File imgFile = new File(index%images.length]);
2. 使用 BitmapFactory.decodeFile
从 imgFile
获取 Bitmap
:
Bitmap imgBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
3.调用ImageView的setImageBitmap
在ImageView中显示图片:
slideShow.setImageBitmap(imgBitmap);
像这样使用TransitionDrawable:
Drawable[] imagesResources= new Drawable[imageIDs.length];
for(int drawableId : imageIDs){
imagesResources[0] = getBaseContext().getResources().getDrawable(imageId);
}
TransitionDrawable transition = new TransitionDrawable(layers);
slideShow.setImageDrawable(transition);
transition.startTransition(1500);
您需要使用这种方式将 Drawables 添加到 slideShow :
for(int i = 0; i < images.length; i++) {
Drawable myDrawable = new BitmapDrawable(getResources(), images[i]);
slideShow.setImageDrawable(myDrawable);
}
这里有一段很棒的简单 Android 幻灯片代码 http://moorandroid.blogspot.com/p/image-slide-show.html 如果使用 R.drawable,效果很好,但我有一组从 Cursor _DATA 获得的图像,需要知道如何转换 这些适合,因为它们在 String[] 数组中,而不是 Integer[]?
Integer[] imageIDs = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4
};
String[] images = {
"/storage/extSdCard/image1.jpg",
"/storage/extSdCard/image2.jpg",
"/storage/extSdCard/image3.jpg",
"/storage/extSdCard/image4.jpg"
};
....
private void animatedSlideShow() {
slideShow = (ImageView)findViewById(R.id.slider1);
slideShow.setImageResource(imageIDs[index%imageIDs.length]); //--- Need to modify
index ++;
Animation showImage = AnimationUtils.makeInAnimation(this,true);
slideShow.startAnimation(showImage);
}
然后我的问题是:如何调整 slideShow.setImageResource 以接受字符串数组?
how to I adapt the slideShow.setImageResource to accept a string array?
因为 images
数组包含 sdcard 内的图像路径,imageIDs
包含应用程序可绘制文件夹内的图像 ID。
要显示来自 images
的图像:
1. 从sdcard读取图片作为文件:
File imgFile = new File(index%images.length]);
2. 使用 BitmapFactory.decodeFile
从 imgFile
获取 Bitmap
:
Bitmap imgBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
3.调用ImageView的setImageBitmap
在ImageView中显示图片:
slideShow.setImageBitmap(imgBitmap);
像这样使用TransitionDrawable:
Drawable[] imagesResources= new Drawable[imageIDs.length];
for(int drawableId : imageIDs){
imagesResources[0] = getBaseContext().getResources().getDrawable(imageId);
}
TransitionDrawable transition = new TransitionDrawable(layers);
slideShow.setImageDrawable(transition);
transition.startTransition(1500);
您需要使用这种方式将 Drawables 添加到 slideShow :
for(int i = 0; i < images.length; i++) {
Drawable myDrawable = new BitmapDrawable(getResources(), images[i]);
slideShow.setImageDrawable(myDrawable);
}