我是Android Studio上的初学者,而java,我想创建一个随机的视图图片背景,我尝试使用这个How To Display Random images on image view,但不能正常工作,此代码不会生成任何形象 请有人帮我解决此代码或创建另一个随机背景代码。
我使用此代码生成图像
final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.rnd_images);
final ImageView img = (ImageView) findViewById(R.id.imgRandom);
// I have 3 images named img_0 to img_2, so...
final String str = "img_" + rnd.nextInt(2);
img.setImageDrawable
(
getResources().getDrawable(getResourceID(str, "drawable",
getApplicationContext()))
);
}
protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
final int ResourceID =
ctx.getResources().getIdentifier(resName, resType,
ctx.getApplicationInfo().packageName);
if (ResourceID == 0)
{
throw new IllegalArgumentException
(
"No resource string found with name " + resName
);
}
else
{
return ResourceID;
}
}
}