什么是像素和缩放的像素之间的关系 [英] What's the relationship between pixels and scaled pixels

查看:438
本文介绍了什么是像素和缩放的像素之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Eclipse中的布局编辑器来嘲笑我的UI布局,然后我创建了code动态填充它,事情都显示为不同的大小。我用添加星图的XML看起来是这样的:

I used the layout editor in eclipse to mock up my ui layout and then I created the code to populate it dynamically and things are showing up as different sizes. The XML I use to add the star images looks like this:

<ImageView
android:src="@drawable/star_gold"
android:layout_height="22sp"
android:layout_width="22sp"
android:adjustViewBounds="true"
android:layout_marginLeft="2sp"
android:layout_marginRight="2sp" />

和Java code我用做同样的事情是这样的:

and the Java code I use to do the same thing looks like this:

ViewGroup.MarginLayoutParams layout = new ViewGroup.MarginLayoutParams(22, 22);
layout.setMargins(2, 0, 2, 0);
ImageView star = new ImageView(mContext);
star.setImageResource(R.drawable.star_gold);
star.setLayoutParams(layout);
star.setAdjustViewBounds(true);
holder.Stars.addView(star);

当我在G1上都排队上运行我的应用程序,看起来很好,但是当我在Droid上运行它,我通过Java加上明星们的那些我加入我的XML的一半大小。什么我需要做的,以确保一切尺度是否恰当?

When I run my app on the G1 everything lines up and looks fine, but when I run it on the Droid the stars that I add via Java are about half the size of the ones I add in my XML. What do I need to do to make sure everything scales appropriately?

编辑: 这些都是伟大的反应,但我需要知道如何从code做到这一点。当我把它在原始像素的大小,有没有什么办法来设置浸的大小?

These are great responses, but I need to know how to do it from code. When I set the size it's in raw pixels, is there any way to set the size in dip's?

推荐答案

在这里,我们走!我需要得到DisplayMetrics密度。

Here we go! I need to get the DisplayMetrics Density.

// Convert the dips to pixels
final float scale = getContext().getResources().getDisplayMetrics().density;
mGestureThreshold = (int) (GESTURE_THRESHOLD_DIP * scale + 0.5f);

感谢大家的指点我朝着正确的方向!

Thank you all for pointing me in the right direction!

这篇关于什么是像素和缩放的像素之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆