2017-12-21 11 views
1

InAppメッセージと呼ばれるものを実装しています。サイズ変更後にImageViewを画像サイズに合わせようとしています。サイズ変更後の画像サイズに合わせる

これは私のImageViewのである:

<?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/constraintLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="15dp" 
    android:layout_marginRight="15dp" 
    android:layout_marginStart="15dp" 
    android:layout_marginTop="40dp" 
    android:padding="6dp"> 

    --other components-- 

    <ImageView 
     android:id="@+id/middleLayoutImage" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/border" 
     android:padding="5dp" 
     app:layout_constraintBottom_toTopOf="@+id/middleLayoutText" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/topWrapper" 
     app:layout_constraintVertical_chainStyle="packed" /> 

    --other components-- 

</android.support.constraint.ConstraintLayout> 

のimgをダウンロードして、このビューに配置するには、私はピカソを使用しています:今 Before fitting

 Picasso.with(getApplicationContext()) 
      .load(inputurl) 
      .into(imageView); 

は今、私の活動は、このようになります親レイアウトの縦横比でサイズを変更したいと思います。私は成功しませんたくさんのODの組み合わせを試してみた:

  • アンドロイド:scaleType = "fitXY" - stretched img

  • ピカソフィット()centerCrop() - centerCrop

  • ピカソフィットを。 ().centerInside() - centerInside

よりアンドロイドとの組み合わせ:adjustViewBoundsのscaleTypes等

Resized correctly - この場合、私はpicasso:.resize(2000,500).centerInside()を使用しましたが、この場合のみ動作します。自動的に?助けを事前に感謝 :)

編集:Picassoについては

ImageView imageView = (ImageView) findViewById(R.id.middleLayoutImage); 

    String inputurl = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSQtqf1EUqH4eQNNqYFbSawpiR5bypfOhvlatgS0mC3Ve1nk2adbw"; //http://www.gatesms.eu/img/android.png"; 
    Picasso.with(getApplicationContext()) 
      .load(inputurl) 
      .resize(2000,500) 
      .centerInside() 
      .into(imageView); 
+0

ピカソをロードしたあなたの活動や断片を見せてください! – Xenolion

+0

上記の "編集"の部分を見て、私は期待した結果を出しますが、このソリューションはこの場合にのみ動作します。あなたがmatch_constriantまたは0dp –

+0

は、それが完璧に動作し、本当にありがとうございます:あなたは歓迎されている – elmorabea

答えて

0

正しくあなたのImageの大きさを知っている必要がありますあなたのImageに合うように。しかし、この場合、あなたは画像のサイズを知らないので、良いUIデザインのためにプレースホルダ(ピカソが別の画像をロードしている間に表示する画像)をdrawableフォルダに保存してplaceholderとし、 srcまたは代わりに、このようピカソ自身を使用して:今

Picasso.with(getActivity()) 
.load(imageUrl) 
.placeholder(R.drawable.placeholder) 
.error(R.drawable.error) 
.resize(screenWidth, imageHeight) 
.fit() 
.centerCrop() 
.into(imageView); 

何?

imageViewに別のImageがあるので、ピカソは画像がうまく収まるように高さと幅を知るようになりました。しかし、あなたがイメージを提供しない場合、結果は非常に小さいイメージまたは不適切に伸ばされたイメージです。ピカソが私のような要求をしても失敗した場合には、必要に応じて省略することができるネットワーク接続なしのエラー画像を追加しました。

XMLでもandroid:adjustViewBoundsを忘れないでください。

+0

@その後、以下の私の答えを確認するために、変化幅をmatch_parentを使用することはできませんConstraintLayoutで –

+0

Dと**ハッピーコーディング**!。 – Xenolion

関連する問題