Android - resize image in gallery based on device resolution

余生颓废 提交于 2019-12-23 19:28:51

问题


I have an Android app that pulls in images from the Web. However when I set:

<supports-screens android:anyDensity="true" />

the gallery appears to be set to the minimum supported resolution.

Here is the gallery layout definition:

<?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>

and the getView class:

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

Is there a way to detect the resolution used and then resize the images in the gallery so they stretch to the width of the screen?


回答1:


try

i.setAdjustViewBounds(true);

also, you shoudn't use hardcoded pixel values (in 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));

and in some xml file (under res/values)

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


来源:https://stackoverflow.com/questions/4724024/android-resize-image-in-gallery-based-on-device-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!