2

In my app I want to place a .png file and I want it to be viewed as a scrolled image at both landscape and portrait modes, please, suggest a code or example....

3 Answers 3

7

To make your Imageview scroll if it doesn't fit in height, you can next a ImageView inside a ScrollView in the xml, & add this parameter -

android:adjustViewBounds="true"

Here's an example -

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/back" >
    </ImageView>
</ScrollView>
3

A simple solution is to scroll a container which contains an ImageView much bigger than the container:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/Container"
        android:layout_width="200dp"
        android:layout_height="200dp" >

        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="480dp"
            android:layout_height="800dp"
            android:src="@drawable/sky_bgr" >
        </ImageView>
    </LinearLayout>

</LinearLayout>

And then use code to scroll it:

public class StartActivity extends Activity {
    private LinearLayout container;   
    private int currentX;   
    private int currentY; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        container = (LinearLayout) findViewById(R.id.Container);
        container.scrollTo(220, 400);
    }

    @Override  
    public boolean onTouchEvent(MotionEvent event) { 
      switch (event.getAction()) { 
          case MotionEvent.ACTION_DOWN: { 
              currentX = (int) event.getRawX(); 
              currentY = (int) event.getRawY(); 
              break; 
          } 

          case MotionEvent.ACTION_MOVE: { 
              int x2 = (int) event.getRawX(); 
              int y2 = (int) event.getRawY(); 
              container.scrollBy(currentX - x2 , currentY - y2); 
              currentX = x2; 
              currentY = y2; 
              break; 
          }    
          case MotionEvent.ACTION_UP: { 
              break; 
          } 
      } 
        return true;  
    } 
}

Here many improvements can be made such as limit the scrolling range etc... The other way is to control ImageView's matrix... Then you can load an image into a bitmap and draw a portion of it on canvas etc

-2

Try this:

<ScrollView>
<HorizontalScrollView>
<ImageView/>
</HorizontalScrollView>
</ScrollView>
1
  • 1
    Good idea in theory but one scroll blocks another - you cannot sroll both x and y at the same time on 2.2
    – Lumis
    Feb 14, 2011 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.