天堂向右,我依然向左

天下之大,虽离家千里,何处不可往!何事不可为!
生活之路,纵坎坷曲折,当奋斗不息,则精彩纷呈!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
屏幕的像素是根据DisplayMetrics类来获取的

具体的计算方法是  width = widthPixels * density;   height = heightPixels * density  (ps: widthPixels  和  heightPixels  ,density   都是从DisplayMetrics中获取的)。

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int widthPixels= dm.widthPixels;
int heightPixels= dm.heightPixels;
float density = dm.density;
int screenWidth = widthPixels * density ;
int screenHeight = heightPixels * density ;

在320*480 的phone 上的 到的 widthPixels 值是320, heightPixels  值是480, density 的值是1.0。

在480*800 的phone 上的到的idthPixels 值是320, heightPixels  值是533, density 的值是1.5。

注意: 此处DisplayMetrics 不要使用context.getApplicationContext().getResources().getDisplayMetrics();

 

打印信息:

800*480:

density=1.5
screen(w,h)=799,480
pixels(w,h)=533,320

480*320:

density=1.5
screen(w,h)=480,319
pixels(w,h)=320,213
posted on 2011-04-03 12:14  老舟  阅读(1543)  评论(1编辑  收藏  举报