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
170 views
in Technique[技术] by (71.8m points)

android - 将像素转换为dp(Converting pixels to dp)

I have created my application with the height and width given in pixels for a Pantech device whose resolution is 480x800.

(我创建了我的应用程序,其高度和宽度以像素为单位,用于分辨率为480x800的Pantech设备。)

I need to convert height and width for a G1 device.

(我需要转换G1设备的高度和宽度。)

I thought converting it into dp will solve the problem and provide same solution for both devices.

(我认为将其转换为dp将解决问题,并为两个设备提供相同的解决方案。)

Is there any easy way to convert pixels to dp?

(有没有简单的方法将像素转换为dp?)

Any suggestions?

(有什么建议?)

  ask by Indhu translate from so

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

1 Reply

0 votes
by (71.8m points)
// Converts 14 dip into its equivalent px
float dip = 14f;
Resources r = getResources();
float px = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    dip,
    r.getDisplayMetrics()
);

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

...