服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android 获得屏幕宽高的三种方式

Android 获得屏幕宽高的三种方式

2021-04-19 16:02Android开发网 Android

这篇文章主要介绍了Android 获得屏幕宽高的三种方式的相关资料,需要的朋友可以参考下

Android 获得屏幕宽高的三种方式

老风格,废话不多说了,直接给大家贴android获取屏幕宽高的代码了。

主要代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.km.screeninfo;
  import android.os.bundle;
  import android.support.v.app.fragment;
  import android.support.v.app.fragmentactivity;
  import android.util.displaymetrics;
  import android.view.layoutinflater;
  import android.view.menu;
  import android.view.menuitem;
 import android.view.view;
 import android.view.viewgroup;
 import android.widget.textview;
 public class mainactivity extends fragmentactivity {
   @override
   protected void oncreate(bundle savedinstancestate) {
     super.oncreate(savedinstancestate);
     setcontentview(r.layout.activity_main);
     if (savedinstancestate == null) {
       getsupportfragmentmanager().begintransaction().add(r.id.container, new placeholderfragment()).commit();
     }
   }
   /**
    * a placeholder fragment containing a simple view.
    */
   public static class placeholderfragment extends fragment {
     private textview tvmodel;
     private textview tvscreenheight;
     private textview tvscreenheight;
     private textview tvscreenheight;
     private textview tvscreenwidth;
     private textview tvscreenwidth;
     private textview tvscreenwidth;
     private textview tvscreendensity;
     private textview tvscreendensitydpi;
     private textview tvscreendensity;
     private textview tvscreendensitydpi;
     private textview tvscreenwidth;
     private textview tvscreenheight;
     public placeholderfragment() {
     }
     @override
     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {
       view rootview = inflater.inflate(r.layout.fragment_main, container, false);
       tvmodel = (textview) rootview.findviewbyid(r.id.tv_model);
       tvscreenheight = (textview) rootview.findviewbyid(r.id.tv_screen_height);
       tvscreenheight = (textview) rootview.findviewbyid(r.id.tv_screen_height);
       tvscreenheight = (textview) rootview.findviewbyid(r.id.tv_screen_height);
       tvscreenheight = (textview) rootview.findviewbyid(r.id.tv_screen_height);
       tvscreenwidth = (textview) rootview.findviewbyid(r.id.tv_screen_width);
       tvscreenwidth = (textview) rootview.findviewbyid(r.id.tv_screen_width);
       tvscreenwidth = (textview) rootview.findviewbyid(r.id.tv_screen_width);
       tvscreenwidth = (textview) rootview.findviewbyid(r.id.tv_screen_width);
       tvscreendensity = (textview) rootview.findviewbyid(r.id.tv_screen_density);
       tvscreendensitydpi = (textview) rootview.findviewbyid(r.id.tv_screen_densitydpi);
       tvscreendensity = (textview) rootview.findviewbyid(r.id.tv_screen_density);
       tvscreendensitydpi = (textview) rootview.findviewbyid(r.id.tv_screen_densitydpi);
       tvmodel.settext(android.os.build.model);
       // 获取屏幕密度(方法)
       int screenwidth = getactivity().getwindowmanager().getdefaultdisplay().getwidth(); // 屏幕宽(像素,如:px)
       int screenheight = getactivity().getwindowmanager().getdefaultdisplay().getheight(); // 屏幕高(像素,如:p)
       tvscreenheight.settext(screenheight + " px");
       tvscreenwidth.settext(screenwidth + " px");
       // 获取屏幕密度(方法)
       displaymetrics dm = getresources().getdisplaymetrics();
       float density = dm.density; // 屏幕密度(像素比例:./././.)
       int densitydpi = dm.densitydpi; // 屏幕密度(每寸像素:///)
       float xdpi = dm.xdpi;
       float ydpi = dm.ydpi;
       int screenwidth = dm.widthpixels; // 屏幕宽(像素,如:px)
       int screenheight = dm.heightpixels; // 屏幕高(像素,如:px)
       tvscreenheight.settext(screenheight + " px");
       tvscreenwidth.settext(screenwidth + " px");
       tvscreendensity.settext(density + "");
       tvscreendensitydpi.settext(densitydpi + "");
       // 获取屏幕密度(方法)
       displaymetrics dm = new displaymetrics();
       getactivity().getwindowmanager().getdefaultdisplay().getmetrics(dm);
       density = dm.density; // 屏幕密度(像素比例:./././.)
       densitydpi = dm.densitydpi; // 屏幕密度(每寸像素:///)
       xdpi = dm.xdpi;
       ydpi = dm.ydpi;
       tvscreendensity.settext(density + "");
       tvscreendensitydpi.settext(densitydpi + "");
       int screenwidth = dm.widthpixels; // 屏幕宽(px,如:px)
       int screenheight = dm.heightpixels; // 屏幕高(px,如:px)
       tvscreenheight.settext(screenheight + " px");
       tvscreenwidth.settext(screenwidth + " px");
       float screenwidthdip = (dm.widthpixels / density); // 屏幕宽(dip,如:dip)
       float screenheightdip = (dm.heightpixels / density); // 屏幕宽(dip,如:dip)
       tvscreenheight.settext(screenheightdip + " dip");
       tvscreenwidth.settext(screenwidthdip + " dip");
       return rootview;
     }
   }
 }

 activity_main.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.km.screeninfo.mainactivity"
  tools:ignore="mergerootframe" />
 <scrollview xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   tools:context="com.km.screeninfo.mainactivity$placeholderfragment" >
   <linearlayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
    android:orientation="vertical" >
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="手机型号:"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_model"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginbottom="dip"
      android:layout_margintop="dip"
      android:text="方法一:getdefaultdisplay().geth/w()"
      android:textcolor="#aaaa"
      android:textsize="sp" />
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:layout_margintop="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕高(一):"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕宽(一):"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_width"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginbottom="dip"
      android:layout_margintop="dip"
      android:text="方法二:getresources().getdisplaymetrics().h/w"
      android:textcolor="#aaaa"
      android:textsize="sp" />
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:layout_margintop="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕高(二):"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕宽(二):"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_width"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:layout_margintop="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="density:"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_density"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginleft="dip"
        android:text="densitydpi:"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_densitydpi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginbottom="dip"
      android:layout_margintop="dip"
      android:text="方法三:getdefaultdisplay().getmetrics(displaymetrics).h/w"
      android:textcolor="#aaaa"
      android:textsize="sp" />
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:layout_margintop="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕高(三):"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕宽(三):"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_width"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:layout_margintop="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="density:"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_density"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginleft="dip"
        android:text="densitydpi:"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_densitydpi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginbottom="dip"
      android:layout_margintop="dip"
      android:text="屏幕宽高(px/density)"
      android:textcolor="#aaaa"
      android:textsize="sp" />
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:layout_margintop="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕高:"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:orientation="horizontal" >
      <textview
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="屏幕宽:"
        android:textstyle="bold" />
      <textview
        android:id="@+id/tv_screen_width"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </linearlayout>
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginbottom="dip"
      android:layout_margintop="dip"
      android:text="科普:"
      android:textcolor="#aaaa"
      android:textsize="sp" />
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:text="density = densitydpi/" />
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:text="px(pixels) = dip * (densitydpi/) = dip*density" />
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="dip"
      android:text="dip(device independent pixels) = dp " />
    <textview
      android:layout_width="wrap_content"
      android:layout_margin="dip"
      android:layout_height="wrap_content"
      android:text="dip = (px * )/densitydpi = px / density" />
    <textview
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="dip" />
  </linearlayout>
</scrollview>

ps:在 activity 中获得屏幕的宽和高

?
1
2
int srceenw = this.getwindowmanager().getdefaultdisplay().getwidth();
int screenh = this.getwindowmanager().getdefaultdisplay().getheight();

如果需要适应不同分辨率的屏幕,则按照以下方式操作:

准备多套不同的图片:

分别放hdpi、mdpi、ldp文件夹中;

摆放控件的时候要得到屏幕宽高度,然后在程序中设定控件的百分乘屏幕的宽和高。

延伸 · 阅读

精彩推荐