修改Android幻灯片以接受R.drawable中的字符串数组输入?

时间:2015-03-09 04:12:47

标签: java android slideshow

这里有一段非常简单的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以接受字符串数组?

3 个答案:

答案 0 :(得分:1)

  

如何调整slideShow.setImageResource以接受字符串   阵列

作为images数组包含sdcard内的图像路径,imageIDs包含应用程序可绘制文件夹内的图像ID。

显示来自images的图片:

1。从sdcard中将图片作为文件读取:

File imgFile = new  File(index%images.length]);

2。使用Bitmap中的BitmapFactory.decodeFile获取imgFile

Bitmap imgBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

3. 调用ImageView的setImageBitmap以在ImageView中显示图片:

slideShow.setImageBitmap(imgBitmap);

答案 1 :(得分:1)

使用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);

答案 2 :(得分:1)

您需要使用以下方式将Drawables添加到slideShow:

for(int i = 0; i < images.length; i++) {
        Drawable myDrawable = new BitmapDrawable(getResources(), images[i]);
        slideShow.setImageDrawable(myDrawable);
    }