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. ALSA channels

when i use the code below.
status_t ALSAStreamOps::channelCount(int channels) {
    int err;
    if (!mHandle)
        return NO_INIT;
    err = snd_pcm_hw_params_set_channels(mHandle, mHardwareParams,
    if (err < 0) {
        LOGE("Unable to set channel count to %i: %s",
            channels, snd_strerror(err));
        return BAD_VALUE;
    LOGD("Using %i %s for %s.",
        channels, channels == 1 ? "channel" : "channels", streamName());
    return NO_ERROR;

i want to set channel 1 when recoring audio. but the message of "Unable to
set channel count to %i: %s" will be print .who know why?


2. Eclipse Error: SWT on ubuntu (amd turion )64 bit

i've just tried to install SDK 1.5 and I have a big problem,
when I try to point the sdk directory(in Eclipse 3.3.2) from "Windows-
"...pingUsageServerfailed" in the second "An SWT erro has occured.We
are reccomended to exit the workbench. .....".
Now I've ridden some blogs and I've understood that the problem is in
SWT that doesn't support 64bit
What shall I do?
Please answer in a simple.


3. PingUsage Server failed

4. Questions on Tab (TabWidget) Width

5. Get Latitude/Longitude informations of an image

6. Hello World Tutorial Error Message

7. Handling key events in a paused Activity