您的位置:首页 > 移动开发 > Android开发

Android动画 SVG VectorDrawable 基础三

2017-07-03 18:38 555 查看

使用动态的VectorDrawable



下面来做一个案例

让箭头左右移动



定义两个 object动画

animator/anim_left.xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:propertyName="translateX"
android:valueFrom="0"
android:valueTo="10"
android:valueType="floatType">
</objectAnimator>


animator/anim_right.xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:propertyName="translateX"
android:valueFrom="10"
android:valueTo="0"
android:valueType="floatType">
</objectAnimator>


VectorDrawable 文件

drawable/arrow.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<group  android:name="left">
<path
android:fillColor="#FF000000"
android:pathData="M9.01,14L2,14v2h7.01v3L13,15l-3.99,-4v3z"/>
</group>
<group android:name="right">
<path
android:fillColor="#FF000000"
android:pathData="M14.99,13v-3L22,10L22,8h-7.01L14.99,5L11,9l3.99,4z"/>
</group>
</vector>


让 drawable文件与 animator绑定起来

drawable/arrow_anim.xml

<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/arrow">
<target
android:animation="@animator/anim_left"
android:name="left"/>

<target
android:animation="@animator/anim_right"
android:name="right"/>
</animated-vector>


使用他们

<ImageView
android:onClick="anim"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/arrow_anim"/>


public void anim(View v){
ImageView iv  = (ImageView) v;
Drawable drawabl
4000
e = iv.getDrawable();
if(drawable instanceof Animatable){
((Animatable)drawable).start();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  动画 android svg