【问题标题】:Horizontally scrolling a single image水平滚动单个图像
【发布时间】:2018-08-25 07:29:20
【问题描述】:

我有一个尺寸为3000 x 1500 像素的图像。我希望它垂直适合屏幕,并且我应该能够水平滚动它。

GITHUB 中的项目https://github.com/rameezalam/imagescrolldemo https://github.com/rameezalam/imagescrolldemo/archive/master.zip

图片链接:https://github.com/rameezalam/imagescrolldemo/raw/master/app/src/main/res/drawable/peace.jpg

以下是我的代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBlue"
    tools:context=".MainActivity">

    <HorizontalScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@color/colorPink"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/colorYellow"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:src="@drawable/peace" />
        </LinearLayout>

    </HorizontalScrollView>

</android.support.constraint.ConstraintLayout>

在布局文件的 Android Studio 预览中,我可以发现它按预期工作。

实机一加3T安卓8.0.0,如下图所示

  • compileSdkVersion 27
  • minSdkVersion 15
  • targetSdkVersion 27

dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }

编辑

所以在为我的布局添加背景颜色之后,我得到了以下内容。看起来图像视图没有扩展到其父视图。

【问题讨论】:

  • 您是否在真实设备中尝试了 3000 x 3000 的图像..?我想是因为这个
  • 你可以尝试在水平滚动视图高度使用 match_parent 吗?
  • 试试我之前问过你的图片...使图片高度超过1500

标签: android imageview horizontalscrollview


【解决方案1】:

问题出在设备上。 OnePlus 使用定制的操作系统,这会导致此类视图出现问题。

尝试使用 Android One 操作系统或默认 Android 操作系统的设备。

它适用于我的设备 Moto X Play。安卓 7.1.1

【讨论】:

    【解决方案2】:
    <?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:id="@+id/rootview"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none">
    
        <ImageView
            android:id="@+id/cropView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@mipmap/peace"
            android:scaleType="fitXY"
            tools:ignore="ContentDescription" />
    </HorizontalScrollView>
    
    
     </RelativeLayout>
    

    【讨论】:

    【解决方案3】:

    尝试将android:fillViewport="true" 设置为 Horizo​​ntalScrollView 并使其成为android:layout_width="wrap_content"

    【讨论】:

    • 不,还是一样。
    【解决方案4】:

    尝试用相对布局替换您的线性布局并添加以下内容:

    <HorizontalScrollView
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <ImageView
             android:layout_alignParentTop="true"
               android:layout_alignParentBottom="true"
               android:layout_alignParentRight="true"
               android:layout_alignParentLeft="true"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:src="@drawable/peace" />
        </RelativeLayout>
    
    </HorizontalScrollView>
    

    【讨论】:

    • 不,RelativeLayout 也一样。
    • @Zeeshan 如果它不起作用,我会更新我的答案尝试删除'ConstraintLayout'
    • 查看我更新的问题。看起来图像视图没有扩展到其父视图。
    • @Zeeshan 尝试删除 ConstraintLayout
    • 删除 ConstraintLayout 效果不佳。我在 GITHUB 中添加了项目:github.com/rameezalam/imagescrolldemo
    【解决方案5】:

    试试这个

    使用AppCompatImageView 而不是ImageView

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <android.support.v7.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitStart"
            app:srcCompat="@drawable/peace" />
    
    </HorizontalScrollView>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-16
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2013-08-14
      • 1970-01-01
      相关资源
      最近更新 更多