android:textSize 在 MotionLayout 中的变化

Art*_*yom 7 android android-layout android-motionlayout

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

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

<CustomAttribute
    motion:attributeName="textSize"
    motion:customDimension="16sp" />
Run Code Online (Sandbox Code Playgroud)

以及以下结束状态

<CustomAttribute
    motion:attributeName="textSize"
    motion:customDimension="14sp" />
Run Code Online (Sandbox Code Playgroud)

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

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

Sk *_*raj 6

尝试这个

<CustomAttribute
    motion:attributeName="textSize"
    motion:customFloatValue="14" />
Run Code Online (Sandbox Code Playgroud)


kja*_*on2 5

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

但是,自定义属性textSizeTextView简单调用时使用setTextSize,正如您在此处看到的那样,它会将给定的浮点解释为可扩展类型,因此它将sp自动将其设置为!田田!


Irw*_*cki 5

尝试使用scale属性为字体大小设置动画,因为它是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>
Run Code Online (Sandbox Code Playgroud)

  • 很高兴您分享了这种方法,其他类似使用 **motion:customFloatValue** 的方法会产生奇怪的闪烁,如果我快速上下拖动,一些字母会在转换过程中丢失 (3认同)
  • 该解决方案比动画“textSize”可靠得多。首先,使用“setTextSize”设置的文本大小值与使用“android:textSize”设置的文本大小值不匹配。其次,通过设置`android:transformPivotX`和`android:transformPivotY`,您可以将文本缩放枢轴放置在您需要的地方。 (2认同)