Android screen size inconsistency

时间:2017-04-10 02:49:32

标签: android android-studio screen

As seen in this image: enter image description here

I have a rounded rectangle that is the size of 1082 x 1796, and I used getWindowManager().getDefaultDisplay() to get and print the pixel values of the screen as seen in the circle bottom left. However, when I draw this bitmap, this happens: enter image description here

Is the screen size different from the getDefaultDisplay() method? How do I fix this?

2 个答案:

答案 0 :(得分:0)

我认为您的形状被系统视为9patch。它使用第一行和最后一行/列来了解如何调整图像大小以便一致地显示它。

这就是为什么宽度和高度存在2px的差异。我认为您可以通过修改png并删除第一行和最后一行/列来纠正它,或者只是使用其他方式绘制图像。也许使用另一种格式(jpg?bmp?)来完成这项工作。我不确定。

答案 1 :(得分:0)

您可以通过计算来实现,以根据设备屏幕的宽度和高度调整所有屏幕设备:

DisplayMetrics dm = new DisplayMetrics();
                       ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);

int width = dm.widthPixels;
int height = width * imageHeight / imageWidth;

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);