0

I need to display a very big image (8000x8000 for example) in a view allowing the user to zoom in and out.

I have checked several options for detecting the user touches and transform the image according to that. For example:

How to use Multi-touch in Android

And others using gesture/touch detectors, etc.

The problem is that none of them takes care of the Bitmap size and the posible crashes because the Bitmap doesn't fit in the memory.

So what I'm looking for is how to implement that like the Android's Gallery does. Without lose quality when the image is zoomed in and of course without crashes

Any ideas? Complete, working answers will be bountried with up to 300 points depending on the complexity and quality of the answer

Addev
  • 31,819
  • 51
  • 183
  • 302
  • You can try [Android source](http://stackoverflow.com/a/9728899/942821) (4.0). –  May 18 '12 at 13:31
  • 1
    http://stackoverflow.com/questions/4996470/load-large-image-from-server-on-android http://stackoverflow.com/questions/7834132/loading-big-image-to-bitmap-in-android These links may help you!! – Thiru VT May 18 '12 at 13:23

2 Answers2

2

Try showing only a scaled subset of the bitmap and decode as few as possible. I managed something similar to this. These are the code snippets which helped me a lot:

To calculate all values you need (e.g. scale factor, number of pixels etc.) you can use inJustDecodeBounds to get the size of the bitmap without allocating any memory.:

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opt);
int width = opt.outWidth;
int height = opt.outHeight;

To decode only a subset of a bitmap use this:

Bitmap.createBitmap(
    source, 
    xCoordinateOfFirstPixel, 
    yCoordinateOfFirstPixel, 
    xNumberOfPixels, 
    yNumberOfPixels
);

To create a scaled bitmap:

Bitmap.createScaledBitmap(
    source, 
    dstWidth, 
    dstHeight, 
    filter
);

To draw a subset of a bitmap:

Canvas.drawBitmap(
    source, 
    new Rect(
        subsetLeft, 
        subsetTop, 
        subsetRight, 
        subsetBottom
    ), 
    new Rect(0,0,dstWidth, dstHeight), 
        paint
);

Edit: I forget to mentioned this snipplet for creating scaled images. To save memory this is what you want:

BitmapFactory.Options opt = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap scaledBitmap = BitmapFactory.decodeFile(path, opt);

Since inSampleSize must be an integer I used createScaledBitmap to adjust the bitmap a bit more.

Xazen
  • 822
  • 2
  • 12
  • 26
1

http://www.anddev.org/large_image_scrolling_using_low_level_touch_events-t11182.html

Possibly?

the sites down at the moment, so it may not even be relevant to you in the end, but i thought i should post it.

also

" ZOOM CONTROL (Widget) listen to the OnTouch event to handle the panning "

finally:

https://github.com/MikeOrtiz/TouchImageView

^^ might be EXACTLY what your looking for as far as i know 2.0+

failing that, a webview and load the file like that.

Broak
  • 4,161
  • 4
  • 31
  • 54
  • Thanks for your answer. I'll check later for the first link, but the second one detects the touches and rescales the image, but doesn't care about the memory management. You can check trying to load a really big image – Addev May 18 '12 at 12:55