方式一
如果图片命名规范
private fun getImageRes(imgIndex: Int): Int {
return resources.getIdentifier("img_$imgIndex", "drawable", packageName)
}
调用
imageView.setImageResource(getImageRes(1))
方式二
如果图片命名不规范
private val drawables = arrayOf(R.drawable.img_0, R.drawable.img_1, R.drawable.img_2)
private fun getImageRes(imgIndex: Int): Int {
return drawables[imgIndex]
}
调用
imageView.setImageResource(getImageRes(1))