Android、图像和 dpi

发布于 2024-10-04 04:18:07 字数 276 浏览 10 评论 0原文

我在 Android 模拟器中使用 WVGA800 皮肤,因此密度(hw.lcd.密度)为 240。我必须在活动视图的底部放置 4 个图像按钮(每英寸 72 像素)。由于分辨率是480X800,我假设图像的宽度必须是120像素。问题是当应用程序启动时,3 个按钮占据所有宽度,并且没有地方容纳第 4 个按钮...然后我在 Fireworks 中创建了按钮图像,分辨率为每英寸 240 像素,宽度为 120 像素,但问题仍然存在。有人可以解释如何制作正确的可绘制对象(尺寸和 dpi),以便它们可以在 Android 上以像素为单位显示吗?

I am using WVGA800 skin in Android emulator, so the density (hw.lcd.density) is 240. I have to put 4 image buttons (72 pixels per inch) at the bottom of activities view. As the resolution is 480X800, I asummed that the width of image must be 120 pixels. The problem is when application is launched, the 3 buttons take all width and there is no place for 4th button... Then I created button image in Fireworks with resolution 240 pixels per inch and in 120px width, but problem remains. Could somebody explain how to make correct drawables (sizes and dpi) so that they can be displayed pixel in pixel on Android?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满地尘埃落定 2024-10-11 04:18:07

如果您将图像放入 res/drawable 中,Android 会假定它们的分辨率为 160dpi,并针对不同的分辨率进行相应的缩放。

您可以选择将按钮图像放入 res/drawable-hdpi 中,表明它们的密度约为 240dpi,或者将其放入 res/drawable-nodpi 中以制作它们无论当前屏幕密度如何,都不会缩放。

您仍然在布局中将它们引用为 @drawable/whatever,引擎会自动选择最佳匹配。

If you put your images in res/drawable, Android assumes they're for 160dpi and scales them accordingly for different resolutions.

Your options are either to put the button images into res/drawable-hdpi signaling that they're intended for densities around 240dpi, or into res/drawable-nodpi to make them never scale regardless of the current screen density.

You still reference them as @drawable/whatever in layouts, the engine picks the best match automatically.

扭转时空 2024-10-11 04:18:07

完全忽略图像 DPI。这无关紧要,您只需要知道像素大小即可。我实际上建议的是创建一个 NinePatch 图像用作您的按钮背景。实际上,您需要将一些内容放入 StateListDrawable 中,因为这就是定义不同焦点状态的方式(例如,按下、聚焦)。有了 NinePatch 后,只需创建一个 LinearLayout,如下所示:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 1"
        android:layout_weight="1"
     />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 2"
        android:layout_weight="1"
     />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 3"
        android:layout_weight="1"
     />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 4"
        android:layout_weight="1"
     />
</LinearLayout>

将所有按钮设置为 fill_parent 并赋予它们相同的权重。它们将拉伸以均匀地适合父级,并且您完全不必担心指定恒定的像素或深度大小。此外,无论分辨率如何,它都会均匀地分配到任何 Android 设备上。

Disregard the image DPI entirely. It's irrelevant, all you need to know is the pixel size. What I would actually suggest is to create a NinePatch image to use as your button background. You'll actually need a few to put into a StateListDrawable, as this is how the different focus states are defined (e.g. Pressed, Focused). Once you have your NinePatch, just create a LinearLayout like so:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 1"
        android:layout_weight="1"
     />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 2"
        android:layout_weight="1"
     />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 3"
        android:layout_weight="1"
     />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/my_nine_patch"
        android:text="button 4"
        android:layout_weight="1"
     />
</LinearLayout>

Set all the buttons to fill_parent and give them all an equal weight. They'll stretch to fit evenly to the parent, and you don't have to worry at all about specifying a constant pixel or dip size. Also, it'll evenly split onto any Android device, no matter the resolution.

满天都是小星星 2024-10-11 04:18:07

宽度实际上是 320 像素,因此对于整个屏幕上的 4 个按钮,每个按钮都设置为 80dip。然后,Android 会将您的密度独立像素调整为 480 像素的正确屏幕尺寸。

The width is actually 320 pixels, so for 4 buttons across the entire screen set each to 80dip. Android will then adjust your density independent pixels to the proper screen size of 480 pixels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文