SVG:
資料1
資料2
用法:
- VectorDrawable: 創(chuàng)建基于XML的SVG圖形
- AnimatedVectorDrawable:"膠水"瘫拣,實(shí)現(xiàn)動(dòng)畫
- 語(yǔ)法什么的看資料。
創(chuàng)建vector SVG文件:
- 通過(guò)下面xml文件告喊,根據(jù)語(yǔ)法畫: VectorDrawable
- 網(wǎng)站SVG圖片源碼/Studio--> new--> vector Asset/ps麸拄,AI將圖片轉(zhuǎn)換成SVG
//res/drawable目錄
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportHeight="100"
android:viewportWidth="100">
<group>
<!-- 如果指定屬性為 pathData,
則要添加一個(gè)屬性 android:strokeLineCap="pathType",來(lái)告訴系統(tǒng)進(jìn)行pathData變換-->
<path
android:name="path1"
android:pathData="
M 20,20
L 50,20 80,20"
android:strokeColor="#000"
android:strokeLineCap="round"
android:strokeWidth="5" />
<path
android:name="path2"
android:pathData="
M 20,80
L 50,80 80,80"
android:strokeColor="#000"
android:strokeLineCap="round"
android:strokeWidth="5" />
//中間的線黔姜,自己加上去的拢切。
<path
android:name="path3"
android:pathData="
M 20,50
L 45,50 M 55,50 80,50"
android:strokeColor="#000"
android:strokeLineCap="round"
android:strokeWidth="5" />
</group>
</vector>
創(chuàng)建animator:
// res/animtor目錄
<objectAnimator
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:interpolator="@android:anim/linear_interpolator"
android:propertyName="pathData"
android:valueFrom="M 20,20 L 50,20 80,20"
android:valueTo="M 20,20 L 50,50 80,20"
android:valueType="pathType"/>
創(chuàng)建 animated-vector文件:
- "膠水" 把 VectorDrawable 和 objectAnimator 連起來(lái)。
// res/drawable目錄
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
//引入vector
android:drawable="@drawable/svg_lines">
<target
//此處的name必須和vector中的name一致秆吵,才能知道動(dòng)畫要作用到哪個(gè)path
android:name="path1"
//動(dòng)畫
android:animation="@animator/obj_svg_path_1">
</target>
<target
android:name="path2"
android:animation="@animator/obj_svg_path_2">
</target>
</animated-vector>
開(kāi)始動(dòng)畫:
// xml中設(shè)置backgroud淮椰,代碼中用getBackground();
// xml中設(shè)置src屬性,代碼中用getDrawable();
// Animatable animDrawable = (Animatable)ivSvgTarget.getBackground();
Animatable animDrawable = (Animatable) ivSvgTarget.getDrawable();
if (!animDrawable.isRunning()) {
animDrawable.start();
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者