ImageView的setScaleType不工作 [英] ImageView setScaleType not working

查看:187
本文介绍了ImageView的setScaleType不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageView的,我想扩展 CENTER_CROP 。如果我设置图像src和规模型XML它的伟大工程。不过,我需要设置code中的形象。下面无法正确缩放。相反,中心裁剪它拉伸图像以填充的宽度。

I have an ImageView and I want to to scale CENTER_CROP. If I set the image src and scale type in XML it works great. But I need to set the image in code. The following fails to scale correctly. Instead of center cropping it stretches the image to fill the width.

imageView.setBackgroundResource(R.drawable.my_image);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

我试图重新排序这些行,我试图离开了 setScaleType ,留下了对XML。无论是帮助。

I tried reordering these lines and I tried leaving out the setScaleType and leaving that to the XML. Neither helps.

推荐答案

setBackgroundResource 设置的ImageView的背景,使这是正常的行为,绵延...

setBackgroundResource sets imageView's background so it is normal behaviour that stretches...

使用 setImageResource 相反,它会设置的ImageView源,因此它会跟着你scaleType定义。

Use setImageResource instead, it will set imageview's source so it will follow your scaleType definition.

这篇关于ImageView的setScaleType不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆