我有一个 Android 应用程序可以从 Web 中提取图像。 但是当我设置时:
<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);
有没有一种方法可以检测使用的分辨率,然后调整图库中图像的大小,使它们拉伸(stretch)到屏幕的宽度?
最佳答案
尝试
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>
关于Android - 根据设备分辨率调整图库中图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4724024/