2017-11-03 12 views
2

centerCropを使用せずに風景画像を表示する際に問題があります。 私はPercentFramelayoutを試してみました、と設定アスペクト比がプログラムによってこのような :imageViewのプログラムでアスペクト比を設定する

laParams.percentLayoutInfo.aspectRatio = img.width.toFloat()/img.height.toFloat() 

結果はokです - アプリケーションはcenterCropなく、すべての風景画像を示した:

Post loaded successfully

しかし、時には私が手アスペクト比が間違っている:

Wrong aspect ratio 1

Wrong aspect ratio 2

私はandroid:adjustViewBounds ="true"を試しましたが、それは私を助けません。 そして、私はこのようなXMLでアスペクト比を設定し、ConstraintLayoutを使用:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    > 

    <android.support.v7.widget.AppCompatImageView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/photo" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     app:layout_constraintDimensionRatio="h,16:9" 
     app:layout_constraintHorizontal_chainStyle="spread" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 

     /> 
</android.support.constraint.ConstraintLayout> 

私は良い結果を得たが、私は、画像に異なるサイズをロードします。画像はCenterCropFitXYなしである必要があります。私は設定されたアスペクト比についてプログラム的に良い答えが見つかりませんConstraintlayout 私はinstagramやvkのような画像を表示したいと思います。

答えて

1

遅くとも、いつものように、これは誰かにとって役に立ちます。

あなたは次の操作を実行して、プログラム的比プロパティを設定することができます

ConstraintSet set = new ConstraintSet(); 
set.clone(mLayout); 
set.setDimensionRatio(mView.getId(), "16:9"); 
set.applyTo(mLayout); 

私は上記のやっていることは、レイアウトの既存ConstraintSetを取得し、ConstraintLayout(mLayout)わたしConstraintSetを再適用する前に、プログラム比率制約を加えています。

findViewById()メソッドを使用してConstraintLayout(mLayout)を取得します(idプロパティを.xmlのConstraintLayoutに手動で追加する必要があります)。

関連する問題