0

So I'm trying to put an image inside a scrollview and have the image stretch to fit different sized screens. It'll stretch horizontally, but vertically it always comes out messed up.

I've read so many pages of people with similar problems, but their solutions never work for me. I've tried every different combination possible using different scaleType,layout_width and layout_height, along with trying changing other things around. The closest I've gotten to getting it to work is shown in the code below, using scaleType="centerCrop". It's the only one that stretches the image vertically in ratio, but it cuts off a big chunk of the image from the top and bottom, it'll scroll up and down the image, but only the middle part shows.

The image is a 480x5500 jpeg, if that matters. Originally before I started messing with all that, the app worked just fine on my phone, but then later I realized the image was crunched when I tried it on a tablet. There's gotta be a way to do this in the xml right? I really don't want to have to do things with the image in the java code part. Also I'm hoping to be able to do this using just one image, I don't want to have use a different image for different screen sizes. Please help, thanks.

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/paper" />
</ScrollView>    
bcsb1001
  • 2,834
  • 3
  • 24
  • 35

3 Answers3

0

may be this help you, make both height and width wrap_content of ImageView

  android:layout_width="wrap_content"
  android:layout_height="wrap_content"

and no need ScrollView here.

Jignesh Ansodariya
  • 12,583
  • 24
  • 81
  • 113
0

Try using custom ImageView like AspectRatioImageView mentioned here:

https://stackoverflow.com/a/4688335/944070

Community
  • 1
  • 1
madlymad
  • 6,367
  • 6
  • 37
  • 68
0

the next sample is more than what you ask for :

hotoview.sample

it fits the image to the screen , plus it allows to zoom in/out using pinching gestures.

android developer
  • 114,585
  • 152
  • 739
  • 1,270