Android - resize image in gallery based on device resolution
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_X开发者_运维问答Y);
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?
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>
精彩评论