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

2024-07-01

我有一个 Android 应用程序,可以从网络上获取图像。 但是当我设置时:

<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);

有没有办法检测所使用的分辨率,然后调整图库中图像的大小,以便它们拉伸到屏幕的宽度?


try

i.setAdjustViewBounds(true);

另外,您不应该使用硬编码的像素值(在i.setLayoutParams(new Gallery.LayoutParams(400, 300));)

use

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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android - 根据设备分辨率调整图库中的图像大小 的相关文章

随机推荐