取得手机屏幕大小

取得手机屏幕大小<wbr style="line-height:25px"><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">DisplayMetrics取得画面宽高的方法</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 范例说明<br style="line-height:25px"> 在开发手机应用程序时,除了底层对API的掌握度之外,最重要的仍是对屏幕分辨率的概念,因各家手机厂商所采用的屏幕尺寸不同,<br style="line-height:25px"> userUI接口呈现及布局自然也各异。<br style="line-height:25px"> 尽管Android可设置为随着窗口大小调整缩放比例,但即便如此,<br style="line-height:25px"> 手机程序设计人员还是必须知道手机屏幕的边界,以避免缩放造成的布局(Layout)变形问题。这个范例非常的简短,只<br style="line-height:25px"> 需几行程序即可取得手机的分辨率,当中的关键则是DisplayMetrics类的应用。<br style="line-height:25px"> android.util底下的DisplayMetrics对象,记录了一些常用的信息,包含了显示信息、大小、维度、字体等等;<br style="line-height:25px"> 在使用时,请记得引用android.util.DisplayMetrics。<br style="line-height:25px"> 值得一提的是DisplayMetrics对象里的widthPixels及heightPixels字段为整数类型,<br style="line-height:25px"> 在以下的程序当中,并没有对其作字符串类型的转换,因为字符串连接运算符的缘故,所以输出strOpt为字符串。<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">packageirdc.ex03_05;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.app.Activity;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.os.Bundle;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.util.DisplayMetrics;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importandroid.widget.TextView;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">EX03_05</span><span style="color:#3366ff; line-height:25px">extendsActivity<br style="line-height:25px"> {privateTextViewmTextView01;<br style="line-height:25px"> /**Calledwhentheactivityisfirstcreated.*/<br style="line-height:25px"> @OverridepublicvoidonCreate(BundlesavedInstanceState)<br style="line-height:25px"> {<br style="line-height:25px"> super.onCreate(savedInstanceState);<br style="line-height:25px"> setContentView(R.layout.main);<br style="line-height:25px"></span><span style="color:#808080; line-height:25px">/*必须引用android.util.DisplayMetrics*/</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px"><wbr style="line-height:25px">DisplayMetricsdm=newDisplayMetrics();<br style="line-height:25px"> getWindowManager().getDefaultDisplay().getMetrics(dm);<wbr style="line-height:25px"></wbr></wbr></span><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">StringstrOpt="手机屏幕分辨率为:"+<br style="line-height:25px"><wbr style="line-height:25px">dm.</wbr></span><span style="color:#99cc00; line-height:25px">widthPixels</span><wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">+"×"+</span><wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">dm.</span><span style="color:#808000; line-height:25px">heightPixels</span><wbr style="line-height:25px"><span style="color:#0000ff; line-height:25px">;</span><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">mTextView01=(TextView)findViewById(R.id.myTextView01);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">mTextView01.setText(strOpt);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">扩展学习<br style="line-height:25px"></wbr></span><wbr style="line-height:25px">程序一开始所创建的DisplayMetrics对象(程序中的dm),不需要传递任何参数(构造时),<br style="line-height:25px"> 但在调用getWindowManager()之后,会取得现有的Activity的窗口句柄,<br style="line-height:25px"> 此时,调用getDefaultDisplay方法将取得的宽高维度存放于DisplayMetrics对象dm中,<br style="line-height:25px"> 而取得的宽高维度是以像素为单位(Pixel),"像素"所指的是"绝对像素"而非"相对像素"。</wbr></wbr></wbr></wbr></wbr></wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值