How to create Drawable from resource

前端 未结 8 1599
臣服心动
臣服心动 2020-12-04 07:48

I have an image res/drawable/test.png (R.drawable.test).
I want to pass this image to a function which accepts Drawable, e.g. mButton.set

8条回答
  •  鱼传尺愫
    2020-12-04 08:33

    If you are trying to get the drawable from the view where the image is set as,

    ivshowing.setBackgroundResource(R.drawable.one);
    

    then the drawable will return only null value with the following code...

       Drawable drawable = (Drawable) ivshowing.getDrawable();
    

    So, it's better to set the image with the following code, if you wanna retrieve the drawable from a particular view.

     ivshowing.setImageResource(R.drawable.one);
    

    only then the drawable will we converted exactly.

提交回复
热议问题