简体   繁体   中英

How to find the pixel width and height of device in Android, when not in Activity

I have just started to learn some Android development and need to find a way to get the pixel height and width from a class that isnt Activity. In my example this Class is called BouncingBall . I want to find out the screen size so I can change the speed of the balls to make it look like they are bouncing on the screen walls!

Thanks for advice!

Try this:

WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
    Display display = window.getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();

where you pass context from constructor. or use getApplicationContext() :)

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