Android:如何制作 Drawable 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3355220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:51:51  来源:igfitidea点击:

Android: How can I make a Drawable array?

android

提问by JoeyCK

How can I make an array that handles some of my images so that I can use it like this?:

我怎样才能制作一个处理我的一些图像的数组,以便我可以像这样使用它?:

ImageView.setImageResource(image[1]);

I hope I explained well...

我希望我解释得很好...

回答by Walter Mundt

To do that, you don't want an array of Drawable's, just an array of resource identifiers, because 'setImageResource' takes those identifiers. How about this:

要做到这一点,您不需要 Drawable 数组,只需要资源标识符数组,因为 'setImageResource' 需要这些标识符。这个怎么样:

int[] myImageList = new int[]{R.drawable.thingOne, R.drawable.thingTwo};
// later...
myImageView.setImageResource(myImageList[i]);

Or for a resizable version:

或者对于可调整大小的版本:

ArrayList<Integer> myImageList = new ArrayList<>();
myImageList.add(R.drawable.thingOne);
// later...
myImageView.setImageResource(myImageList.get(i));

回答by Bevor

First of all you have to get all drawable IDs of your pictures with the following method:

首先,您必须使用以下方法获取图片的所有可绘制 ID:

int android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)

For example, you could read all drawable IDs in a loop:

例如,您可以循环读取所有可绘制 ID:

int drawableId = resources.getIdentifier("picture01", "drawable", "pkg.of.your.java.files");
                                          picture02...
                                          picture03...

..and then save them into an Array of Bitmaps:

..然后将它们保存到位图数组中:

private Bitmap[] images;
images[i] = BitmapFactory.decodeResource(resources, drawableId);

Now you are able to use your images as array.

现在您可以将图像用作数组。

回答by Hamid

with respect of @Bevor, you can use getResources()method instead of android.content.res.Resourceslike this:

关于@Bevor,您可以使用getResources()方法而不是android.content.res.Resources这样:

ArrayList<Integer> imgArr = new ArrayList<Integer>();

    for(int i=0;i<5;i++){

        imgArr.add(getResources().getIdentifier("picture"+i, "drawable", "pkg.of.your.java.files"));
    }

Usage:

用法:

imageView.setImageResource((int)imgArr.get(3));

回答by Pankaj Talaviya

String[] arrDrawerItems = getResources().getStringArray(R.array.arrDrawerItems); // Your string title array

// You use below array to create your custom model like
TypedArray arrDrawerIcons = getResources().obtainTypedArray(R.array.arrDrawerIcons);

for(int i = 0; i < arrDrawerItems.length; i++) {
    drawerItemDataList.add(new DrawerItemModel(arrDrawerItems[i], arrDrawerIcons.getResourceId(i, -1), i == 0 ? true : false));
}
drawerItemAdapter = new DrawerItemAdapter(this, drawerItemDataList);
mDrawerList.setAdapter(drawerItemAdapter);