Android中ViewPager内部的图像拉伸
我试图在一个viewPager中显示两个图像。在横向模式下,图像显示良好,但在纵向模式下,图像拉伸 以下是我的活动:Android中ViewPager内部的图像拉伸,android,android-viewpager,Android,Android Viewpager,我试图在一个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" xmln
<?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>
查看输出
肖像-请看图片在这里延伸
景观
问题可能出在android:scaleType=“fitCenter 试试android:scaleType:“center\u crop”,看看它能做什么
同样从中,您可以看到,
CENTER
不进行缩放…而FIT\u CENTER
基于CENTER
使用android:scaleType=“centerCrop”
均匀缩放图像(保持图像的纵横比),使图像的两个尺寸(宽度和高度)等于或大于视图的相应尺寸(减去填充)
尝试此操作,使您的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"/>
我认为您应该更改scaleType属性。根据您的需要,您可以使用“centerCrop”或“centerInside”。这两种情况都非常适合两个轴,而您当前使用的仅适合一个轴。您还可以通过以下途径找到更多信息:
更改您的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"/>
使用glide在图像视图中加载图像。它们会拉伸,因为原始图像纵横比约为16:9,而您的肖像模式的纵横比正好相反。一种解决方法是使用android:scaleType=“centerCrop”“在你的imageview@JasonKrs而不是祝贺嘿@NileshRathod,运气不好,兄弟。只需提到ImageView被包装在一个线性布局中,该布局包含高度和宽度的包装内容。实际上,几周前我正在做一个项目。。这就是为什么乍一看很明显。。。“我不是专家。@TechBee试图通过编程方式设置imageView.setScaleType(中心裁剪)或imageView.setScaleType(内部imageView.ScaleType.CENTER_);谢谢@Mahesh。不走运,兄弟。
<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"/>