[英]android:textSize change in MotionLayout
我將使用 MotionLayout 為文本大小更改設置動畫。
我為啟動 state 執行以下操作
<CustomAttribute
motion:attributeName="textSize"
motion:customDimension="16sp" />
以下為結束 state
<CustomAttribute
motion:attributeName="textSize"
motion:customDimension="14sp" />
結果,看起來尺寸實際上在變化,但它比 14sp-16sp 大得多
那么,如何正確更改文本大小?
嘗試這個
<CustomAttribute
motion:attributeName="textSize"
motion:customFloatValue="14" />
嘗試使用scale屬性為字體大小設置動畫,因為它是Double類型而不是textSize (Integer) 。 Animation 會更干凈,因為雙刻度更平滑。
<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>
上面的答案是正確的。 因為它是一個值為customFloatValue
的CustomAttribute
,所以您必須傳入一個浮點數,而不是一個可縮放的像素尺寸。
但是,在TextView
上使用自定義屬性textSize
時,只需調用setTextSize
,正如您在此處看到的,它將給定的浮點數解釋為可擴展類型,因此它將自動將其設置為sp
! 多田!
使用“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>
我在同一件事上苦苦掙扎,並設法讓它發揮作用。 如果您使用的是運動編輯器,則啟動約束集很可能是基於布局的。 您必須在開始約束集上手動指定 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.