简体   繁体   English

如何以编程方式在android中的数组中添加可绘制的所有图像

[英]how to add drawable all images in array in android programmatically

I have a code that let me use images in drawable folder, but all the images are stored in array by name-我有一个代码可以让我在 drawable 文件夹中使用图像,但所有图像都按名称存储在数组中 -

I wanna load them programmatically by a function and store them in array我想通过函数以编程方式加载它们并将它们存储在数组中

this is the array :这是数组:

private int[] theBitmapIds = { R.drawable.elefant_001, R.drawable.elefant_002, R.drawable.elefant_003, R.drawable.elefant_004,
R.drawable.elefant_005, R.drawable.elefant_006, R.drawable.elefant_007,
R.drawable.elefant_008, R.drawable.elefant_009, R.drawable.elefant_010,
R.drawable.elefant_011, R.drawable.elefant_012, R.drawable.elefant_013,
R.drawable.elefant_014, R.drawable.elefant_015, R.drawable.elefant_016,
R.drawable.elefant_017, R.drawable.elefant_018, R.drawable.elefant_019,
R.drawable.elefant_020, R.drawable.elefant_021,R.drawable.elefant_022,
R.drawable.elefant_022, R.drawable.elefant_023,R.drawable.elefant_024,
R.drawable.elefant_025, R.drawable.elefant_026, R.drawable.elefant_027,
R.drawable.elefant_028, R.drawable.elefant_029, R.drawable.elefant_030,
R.drawable.elefant_031, R.drawable.elefant_032, R.drawable.elefant_033,
R.drawable.elefant_034, R.drawable.elefant_035, R.drawable.elefant_036,
R.drawable.elefant_037, R.drawable.elefant_038, R.drawable.elefant_039,
R.drawable.elefant_040};

I changed it by the function我通过功能改变了它

public int [] theBitmapIds( ) {
    Field[] ID_Fields = R.drawable.class.getFields();
    int[] theBitmapIds = new int[ID_Fields.length];
    for (int i = 0; i < ID_Fields.length; i++) {
        try {
            theBitmapIds[i] = ID_Fields[i].getInt( null );
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    return  theBitmapIds;
}

private int[] theBitmapIds=theBitmapIds( );

but when I call the array in other function, it still has nothing in it但是当我在其他函数中调用数组时,它仍然没有任何内容

public Bitmap getBitmap(int width, int height, int index) {
        Bitmap b = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        b.eraseColor(0xFFFFFFFF);
        Canvas c = new Canvas(b);
        Drawable d = getResources().getDrawable(theBitmapIds[index]);

        int margin = 1;
        int border = 1;
        Rect r = new Rect(margin, margin, width - margin, height - margin);

        int imageWidth = r.width() - (border * 2);
        int imageHeight = imageWidth * d.getIntrinsicHeight()
                / d.getIntrinsicWidth();
        if (imageHeight > r.height() - (border * 2)) {
            imageHeight = r.height() - (border * 2);
            imageWidth = imageHeight * d.getIntrinsicWidth()
                    / d.getIntrinsicHeight();
        }

I am confused especially I'm a noob in java.我很困惑,尤其是我是 Java 的菜鸟。 How I can make this code work by editing the function or by change this line of code:我如何通过编辑函数或更改这行代码来使此代码工作:

private int[] theBitmapIds=theBitmapIds( );

or或者

Drawable d = getResources().getDrawable(theBitmapIds[index]);

Someone help me pls!有人请帮助我!

You can use AssetManager to directly load assets with their paths.您可以使用AssetManager直接加载带有路径的资产。

You can get its instance by calling context.getAssets() .您可以通过调用context.getAssets()来获取它的实例。

With it's open() method, you'll be able to get an InputStream for that asset.使用open()方法,您将能够获得该资产的InputStream

Then you can use BitmapFactory.decodeStream() to convert it into Bitmap .然后您可以使用BitmapFactory.decodeStream()将其转换为Bitmap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM