0

I created some test assets for ldpi up to xxxhdpi and put them in there respective drawable folders. If I draw things at the corners of the screen say position 0,0 or 0+canvas.getWidth()-bitmap.getWidth()

or things like that they align properly across all device screens.

I'd like to be able to draw my bitmaps anywhere on screen and have them scale adjusted properly across all device sizes but I am just not getting it.

Here's a sample screen shot same code running on two emulators. enter image description here

The drawing code is the same. I was able to get the Paint to scale the line properly by adding a scale. The bitmaps also scale relative to their screen sizes but how do I draw at 50units in code and have it be 50units on all devices?

init function

Paint p = new Paint();
p.setColor(Color.RED);
p.setStyle(Paint.Style.FILL_AND_STROKE);
float scale = context.getResources().getDisplayMetrics().density;
p.setStrokeWidth(40*scale);

Bitmap ball = BitmapFactory.decodeResource(context.getResources(), R.drawable.ball);

render function

canvas.drawLine(0, 0, 400, 400, p);
canvas.drawBitmap(ball, 50, 50, null);
canvas.drawBitmap(ball2, 150, 150, null);

from the above screenshot you can see two things wrong. The length of the line, it's 400px long. Now that's an just a number I chose but it goes way too far on the smaller phone.

You also see how they are really far apart on one device but literally on top of each other on the second device.

What can to get past this issue?

fadden
  • 51,356
  • 5
  • 116
  • 166
user1610950
  • 1,837
  • 5
  • 33
  • 49

1 Answers1

0
  1. Looks like your images are not scaled properly in the drawable folders.

    According to this post xxhdpi images must be 4 times bigger than ldpi images i.e. 400x400px (xxhdpi) and 100x100px (ldpi).

  2. The line goes from (0,0) all the way to (400, 400). The ldpi screen is only 240x320px, so the bottom right corner coordinate would be (240, 320). What you see is perfectly normal.

Community
  • 1
  • 1
Dmitri Timofti
  • 2,428
  • 1
  • 22
  • 25