如何在此应用程序中保持图像视图的纵横比? 【Android、ImageView、长宽比、ScrollView】

Posted

技术标签:

【中文标题】如何在此应用程序中保持图像视图的纵横比? 【Android、ImageView、长宽比、ScrollView】【英文标题】:How do I maintain aspect ratio of an imageview like in this application? [Android, ImageView, Aspect ratio, ScrollView] 【发布时间】:2019-12-22 00:27:15 【问题描述】:

我想实现图像视图和按钮组的纵横比,如下图 1 所示。图 2 是我活动的当前状态。我想达到与图1中的应用程序相同的状态。

编辑 1 - 图片一上的应用程序始终是 imageview + 按钮组,一个所有设备,所以这不是纵横比的问题,而是别的东西

图像视图和按钮组被包裹在一个约束布局中,它位于滚动视图内部。

图片1 - 图二——

下面的代码是关于 ImageView 和按钮组的部分。

<ImageView
        android:id="@+id/poster_image"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toTopOf="@id/buttons_group"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/buttons_group"
        android:layout_
        android:layout_
        app:layout_constraintTop_toBottomOf="@id/poster_image"
        app:layout_constraintBottom_toTopOf="@id/imdb_placeholder"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="parent">

    <Button
            android:id="@+id/trailer_button"
            android:layout_
            android:layout_
            android:background="@drawable/ic_play"
            android:foreground="?android:attr/selectableItemBackground"
            android:scaleType="centerCrop"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/watch_later_button"
            app:layout_constraintTop_toTopOf="@id/buttons_group"
            app:layout_constraintBottom_toBottomOf="@id/buttons_group"/>

    <Button
            android:id="@+id/watch_later_button"
            android:layout_
            android:layout_
            android:background="@drawable/ic_watch_later"
            android:foreground="?android:attr/selectableItemBackground"
            android:scaleType="centerCrop"
            app:layout_constraintTop_toTopOf="@id/buttons_group"
            app:layout_constraintBottom_toBottomOf="@id/buttons_group"
            app:layout_constraintStart_toEndOf="@+id/trailer_button"
            app:layout_constraintEnd_toStartOf="@+id/share_movie_button"/>
    <Button
            android:id="@+id/share_movie_button"
            android:layout_
            android:layout_
            android:background="@drawable/ic_share"
            android:foreground="?android:attr/selectableItemBackground"
            android:scaleType="centerCrop"
            app:layout_constraintTop_toTopOf="@id/buttons_group"
            app:layout_constraintBottom_toBottomOf="@id/buttons_group"
            app:layout_constraintStart_toEndOf="@+id/watch_later_button"
            app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

视图需要滚动吗?您是否要让图片占据整个屏幕并让按钮在顶部滚动? 是的,有点。图片和按钮应该占据屏幕高度,但它们在滚动视图内,所以我想向下滚动。有什么想法吗? 您对 RecyclerViews 感觉如何? 我们又爱又恨哈哈,为什么呢?我指的应用是TodoMovieList, 我知道使用 Recycler 视图完成此任务的一种相当简单的方法。 【参考方案1】:

使用RecyclerView,您可以提供多种视图类型。

enum class ViewType 
    HEADER_CONTROLS,
    ADDITIONAL_CONTROLS,
    COMMENTS


class Adapter() 
    fun getViewType(position: Int): Int 
        when (position) 
            0 -> ViewType.HEADER_CONTROLS.ordinal
            // ETC
        
    

然后您可以将标题视图创建为匹配父级,它会适当地滚动。

【讨论】:

感谢您的提示,试了一下。仍然没有得到图片 1 上的内容,相同的比例在不同设备上显示不同,而图片 1 中的应用在所有设备上保持相同。滚动视图的顶部始终是 imageview+button group 抱歉回复晚了。我应该将我想要在标题视图中的内容封装为 recyclerview 元素吗?我从未使用过具有多种视图类型的 recyclerview 您可以将其分解为每个视图或分组,以使您感觉更舒服。我都做过。两者都行得通。

以上是关于如何在此应用程序中保持图像视图的纵横比? 【Android、ImageView、长宽比、ScrollView】的主要内容,如果未能解决你的问题,请参考以下文章

如何在没有纵横比的情况下在 SVG 中保持图像的纵横比

如何在平行视图中保持良好的纵横比

如何保持并排图像的纵横比,保持图像高度相同并在图像之间固定填充?

AutoLayout约束以适应矩形内的视图,保持一定的纵横比(以编程方式)

UIViewContentModeScaleAspectFill 不保持图像的纵横比

以视频的纵横比在纹理视图上播放视频