2016-11-08 41 views
3

比率を16:9に保つには、ビットマップをImageViewに設定する必要があります。アドバイスはありますか?主な解決策はおそらく、カスタムImageViewのオーバーライドメソッドonMeasureですが、どうですか?Android ImageView - 16:9

+0

[デバイスの画面サイズに基づいて比率を比率に設定する方法、16:9,1:1,3:2,2:1,4:3](https: //stackoverflow.com/questions/36305054/how-to-set-imageview-in-percent-relative-layout-to-following-ratios-based-on-de) –

答えて

2
あなたのxmlで次に
public class CustomImageView extends ImageView { 

    // some other necessary things 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = getMeasuredWidth(); 

     //force a 16:9 aspect ratio    
     int height = Math.round(width * .5625f); 
     setMeasuredDimension(width, height); 
    } 
} 

<path.to.package.CustomImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/img"/> 
2

EDIT:Googleは正式にAPI 26を起動パーセントサポートライブラリを推奨していませんし、あなたはできるだけ早くそれから離れて移動する必要があります。ビュー寸法の少なくとも一方が「制約に一致」するように設定されている場合、図9(0dp):あなたは、比など16を表示サイズを設定することができ 比として

セットサイズ。比率を有効にするには、[アスペクト比の切り替えの切り替え](図10の1)をクリックし、表示される入力に幅:高さの比率を入力します。

幅と高さの両方が制約に一致するように設定されている場合は、[アスペクト比の切り替えの切り替え]をクリックして、他の寸法の比率に基づいて寸法を選択できます。ビューインスペクタは、対応するエッジを実線で結ぶことによって、比率として設定されているものを示します。

たとえば、両方の辺を「拘束条件」に設定した場合は、「アスペクト比の拘束の切り替え」を2回クリックして、高さの比率となる幅を設定します。ここで

The view is set to a 16:9 aspect with the width based on a ratio of the height.

詳細情報:https://developer.android.com/training/constraint-layout/index.html#adjust-the-view-size


以下の図に示すように、今全体のサイズは、(任意の方法で定義することができる)ビューの高さによって決定されます

PercentFrameLayoutPercentRelativeLayoutと呼ばれるものがサポートライブラリにあります。

<android.support.percent.PercentFrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <ImageView 
    app:layout_widthPercent="100%" 
    app:layout_aspectRatio="178%" 
    android:scaleType="centerCrop" 
    android:src="@drawable/header_background"/> 
    <!-- The rest of your layout --> 
</android.support.percent.PercentRelativeLayout> 

あなたは上記のレイアウトを詳しく見てみた場合、あなたは(16で固定する必要があります:9)問題のImageViewがいることがわかりますPercentFrameLayoutに巻き付けられ、上で設定した2つの属性があります

app:layout_widthPercent="100%" 
app:layout_aspectRatio="178%" 

だから(1)あなたが問題になっている私たちの ImageViewため 有数の寸法であることを寸法(幅または高さのどちらか)のいずれかを定義する必要があります:あなたが前に見ていない可能性があります ImageView。この場合、 ImageViewは、最大幅( android:layout_width="match_parent"など) のように垂直方向に拡張されます。(2)アスペクト比をパーセントで設定する必要があります(したがってライブラリ名)。この場合は178%(16/9 = 1.77777777778以上は単純に1.78:1または178%)。

パーセントサポートライブラリhereを参照してください。

1

上記の答えは私のために動作しませんでした、私はこれを発見し、働いていた:

public class AspectRatioImageView extends ImageView { 
    // NOTE: These must be kept in sync with the AspectRatioImageView attributes in attrs.xml. 
    public static final int MEASUREMENT_WIDTH = 0; 
    public static final int MEASUREMENT_HEIGHT = 1; 

    private static final float DEFAULT_ASPECT_RATIO = 1f; 
    private static final boolean DEFAULT_ASPECT_RATIO_ENABLED = false; 
    private static final int DEFAULT_DOMINANT_MEASUREMENT = MEASUREMENT_WIDTH; 

    private float aspectRatio; 
    private boolean aspectRatioEnabled; 
    private int dominantMeasurement; 

    public AspectRatioImageView(Context context) { 
    this(context, null); 
    } 

    public AspectRatioImageView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AspectRatioImageView); 
    aspectRatio = a.getFloat(R.styleable.AspectRatioImageView_aspectRatio, DEFAULT_ASPECT_RATIO); 
    aspectRatioEnabled = a.getBoolean(R.styleable.AspectRatioImageView_aspectRatioEnabled, 
     DEFAULT_ASPECT_RATIO_ENABLED); 
    dominantMeasurement = a.getInt(R.styleable.AspectRatioImageView_dominantMeasurement, 
     DEFAULT_DOMINANT_MEASUREMENT); 
    a.recycle(); 
    } 

    @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    if (!aspectRatioEnabled) return; 

    int newWidth; 
    int newHeight; 
    switch (dominantMeasurement) { 
     case MEASUREMENT_WIDTH: 
     newWidth = getMeasuredWidth(); 
     newHeight = (int) (newWidth/aspectRatio); 
     break; 

     case MEASUREMENT_HEIGHT: 
     newHeight = getMeasuredHeight(); 
     newWidth = (int) (newHeight * aspectRatio); 
     break; 

     default: 
     throw new IllegalStateException("Unknown measurement with ID " + dominantMeasurement); 
    } 

    setMeasuredDimension(newWidth, newHeight); 
    } 

    /** Get the aspect ratio for this image view. */ 
    public float getAspectRatio() { 
    return aspectRatio; 
    } 

    /** Set the aspect ratio for this image view. This will update the view instantly. */ 
    public void setAspectRatio(float aspectRatio) { 
    this.aspectRatio = aspectRatio; 
    if (aspectRatioEnabled) { 
     requestLayout(); 
    } 
    } 

    /** Get whether or not forcing the aspect ratio is enabled. */ 
    public boolean getAspectRatioEnabled() { 
    return aspectRatioEnabled; 
    } 

    /** set whether or not forcing the aspect ratio is enabled. This will re-layout the view. */ 
    public void setAspectRatioEnabled(boolean aspectRatioEnabled) { 
    this.aspectRatioEnabled = aspectRatioEnabled; 
    requestLayout(); 
    } 

    /** Get the dominant measurement for the aspect ratio. */ 
    public int getDominantMeasurement() { 
    return dominantMeasurement; 
    } 

    /** 
    * Set the dominant measurement for the aspect ratio. 
    * 
    * @see #MEASUREMENT_WIDTH 
    * @see #MEASUREMENT_HEIGHT 
    */ 
    public void setDominantMeasurement(int dominantMeasurement) { 
    if (dominantMeasurement != MEASUREMENT_HEIGHT && dominantMeasurement != MEASUREMENT_WIDTH) { 
     throw new IllegalArgumentException("Invalid measurement type."); 
    } 
    this.dominantMeasurement = dominantMeasurement; 
    requestLayout(); 
    } 
} 

:ワンsimbol */に変更された(ソース内のコメントを読む) そして、これをあなたの/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="AspectRatioImageView"> 
     <attr name="aspectRatio" format="float" /> 
     <attr name="aspectRatioEnabled" format="boolean" /> 
     <attr name="dominantMeasurement"> 
      <enum name="width" value="0" /> 
      <enum name="height" value="1" /> 
     </attr> 
    </declare-styleable> 
</resources> 

は、次に、あなたのレイアウト(アスペクト比=幅/高さ)で、この方法を使用することができますあなたのImageViewのために9:PercentFrameLayoutPercentRelativeLayout以来10

Source

9

は、私が比16を維持するためにConstraintLayoutの使用を検討してあなたにお勧めしたい、APIレベル26.0.0では非推奨となりました。 ConstraintLayoutは、Androidプラットフォーム向けのレスポンシブUIを構築するための本当に強力なツールです。詳しくはBuild a Responsive UI with ConstraintLayoutをご覧ください。ここで

は維持するConstraintLayoutにあなたのImageViewのを構築するための方法の例である16:9比率:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_marginEnd="0dp" 
     android:layout_marginStart="0dp" 
     android:layout_marginTop="0dp" 
     app:srcCompat="@mipmap/ic_launcher" 
     app:layout_constraintDimensionRatio="H,16:9" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

</android.support.constraint.ConstraintLayout> 

あなたのモジュールのbuild.gradleファイルにconstraint-layout依存関係を追加することを忘れないでください

implementation "com.android.support.constraint:constraint-layout:1.0.2" 

またはXMLファイルを編集する代わりに、レイアウトエディタで直接レイアウトを編集します。

Layout Editor