Drawing UI using density pixels

| | August 18, 2015

I have a question about proper ways to draw UI, since I’ve learned what I know from the internet and by experimenting I often dont code the “proper” way, instead I do it my own way, not knowing if it will have consequenses that Im unaware of. So, Is it “wrong” to draw your UI using density pixels?

For Example:

canvas.drawText(bullets, this.getWidth() - 134 * dp, 32 * dp, paint);

I try to use getWidth() and getHeight() as much as possible but sometimes I just need to use constants in my code to place stuff where I want it, is this ok as long as I use dp? or will this effect the layout on screens with different resolutions/performance? (Don’t have anything except my own to run the game on)

One Response to “Drawing UI using density pixels”

  1. I am assuming that you perhaps mean the scale? This is a perfectly valid solution!

    All of my android projects run with a “target resolution” and an “actual resolution”, and all of my UI elements scale appropriately.

    Let’s say my target resolution is at 960×540 and my actual resolution is 1920×1080 (aka my Galaxy S3). That means my scale is 2. I draw an object at 50,50 and my handler will scale that from the target resolution to the actual resolution, putting it at 100,100 on the screen.

Leave a Reply