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

标签 android image gallery resolution

我有一个 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/

相关文章:

jquery - 使用 jQTouch 照片库扩展轻拂/滑动的不仅仅是图像

Android 获取对操作栏项目的 View 引用?

android - 特殊字符为上传文件的S3键名

javascript - 如何根据图像 map 上的可点击点创建扩展菜单?

angular - 如何处理本地文件夹中不存在的错误图像

html - 我怎样才能让这个网格不调整大小?

android - 如何在Android的Gallery中应用捏缩放?

c# - Androic C# : Using two different layout in the ViewPager

android - 错误 :(31, 0) 未找到 SDK 位置。苹果操作系统

html - 如何在 Underscore_S 中居中对齐我的标题图片