简体   繁体   English

imageview height作为所示图像的wrap_content

[英]imageview height as the wrap_content of the shown image

I want the width of an ImageView to be set by the parent and the height should be aspect proportional. 我希望父视图设置ImageView的宽度,高度应该是高宽比例。 The reasons for this is that next is shown a TextView that I want to place just under the ImageView. 原因是next会显示一个我想放在ImageView下面的TextView。

I can get image to show correctly using 我可以使用正确显示图像

android:layout_width="fill_parent"
android:layout_height="fill_parent"

however the ImageView height become the parent height which is much larger than that of the shown stretched image. 但是ImageView高度变为父高度,远远大于显示的拉伸图像的高度。 One idea was to make parent smaller vertically, but.. there I don't yet know the stretched image size. 一个想法是让父母垂直变小,但是......我还不知道拉伸的图像大小。

The following doesn't work because a small image is not filled up horizontally. 以下操作无效,因为水平未填充小图像。

android:layout_width="fill_parent"
android:layout_height="wrap_content"

Messing around with 随波逐流

android:layout_height="wrap_content"

for the RelativeLayout surrounding it all does not help. 对于它周围的RelativeLayout都没有帮助。 Also tried FrameLayout and LinearLayout and failed. 还尝试了FrameLayout和LinearLayout并失败了。

Any ideas? 有任何想法吗?

您必须将adjustViewBounds设置为true。

imageView.setAdjustViewBounds(true);

There is two case if your actual image size is equal or grater than your ImageView width and heigh then you can use adjustViewBounds property and if your actual image size is less than ImageView width and height than use scaleType property to shown image in ImageView based on your requirement. 有两种情况,如果您的实际图像大小等于或大于您的ImageView宽度和高度,那么您可以使用adjustViewBounds属性,如果您的实际图像大小小于ImageView宽度和高度,则使用scaleType属性在ImageView中显示基于您的图像需求。

1.Actual image size is equal or grater than ImageView required width and height. 1.实际图像大小等于或大于ImageView所需的宽度和高度。

<ImageView
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_launcher"/>

2.Actual image size is less than ImageView required width and height. 2.实际图像尺寸小于ImageView所需的宽度和高度。

<ImageView
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher"/>

So I have had the same issue more than once and looking through the existing stackoverflow answers have realised that no answer gives a perfect explanation for the real confusion regarding a solution to this problem. 所以我不止一次遇到过同样的问题并且通过现有的stackoverflow回答已经意识到没有答案给出了解决这个问题的真正困惑的完美解释。 So here you go: 所以你走了:

API 17+ API 17+

imageView.setAdjustViewBounds(true);

OR (in XML) 或(以XML格式)

android:adjustViewBounds="true"

solves the issue, it works no matter the actual size of the image resource, ie it will scale your image up or down to the desired size you have in your layout. 解决了这个问题,无论图像资源的实际大小如何,它都能正常工作,即它会将图像向上或向下缩放到您在布局中所需的大小。

Below API 17 API 17以下

Below API 17 android:adjustViewBounds="true" will only work for shrinking an image, not growing, ie if the actual height of the image source is smaller than the dimensions you are trying to achieve in your layout, wrap_content will use that smaller height and not scale 'up' (enlarge) the image as you desire. 在API 17下面android:adjustViewBounds="true"仅适用于缩小图像,而不是增长,即如果图像源的实际高度小于您在布局中尝试实现的尺寸, wrap_content将使用较小的高度而不是按照你的意愿按比例“放大”(放大)图像。

And so for API 17 and lower, you have no choice but to use a custom ImageView to achieve this behaviour. 因此对于API 17及更低版本,您别无选择,只能使用自定义ImageView来实现此行为。 You could either write a custom ImageView yourself or use a library, that has already done that job. 您可以自己编写自定义ImageView,也可以使用已经完成该工作的库。

Using a library 使用库

There is probably more than one library that fixes this issue, one of them is: 可能有多个库修复了这个问题,其中一个是:

compile 'com.inthecheesefactory.thecheeselibrary:adjustable-imageview:1.0.0'

which is used like this: 这是这样使用的:

<com.inthecheesefactory.thecheeselibrary.widget.AdjustableImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/your_drawable"/>

Using a custom View 使用自定义视图

alternatively to using an existing library, you could write a custom view yourself, eg: 或者使用现有的库,您可以自己编写自定义视图,例如:

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

public class ScalableImageView extends ImageView {

    boolean adjustViewBounds;

    public ScalableImageView(Context context) {
        super(context);
    }

    public ScalableImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScalableImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setAdjustViewBounds(boolean adjustViewBounds) {
        this.adjustViewBounds = adjustViewBounds;
        super.setAdjustViewBounds(adjustViewBounds);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }

        if (adjustViewBounds) {
            int drawableWidth = drawable.getIntrinsicWidth();
            int drawableHeight = drawable.getIntrinsicHeight();
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            int widthSize = MeasureSpec.getSize(widthMeasureSpec);
            int heightMode = MeasureSpec.getMode(heightMeasureSpec);
            int widthMode = MeasureSpec.getMode(widthMeasureSpec);

            if (heightMode == MeasureSpec.EXACTLY && widthMode != MeasureSpec.EXACTLY) {
                int height = heightSize;
                int width = height * drawableWidth / drawableHeight;
                if (isInScrollingContainer())
                    setMeasuredDimension(width, height);
                else
                    setMeasuredDimension(Math.min(width, widthSize), Math.min(height, heightSize));
            } else if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) {
                int width = widthSize;
                int height = width * drawableHeight / drawableWidth;
                if (isInScrollingContainer())
                    setMeasuredDimension(width, height);
                else
                    setMeasuredDimension(Math.min(width, widthSize), Math.min(height, heightSize));
            } else {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    private boolean isInScrollingContainer() {
        ViewParent parent = getParent();
        while (parent != null && parent instanceof ViewGroup) {
            if (((ViewGroup) parent).shouldDelayChildPressedState()) {
                return true;
            }
            parent = parent.getParent();
        }
        return false;
    }
}

... which you would use as follows (XML): ...您将使用如下(XML):

<com.YOUR_PACKE_NAME.ScalableImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:src="@drawable/your_drawable" />

This was the only way I could get it working, to have the second ImageView in the center and smaller than the first ImageView, and the TextView under the second ImageView. 这是我能让它工作的唯一方法,让第二个ImageView位于中心,小于第一个ImageView,第二个ImageView下面的TextView。

Unfortunately, it uses fixed "200dp" image size on the second ImageView, so it does not look the same on different sized devices. 不幸的是,它在第​​二个ImageView上使用固定的“200dp”图像大小,因此在不同大小的设备上看起来不一样。

It also destroys my ViewFlipper, since any Layout I tried around the second ImageView and the TextView makes them move or resize. 它还会破坏我的ViewFlipper,因为我尝试围绕第二个ImageView和TextView的任何布局都会使它们移动或调整大小。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ImageView
        android:id="@+id/imageview1"
        android:contentDescription="@string/desc"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/background" />
    <ImageView
        android:id="@+id/imageview2"
        android:layout_centerInParent="true"
        android:contentDescription="@string/desc"
        android:layout_height="200dp"
        android:layout_width="200dp" 
        android:adjustViewBounds="true"
        android:src="@drawable/image2" />
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/imageview2"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:gravity="center"
        android:textSize="26sp"
        android:textColor="#333"
        android:background="#fff"
        android:text="this is the text under the image right here"
        />
</RelativeLayout>

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

相关问题 在android中,尽管wrap_content用于高度和宽度,但在imageview中显示的图像还是太小 - Image shown in imageview too small in android despite being wrap_content for height and width 使用wrap_content高度在FrameLayout中裁剪ImageView - Crop ImageView in FrameLayout with wrap_content height ImageView wrap_content - 如何裁剪图像 - ImageView wrap_content - how to crop image ExpandableHeightListView ImageView 项目 layout_height wrap_content 未显示所有图像 - ExpandableHeightListView ImageView Item layout_height wrap_content not showing all image scaleType 和 wrap_content 如何确定 ImageView 的宽度和高度? - How does scaleType and wrap_content determine ImageView width and height? RelativeLayout Imageview android:layout_height =“ wrap_content”未唤醒 - RelativeLayout Imageview android:layout_height=“wrap_content” not woking 具有高度wrap_content的ConstraintLayout不适用于ImageView AdjustViewBounds - ConstraintLayout with height wrap_content does not work with ImageView adjustViewBounds ListView仅显示一个高度为“ wrap_content”的元素 - ListView only one element shown with height = “wrap_content” android imageview wrap_content - android imageview wrap_content ImageView中的自定义wrap_content - custom wrap_content in ImageView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM