Android - 根据设备分辨率在图库中调整图片大小

3
我有一个安卓应用程序,可以从互联网上拉取图片。然而当我设置以下内容时:
<supports-screens android:anyDensity="true" />

画廊似乎被设置为最低支持的分辨率。
以下是画廊布局定义:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout android:id="@+id/GalleryLayout"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="#000000"
              >

<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/gallery"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:spacing="1dip"
         android:gravity="center_vertical|center_horizontal|center"
/>

</LinearLayout>

getView 类:
ImageView i = new ImageView(mContext);
i.setImageDrawable(drawablesFromUrl[position]);
i.setLayoutParams(new Gallery.LayoutParams(400, 300));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);

有没有一种方法可以检测所使用的分辨率,然后调整图库中的图像大小,使其拉伸到屏幕宽度?
1个回答

7

尝试

i.setAdjustViewBounds(true);

此外,您不应该使用硬编码的像素值(在i.setLayoutParams(new Gallery.LayoutParams(400, 300));中)。请使用
int width = (int) getResources().getDimension(R.dimen.image_width)
int height = (int) getResources().getDimension(R.dimen.image_height)
i.setLayoutParams(new Gallery.LayoutParams(width, height));

在一些 XML 文件中(位于 res/values 目录下)

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <dimen name="image_height">400dp</dimen>
  <dimen name="image_width">300dp</dimen>
</resources>

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