简体   繁体   中英

Pass drawable image in an ImageView variable

如何将图像从可绘制文件转换为图像视图,因为这将不起作用

ImageView image= R.drawable.pic;

Try this

ImageView imageview = findViewById(R.id.imageView);
imageview.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.pic));

Try this

ImageView image = new ImageView(this);
image.setImageResource(R.drawable.pic);

or

ImageView image = new ImageView(this);
image.setImageDrawable(getResources().getDrawable(R.drawable.pic));

Try this

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
         fab.setImageDrawable(getResources().getDrawable(R.drawable.pic,this.getTheme()));
     }else {
         fab.setImageDrawable(getResources().getDrawable(R.drawable.pic));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM