简体   繁体   中英

Icons at exact coordinates on the screen in Android

i want to set the map of a city as the background of my app and i want to place some icons above it at known coordinates.

I managed to realize it on a 320x480 resolution by creating a class which extends a SurfaceView and implements Runnable; both the map and the icons are drawn at the run() method (through a canvas.drawBitmap()).

Obviously, everything comes out in a huge mess when i try to run my app on a different screen resolution.

Can you please give me some hints about how to draw the background image and make the icons keep their "place" regardless of the screen resolution?

Thanks a lot!

You need to implement density independent pixels.

private final float dpScale = 
    getContext().getResources().getDisplayMetrics().density;
private final float dp = (onePixel * dpScale +0.5f);   

put this is the top of your drawing code. and every time you draw something multiply your x and y coordinates with dp. example:

canvas.drawText("hello world", 100*dp, 150*dp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM