2017-07-28 13 views
1

viewPagerで画像のカップルを表示しようとしています。風景モードでは画像がきれいに表示されていますが、ポートレートモードでは画像が伸びています。ここでAndroidのViewPager内で画像が伸びる

は私の活動である:ここでは

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/colorAccent" 
    android:orientation="vertical" 
    tools:context="com.helloExp.builder.BuilderActivity"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/builder_viewPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </android.support.v4.view.ViewPager> 

    <TextView 
     android:layout_width="match_parent" 
     android:gravity="center" 
     android:text="Swipe to change image" 
     android:background="#000" 
     android:textColor="#FFF" 
     android:layout_height="40dp" 
     android:layout_alignParentStart="true" 
     /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_centerInParent="true" 
     android:text="When one door of happiness closes, another opens; but often we look so long at the closed door that we do not see the one that has opened for us." 
     android:layout_height="wrap_content" 
     android:id="@+id/textView"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:orientation="horizontal" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_height="wrap_content"> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_weight="5" 
      android:text="Decoration" 
      android:layout_height="wrap_content"/> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_weight="5" 
      android:text="Share" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 
</RelativeLayout> 

は私のviewPagerアダプタxmlファイルです:最後にここ

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:orientation="vertical" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content"> 

    <ImageView 
     android:id="@+id/backgroundImage" 
     android:layout_width="wrap_content" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 
     android:background="@drawable/cat_life_big" 
     android:layout_height="match_parent"/> 
</LinearLayout> 

は、アダプタのコードです:

public Object instantiateItem(ViewGroup container, int position) { 
     LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layoutView = inflater.inflate(R.layout.layout_builder, container, false); 
     container.addView(layoutView); 
     return layoutView; 
    } 

は出力

を参照してください。

Po rtrait - ここで画像が伸びているのを見てください。 android:scaleType="fitCenter

android:scaleType:"center_crop"を試してみて、それが何をするか見て

enter image description here

風景

enter image description here

+0

使用グライドへ

<ImageView android:id="@+id/backgroundImage" android:layout_width="wrap_content" android:adjustViewBounds="true" android:scaleType="fitCenter" android:background="@drawable/cat_life_big" android:layout_height="match_parent"/> 

変更それがあります。 –

+0

元の画像の縦横比が約16:9で、ポートレートモードのアスペクト比が逆になるため、縦横比が伸びます。あなたの画像ビューでは、使用しているandroid:scaleType = "centerCrop" – Dexter

答えて

0

問題がある可能性があります。 Android developerからも

、あなたは... CENTERは一切致しscallingを行わないことを確認し、FIT_CENTERCENTER

0

使用android:scaleType="centerCrop"

CENTER_CROP

スケール画像は、一様に(画像のを維持するために基づいています画像の両方の寸法(幅および高さ)が、ビューの対応する寸法(パディングなし)に等しいか、またはそれよりも大きくなるようにする。

は、私はあなたがscaleType属性を変更すべきだと思うあなたのImageView

このような
<ImageView 
    android:id="@+id/backgroundImage" 
    android:layout_width="wrap_content" 
    android:adjustViewBounds="true" 
    android:scaleType="centerCrop" 
    android:background="@drawable/cat_life_big" 
    android:layout_height="match_parent"/> 
+0

笑私はこれに最初に答えた;) –

+0

@ JasonKrsよりもお祝い –

+0

こんにちは@NileshRathod、幸運な弟。 ImageViewは、高さと幅のラップコンテンツを持つLinearLayoutの中にラップされています。 – TechBee

0

を作るこれを試してみてください。必要に応じて、 "centerCrop"または "centerInside"のいずれかを使用できます。両方のケースは両方の軸に非常によく合いますが、現在使用しているものは1つだけに合っています。また、到達することで、この上でより多くの情報を見つけることができます。

https://robots.thoughtbot.com/android-imageview-scaletype-a-visual-guide

0

チャゲあなたImageViewのコードを。あなたはバックグラウンドとして画像を使用しています。srcに変更してください。 あなたのコードは、画像ビューの画像をロードする

<ImageView 
    android:id="@+id/backgroundImage" 
    android:layout_width="match_parent" 
    android:adjustViewBounds="true" 
    android:scaleType="fitCenter" 
    android:src="@drawable/cat_life_big" 
    android:layout_height="match_parent"/> 
+0

ありがとう@Mahesh。運がない兄弟。 – TechBee

関連する問題