简体   繁体   English

如何缩放3张图像以适合屏幕宽度

[英]How to scale 3 images to fit screen width

These is the result that I am after: 这些是我追求的结果: 3张图片

Basically I want to scale the 3 images so that they have the same height and all together fill the screen width. 基本上,我想缩放3张图像,使它们具有相同的高度,并且全部填充屏幕宽度。 The original images will all have same height. 原始图像将具有相同的高度。

Can this be done using layout, without width calculations from code? 可以使用布局完成此操作,而无需根据代码计算宽度吗?

Just use Layout Weights. 只需使用布局权重即可。

In the main layout, or the layout which contains the ImageViews, put 在主布局或包含ImageViews的布局中,

android:weightSum="10"

and then in the individual ImageViews, put layout_weights as shown below, or upto your requirements. 然后在各个ImageView中,按如下所示放置layout_weights或达到您的要求。

在此处输入图片说明 This basically means the width of the images will be 25%, 55% and 20% respectively . 这基本上意味着图像的宽度将分别为25%,55%和20%

You can use a linear layout with weight attribute specified as shown below: 您可以使用具有权重属性的线性布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="0dp"
        android:src="@drawable/bg_canvas"
        android:layout_height="300dp"
        android:layout_weight="0.33"/>
    <ImageView
        android:layout_width="0dp"
        android:src="@drawable/bg_canvas"
        android:layout_height="300dp"
        android:layout_weight="0.33"/>
    <ImageView
        android:layout_width="0dp"
        android:layout_height="300dp"
        android:src="@drawable/bg_canvas"
        android:layout_weight="0.33"/>
</LinearLayout>

Comment below if you need any further info 如果您需要更多信息,请在下面评论

try this: 尝试这个:

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="3"
  android:orientation="horizontal">

  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    .../>
  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    .../>
  <ImageView
    android:id="@+id/imageView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    .../>
</LinearLayout>

the "magic" is in the weight component. “魔术”在重量成分中。 you define a total weight of 3 in the layout and your image views should take a third of it, so the value is 1. 您在版面中定义的总权重为3,而图片视图应占总重量的三分之一,因此该值为1。

For my case the images needed to be updated at runtime, so none of the answers were exact fit. 就我而言,图像需要在运行时进行更新,因此没有一个答案是完全合适的。 I ended up extending LinearLayout and writing a small routine that unifies all images heights and make sure that all images together fill the LinearLayout width. 最后,我扩展了LinearLayout并编写了一个统一所有图像高度并确保所有图像一起填充LinearLayout宽度的小例程。 In case someone is trying to achieve the same, my code looks like this: 如果有人试图达到相同的目的,我的代码如下所示:

public class MyImgLayout extends LinearLayout
{

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

public void setup(ArrayList<String> images)
{
        this.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
    this.setLayoutParams(layoutParams); //set 0 height until we calculate it in onMeasure

    for (String image : images) {
        ImageView ivArticle = new ImageView(getContext());
        setImageFromName(image, ivArticle); //this where you set the image
        this.addView(ivArticle);
    }

}

private void scaleImages()
{
    if(getMeasuredHeight() == 0 && getMeasuredWidth() > 0) {
        if (isHorizontal) {
            double childRatioSum = 0;
            int images = 0;
            for (int i = 0; i < getChildCount(); i++) {
                ImageView iv = (ImageView) getChildAt(i);
                double width = iv.getDrawable().getIntrinsicWidth();
                double height = iv.getDrawable().getIntrinsicHeight();
                if (height > 0) {
                    childRatioSum += width / height;
                    images++;
                }
            }

            if (childRatioSum > 0 && images == getChildCount()) {
                //all images are downloaded, calculate the container height
                //(add a few pixels to makes sure we fill the whole width)
                double containerHeight = (int) (getMeasuredWidth() / childRatioSum) + images * 0.5;

                for (int i = 0; i < getChildCount(); i++) {
                    ImageView iv = (ImageView) getChildAt(i);
                    double width = iv.getDrawable().getIntrinsicWidth();
                    double height = iv.getDrawable().getIntrinsicHeight();
                    iv.setLayoutParams(new LinearLayout.LayoutParams((int) (width * (containerHeight / height)), (int) containerHeight));
                    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                }

                LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) this.getLayoutParams();
                params.width = LayoutParams.MATCH_PARENT;
                params.height = (int) containerHeight;
                this.setLayoutParams(params);
                requestLayout();
            }
        }
    }
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    scaleImages();
}
}

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

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