Android系统。 dp不做他们的工作

时间:2012-04-09 15:20:58

标签: android dpi

  1. 新 - >用于测试问题的Android项目
  2. Test.java的代码

    package test.density.yeah;

    import android.app.Activity;
    import android.os.Bundle;
    
    public class TestActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }
    
  3. main.xml的代码

        

    <ImageButton
    android:background="@null"
    android:src="@drawable/icon"
    android:layout_width="200dp"
    android:layout_height="200dp">
    </ImageButton>
    
  4. 使用设置启动模拟器:

  5. 分辨率 - 480x800

    Avstracted LCD密度 - 190

    RESULT

    http://cs5961.userapi.com/u68152416/-3/y_ef134df2.jpg

    之后我开始使用这样的设置模拟器:

    分辨率 - 480x800

    Avstracted LCD密度 - 240

    RESULT

    http://cs5961.userapi.com/u68152416/-3/y_8b99507b.jpg

    第一次测试的仿真器设置与我的HTC HD2特性相匹配,其次与HTC Sensation XL相同。在他们上运行此测试应用程序时 这是同样的问题。 HD2(480x800,190dpi) - 小图片感觉(480x800,240dpi) - 巨幅图片

    那么为什么“密度无关像素”不起作用?

2 个答案:

答案 0 :(得分:4)

它工作正常,但你需要知道一些扭曲。

您正在使用View上的src属性。如果你这样做,视图将根据位图大小调整大小,你通常会有不同的版本用于各种密度,mdpi,hdpi,xhdpi等。它将忽略layout_height和layout_width。

如果希望View忽略位图大小并使用layout_width和layout_height值强制大小,则需要使用background属性,而不是src。

<ImageButton
    android:background="@drawable/icon"
    android:layout_width="200dp"
    android:layout_height="200dp">
</ImageButton>

答案 1 :(得分:1)

可能是图像缩放使您的结果出错,您可以在此处阅读有关图像缩放的更多信息:

http://thebigbyte.blogspot.com/2009/12/android-how-to-scale-image-in-imageview.html

您可以通过将ImageButton xml更改为

来测试结果
<ImageButton
android:background="#ffffff"
android:layout_width="200dp"
android:layout_height="200dp">
</ImageButton>