簡體   English   中英

如何縮放3張圖像以適合屏幕寬度

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

這些是我追求的結果: 3張圖片

基本上,我想縮放3張圖像,使它們具有相同的高度,並且全部填充屏幕寬度。 原始圖像將具有相同的高度。

可以使用布局完成此操作,而無需根據代碼計算寬度嗎?

只需使用布局權重即可。

在主布局或包含ImageViews的布局中,

android:weightSum="10"

然后在各個ImageView中,按如下所示放置layout_weights或達到您的要求。

在此處輸入圖片說明 這基本上意味着圖像的寬度將分別為25%,55%和20%

您可以使用具有權重屬性的線性布局,如下所示:

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

如果您需要更多信息,請在下面評論

嘗試這個:

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

“魔術”在重量成分中。 您在版面中定義的總權重為3,而圖片視圖應占總重量的三分之一,因此該值為1。

就我而言,圖像需要在運行時進行更新,因此沒有一個答案是完全合適的。 最后,我擴展了LinearLayout並編寫了一個統一所有圖像高度並確保所有圖像一起填充LinearLayout寬度的小例程。 如果有人試圖達到相同的目的,我的代碼如下所示:

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