Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

android - 如何在Android中以像素为单位获取屏幕尺寸(How to get screen dimensions as pixels in Android)

I created some custom elements, and I want to programmatically place them to the upper right corner ( n pixels from the top edge and m pixels from the right edge).

(我创建了一些自定义元素,我想以编程方式将它们放置在右上角(顶部边缘n像素,右侧边缘m像素)。)

Therefore I need to get the screen width and screen height and then set position:

(因此,我需要获取屏幕宽度和屏幕高度,然后设置位置:)

int px = screenWidth - m;
int py = screenHeight - n;

How do I get screenWidth and screenHeight in the main Activity?

(如何在主活动中获取screenWidthscreenHeight ?)

  ask by Niko Gamulin translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want the display dimensions in pixels you can use getSize :

(如果要以像素为单位显示尺寸,可以使用getSize :)

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

If you're not in an Activity you can get the default Display via WINDOW_SERVICE :

(如果您不在Activity ,则可以通过WINDOW_SERVICE获取默认的Display :)

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

If you are in a fragment and want to acomplish this just use Activity.WindowManager (in Xamarin.Android) or getActivity().getWindowManager() (in java).

(如果您在一个片段中并且想要完成此操作,请使用Activity.WindowManager(在Xamarin.Android中)或getActivity()。getWindowManager()(在Java中)。)

Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:

(在引入getSize之前(在API级别13中),可以使用现已弃用的getWidthgetHeight方法:)

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

For the use case you're describing however, a margin/padding in the layout seems more appropriate.

(但是,对于您正在描述的用例,布局中的边距/填充似乎更合适。)

Another way is: DisplayMetrics

(另一种方法是: DisplayMetrics)

A structure describing general information about a display, such as its size, density, and font scaling.

(描述有关显示器的一般信息的结构,例如其大小,密度和字体缩放。)

To access the DisplayMetrics members, initialize an object like this:

(要访问DisplayMetrics成员,请像这样初始化一个对象:)

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

We can use widthPixels to get information for:

(我们可以使用widthPixels获取以下信息:)

"The absolute width of the display in pixels."

(“显示的绝对宽度,以像素为单位。”)

Example:

(例:)

Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...