0

My screen

If look very carefully you see that the bitmaps aren't in the middle of screen and I don't know what to do to put them in the middle of all kinds of screen with a space between them, that need to exchange according to screen width.

My Code:

int SPACEBETWEEN = 80;

for(int i = -1; i < 2; i++) { 

    canvas.drawBitmap(bmp,SPACEBETWEEN * i + canvas.getWidth()/2, getHeight()/2, null);
}

My width screen is 480

Sufian
  • 6,405
  • 16
  • 66
  • 120
Lucas Pugliese
  • 569
  • 1
  • 6
  • 12

1 Answers1

3

You're not negatively offsetting half the width of the bitmaps themselves. You need something like - bmp.getWidth()/2 somewhere in your formula (ditto for height).

JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
  • You won't believe, but the bmp.getWidth()/2 is equals to screen.getWidth()/2 – Lucas Pugliese Dec 07 '14 at 14:59
  • Not sure how you're creating your bitmaps -- feel free to show us -- but you can decode them from a file with a definite resolution. If you're creating the bitmaps from a separate canvas, this answer (http://stackoverflow.com/questions/4013725/converting-a-canvas-into-bitmap-image-in-android) shows you how to set the resolution of the resulting bitmap. – JASON G PETERSON Dec 07 '14 at 15:06
  • I am getting the bmp with this: bmp = BitmapFactory.decodeResource(getResources(), R.drawable.small_empty_ball); – Lucas Pugliese Dec 07 '14 at 15:09
  • Hmmm . . . click on the images in your drawables folder and check the resolution. Perhaps they need downscaling/cropping. Perhaps you're storing them with a lot of padding. Some automatic scaling will occur according to this answer (http://stackoverflow.com/questions/23047748/why-does-bitmapfactory-decoderesource-scale-my-image) unless you put them in a no-dpi folder, which is typically *not* what you want to do. – JASON G PETERSON Dec 07 '14 at 15:20
  • 2
    Man I am so sorry for that, but let me tell you what was the small problem and how I solved it. The problem was that the image had a big transparency and the circle wasn't centralized. I just open the image in PSD, remove the exaggerated transparency, centralized the circle and subtract the "- bmp.getWidth()/2" in my formula how you told me. – Lucas Pugliese Dec 07 '14 at 16:12