SVG-Android开源库——关于动画的使用

来源:互联网 发布:苹果 淘宝 编辑:程序博客网 时间:2024/05/23 21:45

Android官方的Vector除了对SVG图片的显示支持外,还支持动画效果,可以与Animator动画框架相结合,实现出各种各样的动画,甚至可以针对SVG图片的部分内容做动画。然而,Vector是5.0版本才开始出现,尽管提供了Support兼容库,但是动画却仍然存在一些兼容问题(主要是AnimatorInflater不兼容)。相对的,SVG-Android不但支持各种动画类型,甚至可以与Animator无缝结合,下面我们来看看如何使用SVG-Android的动画。

SVG-Android开源库相关:
博客:http://blog.csdn.net/megatronkings/article/details/52454927
源码:https://github.com/MegatronKing/SVG-Android


一、帧动画AnimationDrawable

Android中最简单的动画,由于SVGDrawable也是Drawable家族的一种,所以同样适用于AnimationDrawable。

以Sample中的图片旋转动画为例,首先需要定义好一帧帧的图片,由于Vector文件支持rotation属性,我们可以很轻松地搞定一帧帧的这些不同旋转角度图片,只需要修改每个文件的rotation值:

<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="48dp"    android:height="48dp" android:viewportHeight="24.0" android:viewportWidth="24.0">    <group android:rotation="0" android:pivotX="12" android:pivotY="12">        <path android:fillColor="#FFFF0000"            android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z" />    </group></vector>

在定义好这些Vector文件后,接下来运行gradlew svgAssemble命令,或者点击gradle面板的svgAssemble任务,这样就可以生成一帧帧图片对于的Java类。

最后,同定义普通AnimationDrawable一样,我们在xml中定义一个帧动画文件,包括每一帧的停留时间,命名为animation_ic_android.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">    <item android:drawable="@drawable/ic_android_red_01" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_02" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_03" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_04" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_05" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_06" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_07" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_08" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_09" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_10" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_11" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_12" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_13" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_14" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_15" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_16" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_17" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_18" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_19" android:duration="100"/>    <item android:drawable="@drawable/ic_android_red_20" android:duration="100"/></animation-list>

这样,我们就能够使用AnimationDrawable了,既可以在layout文件中配置,也可以在Java中动态设置,比如:

<ImageView    android:id="@+id/animation_drawable_image"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/animation_ic_android"/>
ImageView imageView = (ImageView) findViewById(R.id.animation_drawable_image);AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();animationDrawable.start();

当然,除了旋转动画外,还支持平移、缩放、颜色、透明等,甚至可以改变图片内容,功能非常地强大。相对于普通的PNG图片帧动画来说,使用SVGDrawable的AnimationDrawable帧动画,除了功能强大外,还具有高性能的优点,因为在初始化帧动画的时候不需要将每一帧图片(Bitmap)创建到内存中,而是在动画执行时创建,这种方式能够大幅降低OOM的发生。

不过,很明显这种方式也有一些弊病,比如图片利用率低,仅仅是改变了旋转角度,就需要创建每一帧的图片,其次依然突破不了帧动画的流畅性瓶颈,除非提高的帧数。

这里写图片描述


二、属性动画ObjectAnimator

若论动画,在Android中最强大的非属性动画Animator莫属。SVG-Android从1.1.0版本开始支持属性动画,如果要使用SVG图片属性动画,必须将support库升级到1.1.0版本及以上,最新版本是1.3.1。

dependencies {    compile 'com.github.megatronking:svg-support:1.3.0'}

SVG-Support提供了基础的扩展View来支持属性动画(注意这里的属性动画是针对于Drawable而不是View),比如SVGImageView、SVGTextView等等。

下面,我们以SVGImageView为例,看一下属性动画在SVG图片中的应用。

<com.github.megatronking.svg.support.extend.SVGImageView    android:id="@+id/animation_svgimageview_image"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/ic_android_red" />
SVGImageView imageView = (SVGImageView) findViewById(R.id.animation_svgimageview_image);ObjectAnimator animatorRotation = ObjectAnimator.ofFloat(imageView, "svgRotation", 0, 360);animatorRotation.setDuration(2000);animatorRotation.setRepeatCount(ValueAnimator.INFINITE);animatorRotation.setInterpolator(new LinearInterpolator());animatorRotation.start();

这里定义了一个ObjectAnimator,关联的是SVGImageView的svgRotation属性,运行后我们能看到,图片在循环执行旋转动画,效果同前面的帧动画一模一样。

关键的是,属性动画是针对与SVGDrawable执行动画,而不是针对于View。当然,这里也可以直接使用View的rotation属性执行动画,只不过效果是一样的。

另外,除了svgRotation属性,还支持svgAlpha、svgColor、svgWidth、svgHeight四个属性。

这里写图片描述


三、终极属性动画AnimatedSVGDrawable

前面讲了两种动画的实现,但效果并不完美,现在我们来看第二种方案的升级版AnimatedSVGDrawable,可以支持alpha、rotation、scaleX、scaleY、translationX、translationY、pivotX、pivotY,并支持多个动画的组合使用。

AnimatedSVGDrawable是对SVGDrawable的封装扩展,直接继承于Drawable,依赖图片渲染类SVGRenderer,但我们可以通过SVGDrawable来构造。

AnimatedSVGDrawable支持绑定一个或多个Animator来执行,支持使用定义成xml的animator文件。

先来看一下AnimatedSVGDrawable的实例化方式,其中ic_android_red是框架自动生成SVGRenderer渲染类。

AnimatedSVGDrawable svgDrawable = new AnimatedSVGDrawable(new ic_android_red(this));

同样以前面的旋转效果为例,首先定义一个animator文件,放到res/animator目录下:

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"    android:propertyName="rotation"    android:valueFrom="0"    android:valueTo="360"    android:valueType="floatType" />

除了使用xml方式,在Java代码中定义ObjectAnimator也是可以的。然后,我们可以使用AnimatedSVGDrawable的setupAnimators方法来绑定这个动画:

setupAnimators(this, R.animator.animation_rotation)

最后一步,调用start方法,就可以启动旋转动画了,如果要立刻结束动画,可以使用stop方法。

这里写图片描述


本博客不定期持续更新,欢迎关注和交流:

http://blog.csdn.net/megatronkings

1 3
原创粉丝点击