在 Android 应用程序中导入多个图像

Importing multiple images in Android application

如何在我的 Android 应用程序中导入多张图片?我可以为该束图像分配单个 id 以将其从可绘制对象中获取吗?给我一个方法。

目前我只能在行布局中多次显示一张图片。这是带来图像的代码行:

  placeholder = BitmapFactory.decodeResource(getResources(), R.drawable.picture2);  

我应该为 picture3、picture4 等输入多行吗?

我能想到的最简单的方法:

int ids[] = new int[] {
    R.drawable.picture2,
    R.drawable.picture3,
    R.drawable.picture4
};

Bitmap bitmaps[] = new Bitmap[ids.length];

for(int i = 0; i < ids.length; i += 1){
    bitmaps[i] = BitmapFactory.decodeResource(getResources(), ids[i]);  
}

您也可以将其重写为一个函数以提高模块化。

public Bitmap[] loadBitmaps(int ids[]){
    Bitmap bitmaps[] = new Bitmap[ids.length];

    for(int i = 0; i < ids.length; i += 1){
        bitmaps[i] = BitmapFactory.decodeResource(getResources(), ids[i]);  
    }

    return bitmaps;
}

代替 Drawable 将所有图像存储在 Assets 中,然后您可以通过传递名称来获取图像。在 activity.

中添加以下函数
private Bitmap getBitmapFromAsset(String paramString)
{
    Object localObject = getResources().getAssets();
    try
    {
        Bitmap ret = BitmapFactory.decodeStream(((AssetManager)localObject).open(paramString));
        return ret;
    }
    catch (IOException ex)
    {
        ex.printStackTrace();
    }
    return null;
}