2011-05-26 38 views
0

我下了Android Hello-GridView tutorial,并想用图像的动态列表,从我RES /绘制的文件夹,而不是硬编码阵列的建议:获取图像的动态列表

// references to our images 
private Integer[] mThumbIds = { 
     R.drawable.sample_2, R.drawable.sample_3, 
     R.drawable.sample_4, R.drawable.sample_5, 
     R.drawable.sample_6, R.drawable.sample_7, 
     R.drawable.sample_0, R.drawable.sample_1 
}; 

我怎样才能循环和加载这些图像动态?

回答

0

更新:哎呀,我没有仔细阅读你的问题,因为我应该有。我不知道如何动态加载可绘制文件夹中的资源,而不是将其编码到数组中。

你好,

我在我的应用程序中做了类似的事情。

请看ImageAdapter类的getView方法。在代码的if (view == null)部分中,在else之前,使用ImageView的setter之一(例如setImageBitmap,setImageDrawable,setBackground等)。

我忘了提及您需要创建一个字符串数组来保存图像的文件路径。

在我的代码,这是我行:

imageView.setBackgroundDrawable(Drawable.createFromPath(MainActivity.imageStringArray[position]));

1

尝试使用assets文件夹,而不是这个定义它们的资源。只需将图像转储到res/assets下的文件夹(对于本示例,res/assets/images)即可。然后下面的代码应该得到该文件夹​​中的文件列表:

AssetManager assets = getAssets(); 
String[] drawables = assets.list("images"); 

然后,只需设置绘制的代码@Jack Smartie上面贴:

imageView.setBackgroundDrawable(Drawable.createFromPath(drawables[i])) 

其中“i”是你想要的drawable的索引。