【问题标题】:Changing ImageView size dynamically?动态更改 ImageView 大小?
【发布时间】:2018-09-27 14:21:21
【问题描述】:

我有一个ImageView 夹在两个LinearLayout 之间。 ImageView 具有缩放功能,允许我放大和缩小图像 - 这意味着图像可以放大到屏幕的整个大小(match_parent)。

如果我加载一个非常高(高度)的图像,图像会与两个 LinearLayout 重叠,这在 到达 屏幕时是不行的,但如果用户决定稍后放大则可以。有没有办法在初始化时“限制” ImageView 的高度,但也允许它增大整个屏幕尺寸?

以下是一个“高”图像被错误加载的示例,因为它覆盖了顶部的LinearLayout 文本:

这就是我希望加载图像的方式,并且能够增长到前一个图像的大小:

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBgBlackTintDarker"
    android:orientation="vertical"
    android:paddingTop="20dp">

    <!-- Title at the top -->   
    <LinearLayout
        android:id="@+id/big_display_title_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical">
        <!-- Title stuff here-->
    </LinearLayout>


    <!-- Middle custom ImageView with ability to zoom-->
    <com.sometimestwo.moxie.ZoomieView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/full_displayer_image_zoomie_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"/>  

    <!-- Bottom toolbar-->
    <LinearLayout
        android:id="@+id/big_display_snack_bar_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/transparent"
        android:orientation="horizontal">
        
        <!-- Bottom toolbar stuff-->  
        
    </LinearLayout>

</RelativeLayout>

编辑:这是一个 GIF 来说明我的经历:

https://imgur.com/2Uqy4ZG

图像可以根据需要放大和缩小,但我需要在初始化时将图像放在标题和底部工具栏之间,这样它就不会覆盖任何东西(直到用户决定放大)。

【问题讨论】:

  • 您接受使用 ConstrainLayout 的解决方案吗?
  • 当然。我在 ConstraintLayout 方面做得不多,但我愿意学习。
  • 你能分享你的项目吗?
  • 我认为它太大而无法浏览...但是我已经在我的帖子中添加了一个 GIF 来说明我在寻找什么
  • 您是否提前知道您的顶部和底部线性布局有多大?或者它们的大小可以动态变化吗?

标签: android android-layout android-imageview android-relativelayout


【解决方案1】:

您应该将ImageView 夹在父RelativeLayout 的两个LinearLayouts 之间。更改您的代码如下:

<!-- Middle custom ImageView with ability to zoom-->
<com.sometimestwo.moxie.ZoomieView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/full_displayer_image_zoomie_view"
    android:layout_below="@id/big_display_title_container"
    android:layout_above="@id/big_display_snack_bar_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"/> 

【讨论】:

  • 这不起作用,因为 ImageView 将无法扩大整个屏幕的大小。当用户决定缩放时,我需要让图像放大到整个屏幕尺寸
  • 所以你应该做一些复杂的工作。您应该以编程方式计算底部和顶部 LinearLayouts 的高度,然后从屏幕高度中减去它。结果是初始 ImageView 高度。现在通过使用原始图像高度和初始高度计算缩放百分比,您应该将结果设置为 ZoomieView。
【解决方案2】:

您可以尝试使用 ConstraintLayout。

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="16dp">

<LinearLayout
    android:id="@+id/big_display_title_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

    <!-- Title stuff here-->

</LinearLayout>


   <View
       android:layout_width="0dp"
       android:layout_height="0dp"         
       app:layout_constraintBottom_toTopOf="@+id/big_display_snack_bar_container"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toBottomOf="@+id/big_display_title_container"/>

   <LinearLayout
            android:id="@+id/big_display_snack_bar_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
            android:background="@color/transparent"
        android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent">

    </LinearLayout>    
</android.support.constraint.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2015-01-26
    • 2021-03-11
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多