Android - resize image in gallery based on device resolution

I have an Android app that retrieves images from the Internet. However, when I install:

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

the gallery has 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 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 determine the resolution used, and then resize the images in the gallery so that they stretch to the width of the screen?

+3
source share
1 answer

to try

 i.setAdjustViewBounds(true); 

also you do not use hard coded 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> 
+7
source

Source: https://habr.com/ru/post/949303/


All Articles