我需要在 ViewPager 中设置图像列表。我的资产文件夹中有一个图像列表。我将图像分组在资产/图像等资产内的单独文件夹中;assets/images1,...... 现在我需要在 int[] 数组中获取图像的 id(例如 images1 单独)。我在 String[] 数组中获得了图像(images1)的名称。如何使用它们的名称获取字符串 [] 中图像的 id。
问问题
3252 次
5 回答
1
您必须将图像放在可绘制文件夹中才能访问它们。然后您可以使用此代码从图像的名称中获取图像的 id..
String yourImageName;
int resID = getResources().getIdentifier(yourImageName, "drawable", "com.example.yourpackegename.");
于 2013-09-19T07:17:03.057 回答
0
通常只为资源文件夹中的图像创建“id”,而不为资产文件夹中的图像创建“id”。
于 2013-10-21T10:58:46.817 回答
0
在项目的“res/”中创建一个名为“drawable”的文件夹。
假设您有像...这样的图像
res/drawable/image,image1,image2,...
在您的活动中,使用 R.drawable.image,R.drawable.image1,.... 调用它们
"Now I need to get the id of the images (say images1 alone) in int[] array"
您还可以将它们存储在数组中,也可以使用“R.drawable.YOUR_IMAGE_NAME”作为调用它们,
int[] images = {R.drawable.image,R.drawable.image1,R.drawable.image2,R.drawable.image3,....};
希望这可以帮助。
于 2013-09-19T07:27:35.643 回答
0
int[] array_images = {
R.drawable.image0,
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable........,
....
};
于 2013-09-19T07:33:36.637 回答
-1
如果您将图像保存在资产中,则必须使用资产管理器对其进行解码。
公共类 MainActivity 扩展 Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
InputStream is = null;
ImageView imageView = new ImageView(this);
((RelativeLayout)findViewById(R.id.parent)).addView(imageView);
try {
is = getAssets().open("ic_launcher.png");//name of image in your asset folder
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(bitmap);
// if you dont want bitmap you can use drawable
// Drawable d = Drawable.createFromStream(is, null);
//imageView.setImageDrawable(d);
}
}
否则将图像保存在 res 中的 drawable 文件夹中,并使用 R.drawable.imagename 使用它
于 2014-01-25T11:22:59.257 回答