[英]Modify Android Slideshow to accept String array input in R.drawable?
http://moorandroid.blogspot.com/p/image-slide-show.html上有一个很棒的简单Android幻灯片代码,如果使用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以接受字符串数组?
如何调整slideShow.setImageResource以接受字符串数组?
由于images
数组包含在sdcard内的图像路径,而imageIDs
包含在应用程序可绘制文件夹内的图像ID。
要显示图像中的images
:
1.从sdcard读取图像作为文件:
File imgFile = new File(index%images.length]);
2.使用imgFile
BitmapFactory.decodeFile
获取Bitmap
:
Bitmap imgBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
3.调用ImageView的setImageBitmap
在ImageView中显示图像:
slideShow.setImageBitmap(imgBitmap);
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.