2011-01-13 6 views
51

画面の上部に3つの同じサイズの画像(200×100)を並べて表示する必要があります(ギャップなし)。それらは画面の全幅を占有し、アスペクト比を保持する必要があります。 レイアウトxmlファイルのみを使用するか、Javaコードを使用する必要がありますか?
解決策は独立しています...誰でもこの問題(またはそれに類似した問題)の解決策またはリンクを投稿できますか?ありがとう!アスペクト比を維持しながら3つの画像を画面全体に伸ばす方法は?

+0

私は現在同じ問題を解決しようとしています!解決策がまだ見つかりませんでした:(ImageViewの高さを自動的に調整する独自のImageViewを作成したことがありますか?元のImageViewを使用する方がよかったでしょうか。 –

+0

ここでは**簡単な魔法の式**が問題を解決しますいくつかの場合:http://stackoverflow.com/a/23777047/294884 – Fattie

答えて

1

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

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

もちろんコード内で行うのはいかがですか? –

142

有効になっています。しかし、私が上記のように、あなたは自分のクラスを作成する必要があります。しかし、それはかなり小さいです。 XMLでそれを使用するために今すぐAndroid: How to stretch an image to the screen width while maintaining aspect ratio?

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); 
    } 
} 

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

素敵です!素晴らしい仕事、すばらしい執筆! –

+7

あなたのドロウアブル –

+1

getIntrinsicWidth()が0(0除算)を返していたため、さらに2つの問題が発生しました。また、getDrawable()がnullであるかどうかを確認したら、それ以外の場合はsuper.onMeasureを返すのを忘れていたため、Androidがクラッシュする可能性があります。 –

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); 
} 
} 
+1

ありがとう!ネットから画像をダウンロードするときにはるかに優れています:) – wasatz

+0

"universal image loader"を使用していただきありがとうございます。 – Scoup

+1

はimageloaderで動作しません!私はインターネットから画像をダウンロードしようとしています。 – Manoj

6

ジェイク・ウォートンはAspectRatioImageViewクラスを改善し、あなたは、XMLを介しdominantMeasurementとアスペクト比を設定することができます。あなたは下のリンクでそれを見つけることができます。

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

関連する問題