简体   繁体   English

Android:在具有相同密度但不同大小的屏幕上安装图像

[英]Android: Fitting images on screens with same densities but different size

I have a layout which looks like this: 我有一个看起来像这样的布局:

 <LinearLayout>
     <ImageView>
     android:width="wrap_content"
     android:height:"wrap_content"
      ...
     </ImageView>

     <ImageView>
     android:width="wrap_content"
     android:height:"wrap_content"
      ...
     </ImageView>

     <ImageView>
     android:width="wrap_content"
     android:height:"wrap_content"
      ...
     </ImageView>

     <ImageView>
     android:width="wrap_content"
     android:height:"wrap_content"
      ...
     </ImageView>
 </LinearLayout>

The sizes of the images are such that they fill the screen on a 540x960, 275ppi screen. 图像的大小使得它们在540x960,275ppi屏幕上填充屏幕。 But when I run my app on an emulator, the last image is squished. 但是当我在模拟器上运行我的应用程序时,最后一张图像被压扁了。

After reading the Supporting Multiple Screens document, I learned that specifying the image dimensions in dp's instead of "wrap_content" should cover for different density screens. 在阅读了Supporting Multiple Screens文档后,我了解到在dp中指定图像尺寸而不是“wrap_content”应该涵盖不同的密度屏幕。

But what about screens with different sizes? 但是不同尺寸的屏幕怎么样? For example, if I have two different-sized screens with the same pixel densities, I can have a condition where my image fits the screen on one of them but not on the other, right? 例如,如果我有两个不同大小的屏幕具有相同的像素密度,我可以有一个条件,我的图像适合屏幕上的一个,但不是另一个,对吧? Is having two different layouts the only solution? 有两种不同的布局是唯一的解决方案吗?

For example, the two different-sized (but same density) screens might have 100 and 200 vertical pixels available, and image is sized 150dps, so it wouldn't fit on the first screen. 例如,两个不同大小(但密度相同)的屏幕可能有100和200个垂直像素可用,图像大小为150dps,因此它不适合第一个屏幕。 Am I right? 我对吗?

Thanks in advance 提前致谢

Almost any Android app which tries to fill the entire window with set bitmaps without being willing to crop, scale or leave uncovered areas is doomed to failure. 几乎所有尝试使用设置位图填充整个窗口而不愿意裁剪,缩放或离开未覆盖区域的Android应用程序注定要失败。 With so many different aspect ratios and screen sizes it's just not possible to magically fit a set of content with a fixed aspect ratio and size. 由于具有如此多的不同宽高比和屏幕尺寸,因此无法灵活地适应具有固定宽高比和大小的一组内容。 Even if you provide another layout that handles a different size screen that won't really fix the problem because no doubt there is a third device out there somewhere with a screen size that is different from both of the two that you've covered. 即使您提供另一种处理不同大小屏幕的布局,也无法真正解决问题,因为毫无疑问,有一个第三个设备,屏幕尺寸与您所覆盖的两个屏幕尺寸不同。

Consider using a set of images that include a portion which can be stretched or cropped without losing fidelity. 考虑使用一组图像,其中包括可以拉伸或裁剪而不会失去保真度的部分。 That might be just a solid color, a gradient, a repeating pattern or possibly something else. 这可能只是一种纯色,渐变,重复模式或其他可能的东西。 Alternatively if appropriate you can just allow scrolling. 或者,如果合适,您可以只允许滚动。 All of this is dependent on your content and what you're trying to achieve though. 所有这些都取决于您的内容以及您尝试实现的目标。

Pay your attention at android:scaleType attribute in ImageView . 请注意ImageView中的android:scaleType属性。 By default system scales images using FIT_CENTER value from ImageView.ScaleType . 默认情况下,系统使用ImageView.ScaleType FIT_CENTER值缩放图像。 In this case system maintains original aspect ratio of your image and ensure that it will completely fit by one of device's side, either width or height. 在这种情况下,系统会保持图像的原始宽高比,并确保它完全适合设备的一个宽度或高度。

Edit: 编辑:

Sorry, made great mistake. 对不起,犯了大错。 Image completely fits ImageView by one of it's side. 图像完全适合ImageView的一方。

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

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