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)


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 960x540 and my actual resolution is 1920x1080 (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.

| improve this answer | |
  • \$\begingroup\$ ah, thank you :) Just wanted to make sure before I get to deep in to it \$\endgroup\$ – Green_qaue Jan 20 '14 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.