2

i want my Bitmap that has the height of the screen and has a bigger width than the screen to be moved a little bit to the right or left if the user changes the desktop so he can see the entire image.

Here is my code that just works partially:

        int x = -1*(int)((xOffset*bmp.getWidth()));
        canvas.drawBitmap(bmp, x, 0, null);

thank you!

ihucos
  • 1,821
  • 3
  • 19
  • 30

1 Answers1

1

You get exact pixel values via the variables xPixels and yPixels in onOffsetsChanged(). See my answer here: android live wallpaper rescaling

For example, in onOffsetsChanged(), you can set a private class variable

mPixels = xPixels;

Then, in your draw routine

holder = getSurfaceHolder();
c = holder.lockCanvas();
c.translate(mPixels, 0f);
// c.drawBitmap ... goes here :-)

Typically, you will want a bitmap twice the width of your screen, with your artwork centered in the middle. For example: 320 pixel width screen -> 640 pixel width bitmap with "center" from 160 to 480. mPixels will be 0 at the left most launcher screen (of 5), -160 at the center, and -320 at the rightmost screen.

Community
  • 1
  • 1
George Freeman
  • 2,260
  • 1
  • 15
  • 22
  • no, it didnt work, when i go to the last screen there is still some black room... – ihucos Jul 05 '11 at 14:48
  • @nomoral I can only guess that you're making a subtle mistake...bitmap too small, wrong density, etc. Without being able to see your code, I can only suggest that you find an open source example that works, and compare it to what you have done. – George Freeman Jul 05 '11 at 15:09
  • @nomoral Yes, using xStep, xOffset, and getDesiredMinimumWidth, as explained here: http://stackoverflow.com/questions/5208203/android-live-wallpaper-rescaling/5347525 – George Freeman Jul 05 '11 at 18:03
  • ahh, i just cant put the pieces together. what must i exactly do with xStep, xOffset, and getDesiredMinimumWidth to get the correct x value for canvas.drawBitmap? – ihucos Jul 05 '11 at 19:42
  • @nomoral This is tricky stuff -- hard to get right without our friend Eclipse. I have expanded my answer above to show how I typically do things ... use xPixels to translate the canvas before drawing. You could compute xPixels manually, but you'd get the same result. – George Freeman Jul 05 '11 at 20:40