从URL在Android中加载的图像尺寸较小

3

我需要在我的Android应用程序中从URL加载图像。

为此,我在我的布局中创建了一个ImageView。以下是ImageView:

<ImageView
    android:id="@+id/MyImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_gravity="center_horizontal">          
</ImageView>

然后在我的代码中,我使用以下内容在此 ImageView 中加载图像

ImageView bmImage = (ImageView)this.findViewById(R.id.MyImage);
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
    InputStream in = new java.net.URL(urldisplay).openStream();
    mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
    Log.e("Error", e.getMessage());
    e.printStackTrace();
}

bmImage.setImageBitmap(mIcon11);

我的URL路径上的图片大小为320 X 50。但非常奇怪,当这张图片显示在ImageView中时,它的大小变得非常小,几乎是原来的一半。

我已经尝试了很多方法,但都没有解决。有人能帮忙吗?


1
这个 ImageView 在你的布局中的父级是什么?你尝试设置 ImageView 的 scaleType 属性了吗? - acj
2个回答

1

我认为问题在于您正在设置位图像素并期望设备像素。

尝试获取屏幕的密度,然后设置宽度/高度,使其相应地拉伸,就像这样:

ImageView bmImage = (ImageView)this.findViewById(R.id.MyImage);
String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

 bmImage.setImageBitmap(mIcon11);
 if(mIcon11 != null){
   int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mIcon11.getWidth(), this.getResources().getDisplayMetrics());
   int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mIcon11.getHeight(), this.getResources().getDisplayMetrics());

   bmImage.setMinimumWidth(width);
   bmImage.setMinimumHeight(height);
 }

请再帮我一下,我想把横幅居中显示在屏幕上,但是做不出来。虽然我已经设置了布局为center_horizontal,但横幅仍然和屏幕左边对齐。 - user669231
要居中,请确保适当使用重力和布局重力。 - albattran
请注意,我已经将重力和布局重力设置为“center_horizontal”,但仍然无法正常工作。<ImageView android:id="@+id/MyImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_gravity="center_horizontal"> - user669231

0
使用这个图片加载库,并确保在DisplayImageOptions的配置中包含imageScaleType(ImageScaleType.EXACTLY_STRETCHED)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接