51

画面の上に 3 つの同じサイズの画像 (200 X 100) を並べて (隙間なく) 表示する必要があります。画面の幅全体を占め、縦横比を維持する必要があります。レイアウト xml ファイルのみを使用してそれを達成することは可能ですか、それとも Java コードを使用する必要がありますか?
解決策は解像度に依存しない必要があります...誰もがこの(または同様の)問題の解決策またはリンクを投稿できますか? ありがとう!

4

4 に答える 4

145

動作しました!しかし、私が上で言ったように、あなたはあなた自身のクラスを作成する必要があります。しかし、それはかなり小さいです。私はこの投稿のこのボブ・リーの答えの助けを借りてそれを作成しました:Android:アスペクト比を維持しながら画像を画面幅に伸ばす方法は?

package com.yourpackage.widgets;

import android.content.Context;
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) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
        setMeasuredDimension(width, height);
    }
}

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>
于 2011-01-14T05:52:02.897 に答える
30

何が問題になる可能性があるかを考慮したマイナー アップグレードのみ: null Drawable または 0 width。

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);
}
}
于 2012-06-24T06:13:18.520 に答える
6

Jake Wharton が AspectRatioImageView クラスを改良し、xml 経由でdominantMeasurement とspectRatio を設定できるようにしました。以下のリンクで見つけることができます。

https://gist.github.com/JakeWharton/2856179

于 2013-07-11T07:38:10.350 に答える
1

XML レイアウトと Android API についてはわかりませんが、計算は簡単です。画面の幅を求め、3 で割ります。それが各画像の幅です。ここで、幅に元の画像の高さと幅の比率を掛けます。それは各画像の高さです。

int imageWidth = screenWidth / 3;
float ratio = originalImage.Height / (float)originalImage.Width;
int imageHeight = (int)(imageWidth * ratio);
于 2011-01-13T06:07:35.093 に答える