2017-07-28 134 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 - 看到圖像在這裏拉伸。

enter image description here

景觀

enter image description here

+0

使用滑行在圖像視圖加載圖像。 –

+0

由於原始圖像寬高比大約爲16:9,因此它們將伸展,並且您的肖像模式將具有相反的寬高比。一個得到解決是在你的imageview中使用android:scaleType =「centerCrop」 – Dexter

回答

0

的問題可能與android:scaleType="fitCenter

嘗試android:scaleType:"center_crop",看看它做什麼。

同樣來自Android developer,你看到CENTER確實沒有做scalling ......和FIT_CENTER基於CENTER

0

使用android:scaleType="centerCrop"

CENTER_CROP

縮放圖像均勻(保持圖像的寬高比),以便圖像的兩個尺寸(寬度和高度)等於或大於視圖的相應尺寸(減去填充)。

試試這個讓你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

lol我是第一個回答這個問題的;) –

+0

@JasonKrs比恭喜 –

+0

嘿@NileshRathod,沒有運氣的兄弟。只要提及ImageView就被包裝在一個LinearLayout中,該LinearLayout包含了高度和寬度的內容。 – TechBee

0

CHAGE您的ImageView的代碼。您正在使用圖像作爲免費將其更改爲src。 你的代碼是

<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"/> 

將其更改爲

<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