<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ConstraintLayout2.0一篇寫不完之MotionLabel

          共 2476字,需瀏覽 5分鐘

           ·

          2021-06-03 20:29

          點擊上方藍字關注我,知識會給你力量


          MotionLabel是CL2.1新加入的一個為單行文字制作動畫的View,它支持下面幾種典型的動畫效果:

          • 使用PanX和PanY取代Gravity,它是一個float值,-1是左,0是中心,+1是右,它可以對位置進行動畫
          • 可以設置文本輪廓的厚度和顏色,并設置它的動畫效果
          • 給文本的背景設置顏色或紋理
          • 確保textSize的動畫效果順滑
          ?

          那么問題來了,只支持單行??沒錯,MotionLabel并不是繼承TextView,所以文字測量相關的方法是沒有的,只能支持單行,那么多行咋辦,其實很簡單,你可以借助一個假的TextView來進行文字的測量,再通過它的API獲取每行的文本,根據(jù)每行的文本,創(chuàng)建MotionLabel,或者更直接一點,通過TextMeasure來對文字進行測量。

          ?

          紋理背景

          下面的示例,展示了MotionLabel設置圖片紋理作為它的background,主要涉及三個屬性:

          • textBackground:不同于background,textBackground是文字的填充紋理
          • textureHeight:文字填充紋理的高度
          • textureWidth:文字填充紋理的寬度
          • textBackgroundRotate:文字填充紋理的旋轉角度
          • textBackgroundZoom:文字填充紋理的縮放
          ?

          當不設置textureWidth和textureHeight時,紋理默認撐滿文字。

          ?
          <androidx.constraintlayout.utils.widget.MotionLabel
              android:id="@+id/label1"
              android:layout_width="wrap_content"
              android:layout_height="200dp"
              android:background="@drawable/monterey"
              android:padding="10dp"
              android:text="XuYiSheng"
              android:textSize="60sp"
              android:textStyle="bold"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:textBackground="@drawable/avatar"
              app:textureHeight="10dp"
              app:textureWidth="10dp" />
          image-20210527192322811
          ?

          借助文字紋理,可以很方便的實現(xiàn)文字的Blur效果,實際上就是一張正常圖片墊底,另一個MotionLabel使用背景圖的Blur圖做文字背景來實現(xiàn)的。

          ?
          image-20210527195400855

          TextOutline

          MotionLabel還支持對文字添加描邊處理,涉及到下面這些屬性。

          • textOutlineColor:描邊顏色
          • textOutlineThickness:描邊寬度
          image-20210527192747621

          borderRound

          MotionLabel自帶了背景圓角的處理。

          • borderRound:按dp處理圓角
          • borderRoundPercent:按百分比處理圓角

          textBackgroundPan

          文字紋理背景的偏移,是通過textBackgroundPanX和textBackgroundPanY屬性來實現(xiàn)的。

          通過CustomAttribute給textBackgroundPanX和textBackgroundPanY設置-1到1的變換,就可以實現(xiàn)紋理背景移動的動畫效果。

          textPan

          文字在MotionLabel中的偏移,使用的是textPanX和textPanY,同樣是通過-1到1來實現(xiàn)定位。

          通過CustomAttribute給textPanX和textPanY設置-2到2的變換,就可以實現(xiàn)文字的偏移動畫。

          動畫就不貼了,太麻煩了,最后會有一個驚喜大Demo,滿足你的一切幻想。


          向大家推薦下我的網(wǎng)站 https://xuyisheng.top/  點擊原文一鍵直達

          專注 Android-Kotlin-Flutter 歡迎大家訪問



          往期推薦


          本文原創(chuàng)公眾號:群英傳,授權轉載請聯(lián)系微信(Tomcat_xu),授權后,請在原創(chuàng)發(fā)表24小時后轉載。
          < END >
          作者:徐宜生

          更文不易,點個“三連”支持一下??


          瀏覽 53
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品卡一卡二卡三 | 大雞巴乱人妻1~14 | 97人妻人人澡人人 | 蜜芽大香蕉 | 亚洲成人免费网 |