[英]Image loading small in size when loaded from URL in Android
我需要在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中时,大小变得非常小,几乎是宽度和高度的一半
我已经尝试了很多但没有解决方案。 有人可以帮忙吗?
我认为问题是你正在设置位图像素并期望设备像素
尝试获取屏幕的密度,然后设置宽度/高度,使其相应地拉伸,如下所示:
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);
}
使用此图像加载库并确保在DisplayImageOptions imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
包含此配置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.