在MotionLayout中更改android:textSize

12
我将使用MotionLayout来制作文本大小变化的动画。
我对起始状态进行如下设置:
<CustomAttribute
    motion:attributeName="textSize"
    motion:customDimension="16sp" />

接下来是结束状态的内容

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

作为结果,看起来尺寸实际上正在改变,但比14sp-16sp大得多。那么,如何正确地更改文本大小?

尝试这个:https://dev59.com/4LDla4cB1Zd3GeqP5Dwa - Rahul Khatri
5个回答

12

试试这个

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

1
它将是sp大小,而不是物理像素吗? - Artyom
抱歉,如果您使用此功能,则无法传递sp或像素值,您必须传递浮点数值。 - Sk Suraj

8
尝试使用scale属性来动画字体大小,因为它是类型而不是类型的textSize。使用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>

3
这个解决方案比动画textSize更可靠。首先,使用setTextSize设置的文本大小值与使用android:textSize设置的不匹配。其次,通过设置android:transformPivotXandroid:transformPivotY,您可以将文本缩放中心放置在需要的位置。 - Slav
3
很高兴你分享了这种方法,其他答案使用 motion:customFloatValue 会出现奇怪的闪烁,并且在快速上下拖动时会有一些字母消失。 - MDT
缩放不会影响布局,包括 wrap_content MotionLayout 的大小。 - Agent_L

5
上述答案是正确的。因为它是一个值为customFloatValue的CustomAttribute,所以您必须传递一个浮点数,而不是可缩放像素维度。
然而,当在TextView上使用textSize自定义属性时,它只是调用setTextSize,正如您可以在此处看到的,它将给定的浮点数解释为可缩放类型,因此它会自动将其设置为sp!Tada!

0

我曾在同样的问题上苦苦挣扎,最终成功解决。如果你正在使用动画编辑器,很可能起始约束集是基于布局的。你必须在起始约束集中手动指定文本大小:

<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:
<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>

0

使用“motion:customDimension”来更改文本大小。下面的文件是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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接