我想在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 - 看到图像在这里拉伸。
景观
使用滑行在图像视图加载图像。 –
由于原始图像宽高比大约为16:9,因此它们将伸展,并且您的肖像模式将具有相反的宽高比。一个得到解决是在你的imageview中使用android:scaleType =“centerCrop” – Dexter