Redundant margins when adding ImageView to ScrollView in Android.

Posted by Shmuel Meymann on Stack Overflow See other posts from Stack Overflow or by Shmuel Meymann
Published on 2010-06-08T08:50:44Z Indexed on 2010/06/08 8:52 UTC
Read the original article Hit count: 359

Filed under:
|
|

Hi.. I have been trying to use a ScrollView on a single ImageView with a JPG (~770 x 1024) over an AVD that's 600x800.

My main.xml is:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
<ScrollView 
android:id="@+id/scroller"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/>
</LinearLayout>

Now, I add a single ImageView with

setContentView(R.layout.main);
ScrollView sv = (ScrollView)findViewById( R.id.scroller );
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( "/sdcard/770x1024.jpg" ) ); // same happens with ScaleDrawable.
iv.setScaleType( ScaleType.CENTER_INSIDE );
sv.addView( sv ); // and it does not go any better if I use Linear Layout between the ScrollView and the ImageView.

The result is The image was displayed in a middle of a ScrollView, wrapped with background area on top and bottom as following:

##### 
#####
image
.
.
.
#####
#####

Where ##### stands for background area I tried to set the background of the ImageView red, and it verified that the blank margins were ImageView background.

iv.setBackgroundColor( color.Red );

Where I would expect the image to take no more than its size (scaled to the AVD size) and I expect the ScrollView to let me scroll over the remainder (if any).

For some reason, I see that the drawable size is 600x1024.

Moreover I tried to add a LinearLayout with a dummy text view such as the linear layout is a parent to the ImageView and the TextView, and the ScrollView is a parent to the LinearLayout.

LinearLayout dummy = new LinearLayout( this );
dummy.addView(iv);
TextView someTextView = new TextView( this );
someTextView.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ));
dummy.addView( someTextView );
sv.addView( dummy );

The result was very peculiar: The entire layout was set into the width of a text-less text view (19).

It is important for me to avoid stretching the image. What is the recommended way to implement a display of a page that can be potentially scrolled? Do I have to do it manually with a plain layout and scrolling upon OnMove events?

Thanks

Shmuel

© Stack Overflow or respective owner

Related posts about android

Related posts about imageview