我正在 xml 文件中创建一个 imageLyout,使用这段代码这段代码:
<com.manuelpeinado.imagelayout.ImageLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/image_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aaa"
custom:fit="both"
custom:image="@drawable/plan"
custom:imageHeight="1744"
custom:imageWidth="1150" >
</com.manuelpeinado.imagelayout.ImageLayout>
当我在 drawbale-hdpi 属性中输入图像时,我看到图像尺寸为:515 * 348 像素。
当我使用这段 java 代码时:
hauteurLayout = imageLayout.getHeight();
largeurLayout = imageLayout.getWidth() ;
我得到这个结果:407 * 601。
好吧,我对所有这些维度感到困惑,为什么它们不同并且代表相同的图像?
我需要在 map 上画一些东西,所以我需要知道我必须使用的正确尺寸,以便在准确的位置绘制对象。
这是图片(我使用的是 horizontalScreeView ):
最佳答案
要支持多种屏幕分辨率,您应该使用与密度无关的像素 (dip),也称为 dp。
这是一种从 px(取决于屏幕分辨率)缩放到 dp(在每个设备上都能很好地缩放)的缩放方法:
private final int px2dp(int px)
{
final float scale = getResources().getSystem().getDisplayMetrics().density;
return (int) (px * scale);
}
关于安卓 :The unit of imageLayout. getHeight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23822802/