画面の上部に3つの同じサイズの画像(200×100)を並べて表示する必要があります(ギャップなし)。それらは画面の全幅を占有し、アスペクト比を保持する必要があります。 レイアウトxmlファイルのみを使用するか、Javaコードを使用する必要がありますか?
解決策は独立しています...誰でもこの問題(またはそれに類似した問題)の解決策またはリンクを投稿できますか?ありがとう!アスペクト比を維持しながら3つの画像を画面全体に伸ばす方法は?
答えて
XMLレイアウトとAndroid APIについてはわかりませんが、数学は単純です。画面の幅を見つけて3で割ります。それは各画像の幅です。ここで元の画像の幅に高さの比率を掛けます。これが各画像の高さです。
int imageWidth = screenWidth/3;
float ratio = originalImage.Height/(float)originalImage.Width;
int imageHeight = (int)(imageWidth * ratio);
もちろんコード内で行うのはいかがですか? –
有効になっています。しかし、私が上記のように、あなたは自分のクラスを作成する必要があります。しかし、それはかなり小さいです。 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>
素敵です!素晴らしい仕事、すばらしい執筆! –
あなたのドロウアブル –
getIntrinsicWidth()が0(0除算)を返していたため、さらに2つの問題が発生しました。また、getDrawable()がnullであるかどうかを確認したら、それ以外の場合はsuper.onMeasureを返すのを忘れていたため、Androidがクラッシュする可能性があります。 –
可能性のあるものを考慮に入れたマイナーアップグレードのみ: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);
}
}
ジェイク・ウォートンはAspectRatioImageViewクラスを改善し、あなたは、XMLを介しdominantMeasurementとアスペクト比を設定することができます。あなたは下のリンクでそれを見つけることができます。
- 1. アスペクト比を維持しながら画像を塗りつぶす方法は?
- 2. ストーリーボードで画像のアスペクト比を維持する方法は?
- 3. 画像+テキストDIVで、アスペクト比を維持
- 4. UISegmentedControlは画像にアスペクト比を維持します
- 5. 画面全体にヘッダーを伸ばす方法
- 6. CSS - 画像フルスクリーンですが、アスペクト比を維持します
- 7. ios:colorWithPatternImage、画像全体を伸ばす
- 8. 画像境界は120x120中の画像は、アスペクト比を維持し
- 9. 画像を動的にロードしてアスペクト比を維持する
- 10. Androidで画像のアスペクト比を乱すことなく画像を伸ばす方法は?
- 11. オーバーフローで画像アスペクト比を維持しながら固定高さのフレックスライダー
- 12. イメージスケールフィッティング画面全体が伸びずに
- 13. ブートストラップは画面全体にコンテンツを伸ばします
- 14. アスペクト比を維持しながら画像を縮小する最も効率的な方法は?
- 15. てPhotoViewフィット画面の高さのアスペクト比を維持
- 16. 異なる画面で画像を伸ばす方法
- 17. アスペクト比を維持してAndroidで画像を切り取る
- 18. CSS divを伸ばして画面全体に伸ばします
- 19. アスペクト比とスケーラビリティを維持しながらCSS背景画像としてSVGスプライトシートを使用する方法
- 20. アップロードされた画像のサイズを変更しながらアスペクト比を維持する方法
- 21. レスポンシブな画像画面幅にスケーリングしてその比率を維持する
- 22. 画像の高さを拡大し、画面の境界外でアスペクト比を維持し、右揃え
- 23. アスペクト比を維持しない画像のサイズを変更する
- 24. 任意の画面で16:9のアスペクト比に比例して高さと幅を維持する方法
- 25. MVC Webアプリ全体を画面全体に伸ばしてください
- 26. Androidで画面全体に画像を移動する方法
- 27. 正方形のhtml要素内の画像のアスペクト比を維持する
- 28. ボタン画像のアスペクト比を保持する方法
- 29. 異なる画面の画像ビュー間のサイズの比率を維持する
- 30. 別の画像サイズに基づいて画像をスケーリングし、そのアスペクト比を維持するアルゴリズム
私は現在同じ問題を解決しようとしています!解決策がまだ見つかりませんでした:(ImageViewの高さを自動的に調整する独自のImageViewを作成したことがありますか?元のImageViewを使用する方がよかったでしょうか。 –
ここでは**簡単な魔法の式**が問題を解決しますいくつかの場合:http://stackoverflow.com/a/23777047/294884 – Fattie