在android中动态加载来自res的图像

时间:2017-05-27 22:02:10

标签: android loading android-image

我正在创建一个显示图像的漫画阅读器应用程序和一个透明按钮单击移动到下一个图像我不想进入存储和/或现在从互联网加载所以我保持简单和愚蠢(我知道)并将所有图像存储在res / drawable中有超过1000个图像,因此按名称加载它们并将它们全部输入似乎很荒谬它们是标签t1 t2 t3 t ...我正在使用

ImageView image = (ImageView) findViewById(R.id.MainComic);
image.setImageResource(R.drawable.t1);

在一个使用的开关和在按钮点击上添加的整数但是需要为整数输入1000多个案例才有办法

image.setImageResource(R.drawable.t(int));

我知道这不是那么简单,因为这是我先提前感谢的。

1 个答案:

答案 0 :(得分:0)

您可以使用Android上下文:

ImageView image = (ImageView) findViewById(R.id.MainComic);   
Context context = image.getContext();
    int resId = context.getResources().getIdentifier("t1", "drawable", context.getPackageName());
    image.setImageResource(resId);

或者你可以使用Class.forname("...")

的java instropection