-1

i'm having a little trouble with my android app and different screen sizes. I want my game to look the same on every screen but i uses static px-values to fit it on my phone (HTC one m7, 1080x1920, 468 dpi).

The Code for the Header looks like this:

canvas.drawBitmap(header, null, new Rect(0,0,1080,200), null);

Now i want it to fit on every Screen. I read some tutorials for using dp (is that what i want and need?) but none of them worked. Or am i just using the wrong calculations?

Or do i have to work with f.E. 1/7*width and so on...

Thanks for your time and help.

user2798381
  • 21
  • 1
  • 6
  • http://developer.android.com/guide/practices/screens_support.html – Simon Dec 29 '14 at 11:18
  • @Simon, that link is actually useless for what he wants to do, he's using the canvas. – Stephan Branczyk Dec 29 '14 at 11:19
  • @StephanBranczyk Why is it useless? I use DPs all the time in custom views in the onMeasure() methods and so on with the required pixel counts stored in class fields. Once you understand the system, it's easy. – Simon Dec 30 '14 at 09:58
  • He's making a game using the Canvas. Drawing on the Canvas is more intricate than making a custom view. And except for a FrameLayout to contain the Canvas in, the android layout system and most of the best practices that come with it do not really apply in his case. – Stephan Branczyk Dec 30 '14 at 14:50

2 Answers2

1

Either way, you can use dp (density independent pixels) or px, but measure everything yourself at run-time.

Since you're obviously not using the built-in layout classes of Android, you really do have to measure everything yourself.

And yes, by that I mean you have to "work with f.E. 1/7*width and so on...". Just do not assume what the width is, determine that width at run-time.

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
0

You can create an integer.xml in various values folders, and use the values (0,0,1080,200) according to your need. For e.g.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="leftwidth">0</integer>
<integer name="rightwidth">0</integer>
<integer name="topwidth">1080</integer>
<integer name="bottomwidth">200</integer>
</resources>

Now you can get these values by calling:

canvas.drawBitmap(header, null, new Rect(getResources().getInteger(R.integer.leftwidth),getResources().getInteger(R.integer.rightwidth),getResources().getInteger(R.integer.topwidth),getResources().getInteger(R.integer.bottomwidth)), null);
  • Why not? I have just shown him another approach for solving his prob. @StephanBranczyk – Partha Chakraborty Dec 29 '14 at 11:42
  • Hardcoding values for his specific phone won't work with phone screens of other dimensions. Furthermore, the value for the height can change depending on the status bar and the main navigation buttons. – Stephan Branczyk Dec 29 '14 at 18:22