简体   繁体   English

Android,图片和dpi

[英]Android, images and dpi

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. 我在Android模拟器中使用WVGA800外观,因此密度(hw.lcd.density)为240。我必须在活动视图的底部放置4个图像按钮(每英寸72像素)。 As the resolution is 480X800, I asummed that the width of image must be 120 pixels. 由于分辨率为480X800,我假设图像的宽度必须为120像素。 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. 问题是启动应用程序时,这3个按钮占据了整个宽度,第4个按钮没有位置...然后我在Fireworks中创建了分辨率为每英寸240像素,宽度为120px的按钮图像,但是问题仍然存在。 Could somebody explain how to make correct drawables (sizes and dpi) so that they can be displayed pixel in pixel on Android? 有人可以解释如何制作正确的可绘制对象(大小和dpi),以便可以在Android上以像素为单位显示它们。

If you put your images in res/drawable , Android assumes they're for 160dpi and scales them accordingly for different resolutions. 如果您将图片放置在res/drawable ,则Android会假定它们的分辨率为160dpi,并相应地缩放它们以适应不同的分辨率。

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. 您可以选择将按钮图像放到res/drawable-hdpi信号中,以表示它们的密度约为240dpi,或者放到res/drawable-nodpi以使它们无论当前屏幕密度如何都不会缩放。

You still reference them as @drawable/whatever in layouts, the engine picks the best match automatically. 您仍然将它们引用为@drawable/whatever在布局中是@drawable/whatever ,引擎都会自动选择最佳匹配项。

Disregard the image DPI entirely. 完全忽略图像DPI。 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. 我实际上建议创建一个NinePatch图像用作您的按钮背景。 You'll actually need a few to put into a StateListDrawable , as this is how the different focus states are defined (eg Pressed, Focused). 实际上,您实际上需要将一些内容放入StateListDrawable中 ,因为这是定义不同焦点状态(例如,Pressed,Focused)的方式。 Once you have your NinePatch, just create a LinearLayout like so: 有了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>

Set all the buttons to fill_parent and give them all an equal weight. 将所有按钮设置为fill_parentfill_parent赋予相等的权重。 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. 它们将拉伸以均匀地适合父级,并且您完全不必担心指定恒定的像素或Dip大小。 Also, it'll evenly split onto any Android device, no matter the resolution. 此外,无论分辨率如何,它都会平均分配到任何Android设备上。

The width is actually 320 pixels, so for 4 buttons across the entire screen set each to 80dip. 宽度实际上是320像素,因此对于整个屏幕上的4个按钮,每个按钮设置为80dip。 Android will then adjust your density independent pixels to the proper screen size of 480 pixels. 然后,Android会将您的密度无关像素调整为480像素的适当屏幕尺寸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM