Android 像素和缩放像素有什么关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2000892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the relationship between pixels and scaled pixels
提问by CaseyB
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:
我在 eclipse 中使用布局编辑器来模拟我的 ui 布局,然后我创建了代码来动态填充它,并且东西显示为不同的大小。我用来添加星形图像的 XML 如下所示:
<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" />
and the Java code I use to do the same thing looks like this:
我用来做同样事情的 Java 代码如下所示:
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);
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?
当我在 G1 上运行我的应用程序时,一切都排列整齐并且看起来很好,但是当我在 Droid 上运行它时,我通过 Java 添加的星星大小大约是我在 XML 中添加的星星大小的一半。我需要做些什么来确保一切都适当地扩展?
Edit: 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?
编辑:这些都是很好的回应,但我需要知道如何从代码中做到这一点。当我设置大小时,它以原始像素为单位,有什么方法可以设置倾角的大小吗?
采纳答案by CaseyB
Here we go! I need to get the DisplayMetrics Density.
开始了!我需要获取 DisplayMetrics 密度。
// 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!
谢谢大家为我指出正确的方向!
回答by Dave Webb
Firstly, you should be using Density Independent Pixels - dip
or dp
- and not Scaled Pixels - sp
.
首先,您应该使用 Density Independent Pixels -dip
或dp
- 而不是 Scaled Pixels - sp
。
The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen. If your resources are sized in dip
they will get automatically scaled on a screens with different densities; this is why the stars specified in your XML look OK. Scaled Pixels are similar but should be used for text resources only since they also reflect text scaling options.
密度无关像素相当于 160 dpi 屏幕上的一个物理像素。如果您的资源被调整大小,dip
它们将在具有不同密度的屏幕上自动缩放;这就是为什么在您的 XML 中指定的星星看起来不错的原因。Scaled Pixels 类似,但应该仅用于文本资源,因为它们也反映了文本缩放选项。
Your problem is that your Java code is working in unscaled raw pixels which is why your stars look smaller. The Android documentation has instructions on how to convert from dip
s to pixels. In short, you need to look at the android.util.DisplayMetrics.density
field.
您的问题是您的 Java 代码在未缩放的原始像素中工作,这就是为什么您的星星看起来更小。Android 文档有关于如何从dip
s转换为 pixel 的说明。总之,你需要看android.util.DisplayMetrics.density
场。
回答by doug
Going back to the original question of how do you set text size in dips or scaled pixels in Java:
回到最初的问题,即如何在 Java 中以倾角或缩放像素设置文本大小:
text.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12f);
OR
或者
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f);
回答by Mark B
From the documentation:
从文档:
Scale-independent Pixels (sp) this is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and user's preference.
Scale-independent Pixels (sp) 这类似于 dp 单位,但它也由用户的字体大小首选项缩放。建议您在指定字体大小时使用此单位,这样它们将根据屏幕密度和用户偏好进行调整。
It sounds like what you want to use for font sizing, but I would use dip for things like margin/padding:
听起来你想用什么来调整字体大小,但我会用 dip 来做边距/填充之类的东西:
Density independent pixel (dip) A virtual pixel unit that applications can use in defining their UI, to express layout dimensions or position in a density-independent way.
密度无关像素 (dip) 应用程序可在定义其 UI 时使用的虚拟像素单元,以与密度无关的方式表达布局尺寸或位置。
回答by Adam Davis
Scaled pixels change according to the user set font size preferences.
缩放像素根据用户设置的字体大小首选项而变化。
Read about screen and resolution support.
阅读有关屏幕和分辨率支持的信息。
You'll also find some good information on dp and sp in this question:
您还会在这个问题中找到一些关于 dp 和 sp 的好信息: