【问题标题】:android:textSize change in MotionLayoutandroid:textSize 在 MotionLayout 中的变化
【发布时间】:2021-02-05 19:39:28
【问题描述】:

我将使用 MotionLayout 为文本大小更改设置动画。

我对开始状态执行以下操作

<CustomAttribute
    motion:attributeName="textSize"
    motion:customDimension="16sp" />

以下是结束状态

<CustomAttribute
    motion:attributeName="textSize"
    motion:customDimension="14sp" />

结果,看起来大小实际上在变化,但比 14sp-16sp 大得多

那么,如何正确改变文字大小呢?

【问题讨论】:

标签: android android-layout android-motionlayout


【解决方案1】:

试试这个

<CustomAttribute
    motion:attributeName="textSize"
    motion:customFloatValue="14" />

【讨论】:

  • 会是 sp 的大小,而不是物理像素吗?
  • 对不起,如果你使用这个你不能在 sp 或像素中传递值,你可以在 float 中传递值。
【解决方案2】:

尝试使用 scale 属性为 font size 设置动画,因为它是 Double 类型而不是 textSize (Integer)。动画会更干净,因为 Double 缩放更平滑。

<ConstraintSet 
        android:id="@id/start">
    <Constraint
        android:id="@id/element"   
        android:scaleX="1.0"
        android:scaleY="1.0"/>
</ConstraintSet>

<ConstraintSet
        android:id="@id/end">
    <Constraint
        android:id="@id/element"   
        android:scaleX="0.5"
        android:scaleY="0.5"/>
</ConstraintSet>

【讨论】:

  • 这个解决方案比动画textSize可靠得多。首先,使用setTextSize 设置的文本大小值与使用android:textSize 设置的值不匹配。其次,通过设置android:transformPivotXandroid:transformPivotY,可以在需要的地方放置文本缩放轴。
  • 很高兴您分享了这种方法,其他类似使用 motion:customFloatValue 的回答会出现奇怪的闪烁,如果我快速上下拖动,则在转换过程中会丢失一些字母跨度>
  • 缩放不影响布局,包括wrap_contentMotionLayout的大小。
【解决方案3】:

以上答案是正确的。因为它是一个CustomAttribute,其值为customFloatValue,所以您必须传入一个浮点数,而不是一个可缩放的像素尺寸。

然而,自定义属性textSizeTextView 上使用时只调用setTextSize,正如您所见here 将给定的浮点数解释为可伸缩类型,因此它会自动将其设置为sp!多田!

【讨论】:

    【解决方案4】:

    使用“motion:customDimension”更改 textSize。下面的文件是 scene.xml

      <?xml version="1.0" encoding="utf-8"?>
    <MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:motion="http://schemas.android.com/apk/res-auto">
    
        <Transition
            motion:constraintSetEnd="@+id/end"
            motion:constraintSetStart="@+id/start"
            motion:duration="2000">
            <OnSwipe
                motion:dragDirection="dragDown"
                motion:moveWhenScrollAtTop="true"
                motion:touchAnchorSide="bottom"
                motion:touchRegionId="@+id/ivExpand" />
        </Transition>
    
        <ConstraintSet android:id="@+id/start">
    
        <Constraint
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <CustomAttribute
                    motion:attributeName="textSize"
                    motion:customDimension="10sp" />
    
            </Constraint>
    
     </ConstraintSet>
    
        <ConstraintSet android:id="@+id/end">
     <Constraint
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <CustomAttribute
                    motion:attributeName="textSize"
                    motion:customDimension="5sp" />
    
            </Constraint>
        </ConstraintSet>
    

    在mainActivity.xml中添加scene.xml(场景描述文件)

    <androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
                <?xml version="1.0" encoding="utf-8"?>
        <MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:motion="http://schemas.android.com/apk/res-auto">
    
     <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tvName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"/>
    </<androidx.constraintlayout.motion.widget.MotionLayout>
    

    【讨论】:

      【解决方案5】:

      我在同一件事上苦苦挣扎,并设法让它发挥作用。如果您使用的是运动编辑器,则启动约束集很可能是基于布局的。您必须在开始约束集上手动指定 textSize:

      <ConstraintSet android:id="@+id/start">
          <Constraint
              android:id="@+id/tv_car_name"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintBottom_toBottomOf="@id/iv_car"
              app:layout_constraintStart_toStartOf="parent">
              <CustomAttribute
                  app:attributeName="textSize"
                  app:customFloatValue="30"/>
          </Constraint>
      </ConstraintSet>
      

      最后,对于结束约束集:

      <ConstraintSet android:id="@+id/end">
          <Constraint
              android:id="@+id/tv_car_name"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintBottom_toBottomOf="@id/iv_car"
              app:layout_constraintStart_toStartOf="parent">
              <CustomAttribute
                  app:attributeName="textSize"
                  app:customFloatValue="15"/>
          </Constraint>
      </ConstraintSet>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-29
        • 2014-05-21
        • 2021-05-29
        • 2021-10-30
        • 2021-12-26
        • 2019-06-20
        • 1970-01-01
        • 2023-03-05
        相关资源
        最近更新 更多