当前位置 : 主页 > 手机开发 > android >

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

来源:互联网 收集:自由互联 发布时间:2021-06-11
我有一个 Android应用程序从Web上提取图像. 但是,当我设置: supports-screens android:anyDensity="true" / 图库似乎设置为支持的最低分辨率. 这是画廊布局定义: ?xml version="1.0" encoding="utf-8" ?Linea
我有一个 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);

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

尝试

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>
网友评论