SVG-Vector-ObjectAnimator 实现的有趣而强大的动画效果

开始上手

废话不多说,直接上代码块

1、res/drawable/splash_logo.xml,静态SVG图片

核心代码就是这个pathData,此代码参考https://github.com/18601949127/DiDiCallCar,滴滴出行logo动画

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="72dp"
    android:height="50dp"
    android:viewportWidth="72.0"
    android:viewportHeight="50.0">

    <path
        android:name="animate_logo_target"
        android:pathData="M60,5,L10,5,V15,A1,1,0,0,0,65,20,L65,10"
        android:strokeWidth="10"
        android:strokeColor="@color/colorAccent" />
</vector>

2、res/animator/logo_animator.xml,动画效果

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:propertyName="trimPathEnd"
    android:repeatCount="0"
    android:valueFrom="0"
    android:valueTo="1"
    android:valueType="floatType"
    />

3、res/drawable/logo_anim.xml,把上面两个结合起来

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/splash_logo">
<target
    android:name="animate_logo_target"
    android:animation="@animator/logo_animator"/>
</animated-vector>

4、res/layout/activity_main.xml

  <ImageView
            android:id="@+id/image_logo"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:layout_gravity="center"
            android:focusable="true" />

5、MainActivity.java,这里可以用handler.postDelay一个runnable配合动画的播放,来达到实现欢迎动画的效果

 ImageView logo = findViewById(R.id.image_logo);
        AnimatedVectorDrawableCompat animatedVectorDrawableCompat = AnimatedVectorDrawableCompat.create(
                this, R.drawable.logo_anim
        );
        logo.setImageDrawable(animatedVectorDrawableCompat);
        ((Animatable) logo.getDrawable()).start();

或者

AnimatedVectorDrawable anim =
            (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.logo_anim);
ImageView logo = findViewById(R.id.image_logo);
logo.setImageDrawable(anim);
anim.start();

参考博文

猜你喜欢

转载自blog.csdn.net/diyangxia/article/details/94742044