imageView.setImageResource(int resId)与imageView.setImageBitmap(Bitmap bitmap);

时间:2013-03-04 15:12:16

标签: android bitmap

图像文件完全相同。一旦它来自drawable,一个来自file system

这是代码。

         <ImageView
                android:scaleType="fitEnd"
                android:id="@+id/partner_logo"
                android:layout_width="51dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:contentDescription="@string/parter_logo"
                />

我想动态设置合作伙伴徽标,因此我从网络服务器加载ID,然后

使用imageView.setImageBitmap(Bitmap bitmap);

设置

使用ImageResource它看起来不错,正如我所期望的那样但在Bitmap情况下它太小了。

这里要比较的图片


徽标(黑色箭头点)是通过setBitmap设置的。它似乎没有扩大规模

enter image description here

-

徽标(读取箭头点)是通过setImageResource设置的。它看起来像我希望它看起来像。

enter image description here

所以问题是为什么这里有区别?在位图的情况下使用scaleType

1 个答案:

答案 0 :(得分:2)

因为setImageResource()会自动缩放您的图片。

如果您想使用setImageBitmap(),则需要将位图缩放到所需的尺寸,或使用保持纵横比的setAdjustViewBounds(true)

Bitmap.createScaledBitmap(yourbitmap, width, height, false);

http://developer.android.com/reference/android/graphics/Bitmap.html