Android 从 setImageBitmap 缩放 imageview

标签 android android-imageview android-image

我有这个代码:

<ImageView
android:id="@+id/listitem_logo"
android:layout_width="match_parent"                                   
android:layout_height="wrap_content" />

imageview_logo.setImageBitmap(bit); // comes from assets
imageview_logo.setAdjustViewBounds(true);        
imageview_logo.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview_logo.setBackgroundColor(0x00000000); 
imageview_logo.setPadding(0, 0, 0, 0);
imageview_logo.setVisibility(v.VISIBLE);

以这种方式加载图像时,似乎没有进行缩放。但是,如果我通过 setImageDrawable() r.res. 加载图像,ImageView 中的图像会调整大小。但是,我需要使用 setImageBitmap(),因为我是通过 Assets 文件夹加载图像的。

我是不是遗漏了一些设置?这将使 Android 调整位图的大小和缩放位图,以便它使用 ImageView 的整个宽度?我想我可以自己用代码来完成,但我认为只要设置一些属性就可以支持我想做的事情。

最佳答案

不能信任系统的一切,特别是 android,它有时表现得非常不同。 您可以调整位图的大小。

height = (Screenwidth*originalHeight)/originalWidth;

这将生成适当的高度。 如您所述,宽度等于屏幕宽度。

Bitmap pq=Bitmap.createScaledBitmap(pq,Screenwidth,height, true);

关于Android 从 setImageBitmap 缩放 imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16749044/

相关文章:

android - 无法使用 android 将 react-native 更新到 0.59.2

android - 在 Kubernetes 上运行多个 docker-android 容器 (adb)

android - 无法将 TouchImageView 与 ViewPager 一起使用

android - ImageView scaleType ="fitXY"在 Cardview 内的 Lollipop 前设备中不起作用 |安卓

Android 文件提供程序非法参数异常

java - 更改 TextView 样式 Android 应用程序

java - 反射库在 Android 中返回空列表

android - 将布局参数设置为 ImageView 时出错

android - 应用程序模块之间的图像处理

java - 将图像序列化为字符串