Android动画

来源:互联网 发布:android连接数据库 编辑:程序博客网 时间:2024/05/17 01:17
在XML中定义动画:
1.在XML中有两个文件可以用来设置动画的XML,一个文件夹名字为anim,另一个是animator,却别是: 
(1).anim用于设置帧动画和补间动画,下面的标签有alpha scale rotate translate set
(2).animator用于设置属性动画,下面的标签有animator objectAnimator set
2.从XML中加载动画的方式分别为:
(1).anim的方式是AnimationUtils.loadAnimation()
(2).animator的方式是AnimatorInflater.loadAnimator()
3.给组件设置动画的方式分别为:
(1).anim的方式是mView.setAnimation()或者直接开始动画mView.startAnimation()
(2).animator的方式是mAnimation.setTarget(mView)然后使用mAnimation.start()开始动画
另外注意属性动画和补间动画的区别:
对于补间动画,开启了它的动画之后,当动画结束了,它还会回到之前的状态,比如移动了之后还会回到初始的位置,
透明度变成0之后还会恢复原来100%的状态。而属性动画就不一样了,它会改变一个组件的属性,也就是说比如进行了移
动动画,将组件移动到了指定的位置,那么它就待在那里了,动画结束也不会再回来了。
4.帧动画的使用方式:注意它的定义不是在anim也不是在animator文件夹下的,而是在drawable文件夹下面
帧动画的实现方式和普通的补间动画稍微有些不同,它要先为ImageView设置drawable然后在从中获取动画对象。
(1).首先在XML中定义帧动画:
<animation-list android:oneshot="false"xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/record0" android:duration="500"/><item android:drawable="@drawable/record1" android:duration="500"/><item android:drawable="@drawable/record2" android:duration="450"/><item android:drawable="@drawable/record3" android:duration="400"/><item android:drawable="@drawable/record4" android:duration="350"/><item android:drawable="@drawable/record5" android:duration="400"/><item android:drawable="@drawable/record6" android:duration="400"/></animation-list>
其中oneshot是指动画是否仅播放一次,上面的例子就是一个录音时的模仿微信的效果。
(2).在程序中获取的方式是:
ImageView recordImage = (ImageView) findViewById(R.id.record_image);    recordImage.setImageResource(R.drawable.record_anim);    AnimationDrawable animDraw = (AnimationDrawable) recordImage.getDrawable(); // 获取XML中定义的动画animDraw.start(); // 开启动画animDraw.stop(); // 停止动画
5.移动补间动画
<set android:duration="1000"    xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:fromXDelta="50"        android:fromYDelta="100"        android:toXDelta="0"        android:toYDelta="0"/></set>
早期的教材中会提到在XML中使用interpolator,但是现在好像没有这个属性了,不过在程序中这个属性仍然是可以使用的。
另外Animation是具有一些方法的,可以查找官方文档。
6.缩放补间动画
<set android:duration="1000"    xmlns:android="http://schemas.android.com/apk/res/android">    <scale        android:fromXScale="1"        android:fromYScale="1"        android:toYScale="0"        android:toXScale="0"        android:pivotX="50%"        android:pivotY="50%"/></set>
7.旋转补间动画
<set android:duration="1000" xmlns:android="http://schemas.android.com/apk/res/android">    <rotate        android:fromDegrees="0"        android:toDegrees="720"        android:pivotX="50%"        android:pivotY="50%"/></set>
8.透明度补间动画
<set xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000">    <alpha android:fromAlpha="1"        android:toAlpha="0" /></set>
注意在XML中这几种补间动画是可以放置在一起的,这样几个动画就在一起执行了。也可以在为组件设置fillAfter等,
这样设置的动画在其他动画完成之后才会触发。另外,这些动画在程序当中都是有它们的类的,比如RotateAnimation 
ScaleAnimation等等。


9.属性动画
(1).一个属性动画的例子是:
<set android:ordering="together"    xmlns:android="http://schemas.android.com/apk/res/android">    <objectAnimator android:duration="1000"        android:propertyName="y"        android:valueTo="300"/>    <objectAnimator        android:duration="1000"        android:propertyName="x"        android:valueTo="200"/></set>
在程序中获取动画并设置的方式是:
    AnimatorSet transAnimSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.property_anim);        transAnimSet.setTarget(iv5);        transAnimSet.start();
(2).在定义属性动画的时候可以使用PropertyValuesHolder和Keyframe标签来让几个动画同时执行。比如,
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"android:duration="1000"android:repeatCount="1"android:repeatMode="reverse">    <propertyValuesHolder android:propertyName="x" android:valueTo="400"/>    <propertyValuesHolder android:propertyName="y" android:valueTo="200"/></objectAnimator>
注意应该将objectAnimator作为XML的根节点,而且在获取的时候要使用ObjectAnimator进行强制转换,而不是AnimatorSet. 即
    ObjectAnimator animHolder = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.property_anim_holder);    animHolder.setTarget(iv6);    animHolder.start();
(3).使用Frame定义属性动画:
<propertyValuesHolder android:propertyName="x" >    <keyframe android:fraction="0" android:value="800" />    <keyframe android:fraction=".2"              android:interpolator="@android:anim/accelerate_interpolator"              android:value="1000" />    <keyframe android:fraction="1"              android:interpolator="@android:anim/accelerate_interpolator"              android:value="400" /></propertyValuesHolder><propertyValuesHolder android:propertyName="y" >    <keyframe/>    <keyframe android:fraction=".2"              android:interpolator="@android:anim/accelerate_interpolator"              android:value="300"/>    <keyframe android:interpolator="@android:anim/accelerate_interpolator"              android:value="1000" /></propertyValuesHolder>
注意fraction在API23中才能使用。
在ObjectAnimation中定义了许多方法用于设置属性动画,比如ofFloat ofInt等等。
Keyframe的意思是指定在某个状态时某个属性的状态。在程序中实现的方式是:
Keyframe kf0 = Keyframe.ofFloat(0f, 0f);Keyframe kf1 = Keyframe.ofFloat(.5f, 360f);Keyframe kf2 = Keyframe.ofFloat(1f, 0f);PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2);ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(target, pvhRotation)rotationAnim.setDuration(5000ms);
上面的意思是"rotation"属性在完成了0%的时候角度为0,0.5%的时候为360,100%的时候是0.
(4).属性动画的顺序
AnimatorSet bouncer = new AnimatorSet();bouncer.play(bounceAnim).before(squashAnim1);bouncer.play(squashAnim1).with(squashAnim2);bouncer.play(squashAnim1).with(stretchAnim1);bouncer.play(squashAnim1).with(stretchAnim2);bouncer.play(bounceBackAnim).after(stretchAnim2);ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);fadeAnim.setDuration(250);AnimatorSet animatorSet = new AnimatorSet();animatorSet.play(bouncer).before(fadeAnim);animatorSet.start();
比如上面的动画的播放顺序是:
1.Plays bounceAnim.
2.Plays squashAnim1, squashAnim2, stretchAnim1, and stretchAnim2 at the same time.
3.Plays bounceBackAnim.
4.Plays fadeAnim.


10.渲染器
Android自带了一些渲染器,比如
ArgbEvaluator, FloatArrayEvaluator, FloatEvaluator, IntArrayEvaluator, IntEvaluator, PointFEvaluator, RectEvaluator
可以使用这些渲染器,只要在动画的setInterpolator方法中传入实例即可。
自定义渲染器的方式实现Interpolator接口,只要实现一个getInterpolation方法即可,比如AccelerateInterpolator中,
该方法是按照如下方式实现的
    public float getInterpolation(float input) {        if (mFactor == 1.0f) {            return input * input;        } else {            return (float)Math.pow(input, mDoubleFactor);        }    }
也就是说,这里只要根据想要的逻辑的数学模型写一个表达式进行计算即可。


另外在这三个博客中已经详细地讲解了很多关于动画的知识(很惊讶于博主居然有如此耐心去做那么详细地讲解),既然如此,就
将地址贴在下面好了,以后需要的时候再来研究:
http://blog.csdn.net/sinyu890807/article/details/43536355
http://blog.csdn.net/sinyu890807/article/details/43816093
http://blog.csdn.net/sinyu890807/article/details/44171115
0 0
原创粉丝点击