简体   繁体   中英

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. 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.

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. 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. 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 . By default system scales images using FIT_CENTER value from ImageView.ScaleType . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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