Android layout 如何在屏幕上拉伸三幅图像以保持纵横比?

Android layout 如何在屏幕上拉伸三幅图像以保持纵横比?,android-layout,android-imageview,Android Layout,Android Imageview,我需要在屏幕顶部并排显示三个相同大小的图像(200 X 100)(没有间隙)。它们应该占据屏幕的整个宽度,并保持纵横比。 是否可以仅使用布局xml文件来完成此任务,或者我需要使用java代码? 解决方案应与解决方案无关。。。是否有人可以发布此(或类似)问题的解决方案或链接?谢谢 我不知道XML布局和android API,但数学很简单;找到屏幕的宽度并除以三。这是每个图像的宽度。现在将宽度乘以原始图像的高宽比。这是每个图像的高度 int imageWidth = screenWidth / 3;

我需要在屏幕顶部并排显示三个相同大小的图像(200 X 100)(没有间隙)。它们应该占据屏幕的整个宽度,并保持纵横比。 是否可以仅使用布局xml文件来完成此任务,或者我需要使用java代码?

解决方案应与解决方案无关。。。是否有人可以发布此(或类似)问题的解决方案或链接?谢谢

我不知道XML布局和android API,但数学很简单;找到屏幕的宽度并除以三。这是每个图像的宽度。现在将宽度乘以原始图像的高宽比。这是每个图像的高度

int imageWidth = screenWidth / 3;
float ratio = originalImage.Height / (float)originalImage.Width;
int imageHeight = (int)(imageWidth * ratio);

开始工作了!但是正如我上面所说的,您需要创建自己的类。但是它很小。我根据Bob Lee在本文中的回答创建了它:

现在在XML中使用它:

<com.yourpackage.widgets.AspectRatioImageView 
    android:id="@+id/image"
    android:src="@drawable/yourdrawable" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true" />

玩得开心

=====================================

通过使用android:adjustViewBounds=“true”,找到了另一种仅在XML中执行相同操作的方法。这里有一个例子:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/image1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/image2" />


    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/image2" />

</LinearLayout>

考虑到可能出现的问题,仅进行了一次小的升级:空可绘制或0宽度

package com.yourpackage.yourapp;

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

public class AspectRatioImageView extends ImageView {

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

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

public AspectRatioImageView(Context context, AttributeSet attrs,
        int defStyle)
{
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    Drawable drawable = getDrawable();
    if (drawable != null)
    {
        int width =  MeasureSpec.getSize(widthMeasureSpec);
        int diw = drawable.getIntrinsicWidth();
        if (diw > 0)
        {
            int height = width * drawable.getIntrinsicHeight() / diw;
            setMeasuredDimension(width, height);
        }
        else
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    else
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

Jake Wharton改进了AspectRatioImageView类,您可以通过xml设置主测量和aspectRatio。你可以在下面的链接中找到它


当然可以使用代码。但是,如果只在XML中执行它呢?我目前正在尝试解决同样的问题!尚未找到解决方案:(.可能有用,我们编写了自己的ImageView,可以自动调整其高度?但最好使用原始ImageView。以下是一个简单的神奇公式,可以在某些情况下解决问题:很好!很好,写得很好!当心你的绘图为空(例如,您正在从Internet上下载内容)。否则,它会像一个符咒一样工作。在某些情况下还会遇到另外两个问题-getIntrinsicWidth()返回0(除以0),因此您需要检查它。另外,一旦我检查getDrawable()是否如果为空,Android将崩溃,因为我忘了返回super.onMeasure,否则。感谢您提供的信息。我想知道它何时会返回0。您能告诉我更多信息吗?文档说明:“返回底层可绘制对象的固有宽度。如果没有固有宽度,例如纯色,则返回-1。”在-1情况下,我们必须给出“默认高度”我猜。这一切都取决于试图解决的确切问题。在我的情况下,照片总是有宽度和高度。空问题是有道理的,但我没有解决它,因为在我的代码中,我从来没有遇到过这个问题。我设法解决了滚动问题,修改了这个,谢谢!下载图像时效果更好当我试图从internet下载图像时,网络:)无法使用imageloader。
package com.yourpackage.yourapp;

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

public class AspectRatioImageView extends ImageView {

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

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

public AspectRatioImageView(Context context, AttributeSet attrs,
        int defStyle)
{
    super(context, attrs, defStyle);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    Drawable drawable = getDrawable();
    if (drawable != null)
    {
        int width =  MeasureSpec.getSize(widthMeasureSpec);
        int diw = drawable.getIntrinsicWidth();
        if (diw > 0)
        {
            int height = width * drawable.getIntrinsicHeight() / diw;
            setMeasuredDimension(width, height);
        }
        else
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    else
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}