views:

128

answers:

3

I know how to display an image that is bigger than the screen. That's fairly simple and explained in details here, however, this method makes you able to scroll as far as you want, event if you have left the image and you just have black screen. I would like to know how we can make the scrolling stop when we reach the side of the picture...

+1  A: 

I haven't developed on Android in a while but last time I played with it, I'm pretty sure that ScrollView wouldn't allow you to scroll past the end of the object(s) it enclosed.

If you've tried a scrollview, can you publish an example of how you're using it and I'll fire it up on my emulator

Basiclife
Well, yeah I thought about ScrollView, but I think it offers the opportunity to scroll only in one direction. So I would not be able to scroll diagonally for instance.
Sephy
Have a look here: http://blog.gorges.us/2010/06/android-two-dimensional-scrollview/ - Someone has made a 2d scroll view. Another one here: http://www.mail-archive.com/[email protected]/msg28442.htmlIt seems that you may hit a memory limit issue quite rapidly depending on the size of your image.An alternate method would be to have a blank canvas which you place inside a 2dScrollView - and then handle the rendering of the appropriate part of the image to the visible canvas
Basiclife
I'll have a look at that later and keep you posted.
Sephy
+2  A: 

Load your image into a WebView. Then you will get all the scroll behaviors you are looking for, and even the default zoom controls if you choose to turn them on...practically for free. Your only other option (to my knowledge) would be to used the 2D graphics APIs (Canvas, etc.) and create your own version of drawing tiles (like viewing a section of a map).

If your image is local, take a look at this example of reading local image data in from the SD Card. The cleaner approach to serving a local image in this case would be to create a ContentProvider and access the resource through a content:// URL in your WebView.

Example using the bundled image car.jpg in the assets directory of your project:

res/layout/main.xml

<?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">
    <WebView
      android:id="@+id/web"
      android:layout_width="150dip"
      android:layout_height="150dip"
    />
</LinearLayout>

src/ImageViewer.java

public class ImageViewer extends Activity {

  WebView webView;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    webView = (WebView)findViewById(R.id.web);
    webView.loadUrl("file:///android_asset/car.jpg");
  }
}

For an image bundled with your project, that's the simplest method. This does not work with images in your resources directory (like res/drawable). That path is not worth the code required to complete it.

Wireless Designs
Thanks for your answer, I'll try to take time to have a look at all of this tomorrow and come back with some feedback. thx guys
Sephy
Ok so, first issue now, I can't get this image to set in the Webview because either I put it as the background of the webview and the image gets resized or i try to load it from resources and I can't work it out. (I think it's an issue of path... I know I can do it from asset but i'll like it to come from res/drawable), and as I don't have the size of the screen, I don't know what to put for width and height in the img src
Sephy
Loading bundled image from either the filesystem or from assets is by FAR the simplest method when working with WebView (I edited with an example). This is because Android has built content serving into the AssetManager...and this doesn't exist for other resources. You would have to duplicate the behavior `AssetManager.openFd()` in a custom ContentProvider's `openFile()` method. That's a lot more code.
Wireless Designs
IS there a way to prevent the image from being resized to fit entirely in the screen and make it keep it's original size?
Sephy
If you load the content in a WebView, it should NEVER resize the data, that's the point of the suggestion. Unless you are setting it to the background of the view or some other property. I have retooled my answer with a recommended example using assets.
Wireless Designs
Nice, That's a good start. Thanks for your time Mr WD. here are your well earned points!
Sephy
A: 

In your Activity.onCreate():

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(...);

I was not sure which layout you exactly mean so I am posting 2 versions:

Version 1 (Buton below the image):

<?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:orientation="vertical">

    <ScrollView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="1"
        android:fadingEdge="none" android:scrollbars="none">

        <ImageView android:id="@+id/image" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:scaleType="center" />

    </ScrollView>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="OK"
        android:layout_gravity="center" />

</LinearLayout>

Version 2 (Button over the image - in z axis):

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

    <ScrollView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:fadingEdge="none"
        android:scrollbars="none">

        <ImageView android:id="@+id/image" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:scaleType="center" />

    </ScrollView>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="OK"
        android:layout_gravity="bottom|center_horizontal" />

</FrameLayout>

You can also try putting ScrollView inside a HorizontalScrollView.

radek-k