简体   繁体   English

Android ImageView被裁剪

[英]Android ImageView being cropped

first of all here's a graphic to illustrate my problem: Graphic 首先,这里有个图形来说明我的问题: 图形

On the left image you can see how it should look. 在左图上,您可以看到它的外观。 And this is also how AndroidStudio shows it in the preview. 这也是AndroidStudio在预览中显示它的方式。 But if I run it on the emulator, it cropps the image and only the dark green part is left. 但是,如果我在模拟器上运行它,它将裁剪图像,仅留下深绿色的部分。 (right image) (右图)

<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/img_header"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:focusableInTouchMode="false"
        android:src="@drawable/header"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop" />

The parent view: 父视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".menu">

I trief different scaleTypes, but nothing gives me the outcome I want to have. 我尝试了不同的scaleTypes,但没有任何东西能给我我想要的结果。 Not the first time I'm sturggling with ImageViews, but the first time I can't solve the issue myself... 这不是我第一次迷恋ImageViews,而是我第一次无法自己解决问题...

If I remove the "adjustViewBounds" the preview shows the same outcome as the device/emulator. 如果删除“ adjustViewBounds”,则预览显示与设备/仿真器相同的结果。 In case that that is a hint for someone. 如果那是对某人的提示。

Edit: I created a second version of the header, which is double the size. 编辑:我创建了标题的第二个版本,它的大小是原来的两倍。 And now it works with "fitXy" and "adjustViewBound(true)". 现在,它可以与“ fitXy”和“ adjustViewBound(true)”一起使用。 But why? 但为什么? The original image was 512px in height, which should've been enough for adjustViewBounds to keep the aspect ratio. 原始图像的高度为512像素,这足以使AdjustViewBounds保持宽高比。

It sounds like you want CENTER_INSIDE: 听起来您想要CENTER_INSIDE:

android:scaleType="centerInside"

public static final ImageView.ScaleType CENTER_INSIDE 公共静态最终ImageView.ScaleType CENTER_INSIDE

Added in API level 1 Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding). 在API级别1中添加了统一缩放图像(保持图像的宽高比),以便图像的两个尺寸(宽度和高度)都等于或小于视图的相应尺寸(减去填充)。 The image is then centered in the view. 然后,图像在视图中居中。 From XML, use this syntax: android:scaleType="centerInside". 在XML中,使用以下语法:android:scaleType =“ centerInside”。

also

The issue may be that adjustViewBounds will not increase the size of the ImageView beyond the natural dimensions of the drawable. 问题可能是AdjustViewBounds不会使ImageView的大小超出可绘制对象的自然尺寸。

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

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