How to draw a piece of Bitmap

by Nick » Tue, 10 Mar 2009 01:09:37 GMT

Sponsored Links
I'm creating a simple game for Android and faced with a problem: Idea
is to have a huge "map" image file and re-draw only small part of it
(320*480) depending on user actions.

I didn't find a better solution than using "public static Bitmap
createBitmap (Bitmap source, int x, int y, int width, int height)"
method and decided to try it (expected huge performance degradation),
but actually it's appeared that this method changes source bitmap and
when i call it second time source size is significantly reduced. For
instance this code:

int bitHeight = mBackgroundImage.getHeight();
int bitWidth = mBackgroundImage.getWidth();
Bitmap curMap = Bitmap.createBitmap(mBackgroundImage,
                0, mY, 320, 480);
canvas.drawBitmap(curMap, 0, 0, null);

gives 320*960 (initial source image size) size for the first time and
320*455 for the second call

I hope this is not a best solution and look forward to hearing a new
direction or at least clarification on createBitmap method.

p.s i'm not a native english speaker and i apologise for possible


How to draw a piece of Bitmap

by Ikon » Mon, 27 Apr 2009 06:33:40 GMT

 Nick, Did you figure this one out? I want to do the same.


Sponsored Links

How to draw a piece of Bitmap

by Nick » Mon, 27 Apr 2009 21:06:10 GMT

 Yes, I managed with this "problem".
I use specific drawBitmap method of Canvas class - public void
drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)
Rect rect = new Rect(0, mCanvasHeight - mY, mCanvasHeight,
mBackgroundHeight - mY);
Rect rect2 = new Rect(0, 0, mCanvasWidth, mCanvasHeight);
canvas.drawBitmap(mBackgroundImage, rect, rect2, null);
//increment mY each time you need and your picture would be "moving"

As for unexpected resizing - usual copy/paste :) - I used LunarLander
SDK example for my experiments and didn't notice this code:
  // don't forget to resize the background image
                mBackgroundImage = mBackgroundImage.createScaledBitmap
                        mBackgroundImage, width, height, true);

in setSurfaceSize method. So I actually worked with "updated" copy of
my background image in my code.

That's it.


How to draw a piece of Bitmap

by Streets Of Boston » Mon, 27 Apr 2009 22:22:17 GMT

 My app zooms in and out of pics and lets the user moved the (zoomed)
in pic around.
The key methods for mapping a sub-picture (the one the size of the
screen) is done by using the calls Matrix.setRectToRect and
Bitmap.createBitmap(Bitmap source, int x, int y, int width, int
height, Matrix m, boolean filter), with x=y=0 (letting the Matrix m do
all the translations), and 'width' and 'height' set to the size of the
full-sized image (letting the Matrix m do all the scaling).


Other Threads

1. how to let my shared library,, work

Hi, guys,

I built a shared library, which only contained a function
print_hello(char*), and and a program, which used the only function to
print "hello android" with android-toolchain.

I mkdir a new dir- /data/test and push and hello into
emulator, then execute the hello and output the string "hello
android". It is work good.

But after I move into other dir, say ./mylib, and set
LD_LIBRARY_PATH=/system/lib:/data/test/mylib, hello outputs nothing.

So, any guy knows why and how to make it work ?

Thanks in advance!


2. video playing

hi all.
when i play a video in my device. it return -4 in mediaplayer. but it playing 
well in simulation-SDK. who could tell me why? the audio and videodisplay in my 
device are ok. some other files could be played well.


3. ListView for XML data

4. orientation change crashes when ids are not unique

5. How can I import any widgets under into my app?

6. How to simulate HW key events?

7. android.os.ServiceManager