1. 程式人生 > >最近使用了TextPathView這個元件記錄一下

最近使用了TextPathView這個元件記錄一下

 

.

介紹

使用

  TextPathView是一個把文字轉化為路徑動畫然後展現出來的自定義控制元件。效果如上圖。

Gradle

compile 'com.yanzhikai:TextPathView:0.1.3'

minSdkVersion 16

如果遇到播放完後消失的問題,請關閉硬體加速,可能是硬體加速對drawPath()方法不支援

 

 使用方法

TextPathView

  TextPathView分為兩種,一種是每個筆畫按順序刻畫的SyncTextPathView,一種是每個筆畫同時刻畫的AsyncTextPathView,使用方法都是一樣,在xml裡面配置屬性,然後直接在java裡面呼叫startAnimation()方法就行了,具體的可以看例子和demo。下面是一個簡單的例子:

xml裡面:

 

<yanzhikai.textpath.SyncTextPathView
         android:id="@+id/stpv_2017"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         app:duration="12000"
         app:showPainter="true"
         app:text="2017"
         app:textInCenter="true"
         app:textSize="60sp"
         android:layout_weight="1"
         />

    <yanzhikai.textpath.AsyncTextPathView
        android:id="@+id/atpv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:duration="12000"
        app:showPainter="true"
        app:text="炎之鎧"
        app:textStrokeColor="@android:color/holo_orange_light"
        app:textInCenter="true"
        app:textSize="62sp"
        android:layout_gravity="center_horizontal"
        />

java裡面使用:

 atpv1 = findViewById(R.id.atpv_1);
        stpv_2017 = findViewById(R.id.stpv_2017);

        //從無到顯示
        atpv1.startAnimation(0,1);
        //從顯示到消失
        stpv_2017.startAnimation(1,0);

還可以通過控制進度,來控制TextPathView顯示,這裡用SeekBar:

 sb_progress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                atpv1.drawPath(progress / 1000f);
                stpv_2017.drawPath(progress / 1000f);

            }
        }

PathView

  PathView是0.1.1版本之後新增的,擁有三個子類TextPathView、SyncPathView和AsyncPathView,前者上面有介紹是文字的路徑,後面這兩個就是圖形的路徑,必須要輸入一個Path類,才能正常執行:

 

 

 

public class TestPath extends Path {
    public TestPath(){
        init();
    }

    private void init() {
        addCircle(350,300,150,Direction.CCW);
        addCircle(350,300,100,Direction.CW);
        addCircle(350,300,50,Direction.CCW);
        moveTo(350,300);
        lineTo(550,500);
    }
}
 //必須先呼叫setPath設定路徑
        aspv.setPath(new TestPath());
        aspv.startAnimation(0,1);

 只使用了這麼多功能

作者地址:https://github.com/totond/TextPathView