Using setImageDrawable dynamically to set image in an ImageView

前端 未结 17 1824
借酒劲吻你
借酒劲吻你 2020-12-04 08:22

I am generating a string from database dynamically which has the same name of image in drawable folder.

Now I want to set that value for ImageView

17条回答
  •  误落风尘
    2020-12-04 08:57

    This works, at least in Android API 15

    ImageView = imgv;
    Resources res = getResources(); // need this to fetch the drawable
    Drawable draw = res.getDrawable( R.drawable.image_name_in_drawable );
    imgv.setImageDrawable(draw);
    

    You could use setImageResource(), but the documentation specifies that "does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup ... consider using setImageDrawable() or setImageBitmap()." as stated by chetto

提交回复
热议问题