欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android动画之补间动画

程序员文章站 2022-07-12 22:30:08
...

补间动画

可以使用补间动画系统执行补间动画。补间动画计算动画相关的信息包括开始点、结束点、大小、旋转角度以及其他与动画相关的共同点。

一个补间动画能执行一系列简单的变换(位置、大小、旋转角度和透明度)关于一个视图对象的属性。所以,如果有一个TextView,你可以让文本移动、旋转、放大、缩小。如果有背景图片,背景图片将和文本一起被变换。

补间动画可以通过XML文件或Android代码,推荐使用XML文件。因为可读性更好,重用性更好,比硬编码更好替换。

每一个变换获得一个具体变换的参数集合(开始大小,结束大小,开始角度,结束角度等等)和一些常见的参数(开始时间,时长)。为了使几个变换同时开始,使它们的开始时间相同;为了顺序执行,让开始时间加上变换执行的时间。

补间动画的XML文件放在res/anim目录下。该文件有位移根元素,,,,插值器元素或。为了使变换顺序执行,必须设置startOffset属性。

如下:
Android动画之补间动画

屏幕坐标:左上角为(0,0),向右、向下增加。
一些值,比如pointX,能规定相对自身或者相对父控件的关系。根据需要选择合适的格式(50为相对父控件的50%,50%为相对自身的50%)。

代码调用res/anim目录下的hyperspace_jump.xml动画:

ImageView spaceshipImage=(ImageView)findViewById(R.id.spaceshipImage);

Animation hyperspaceJumpAnimation=AnimationUtils.loadAnimation(this,R.anim.hyperspace_jump);

spaceshipImage.startAnimation(hyperspaceJumpAnimation);

startAnimation()可以使用如下方式替代:

给动画设置一个开始时间Animation.setStartTime(),然后给视图设置动画View.setAnimation()。

透明度动画:
XML:res/anim/anim_alpha.xml

<alpha
    android:duration="300"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"/>

Animation animation=AnimationUtils.loadAnimation(this,R.anim.anim_alpha);
imageView.startAnimation(animation);

注:
fromAlpha:动画开始时的透明度;0.0透明,1.0不透明。
toAlpha:动画结束时透明度;0.0透明,1.0不透明。

Android Code:

AlphaAnimation localAlphaAnimation =new AlphaAnimation(0.0,1.0);
localAlphaAnimation.setDuration(300);
imageView.startAnimation(localAlphaAnimation);

缩放动画:
XML:res/anim/anim_scale.xml

<scale
    android:duration="300"
    android:fromXScale="0.0"
    android:fromYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0"/>


Animation animation=AnimationUtils.loadAnimation(this,R.anim.anim_scale);
imageView.startAnimation(animation);

注:

fromXScale:开始时横向(X轴)的大小;1.0指没有改变。fromYScale同理。
toXScale:动画结束时纵向(Y轴)的大小;1.0指没有改变。toYScale同理。
pivotX:对象缩放时,X轴坐标保持不变的位置。pivotY同理。

Android Code:

ScaleAnimation localScaleAnimation =new ScaleAnimation(0.0,1.0,0.0,1,0.5F,0.5F);
localScaleAnimation.setDuration(300);
imageView.startAnimation(localScaleAnimation);

旋转动画:
XML:res/anim/anim_rotate.xml

<rotate
    android:duration="300"
    android:fromDegrees="0.0"
    android:toDegrees="90.0"
    android:pivotX="50%"
    android:pivotY="50%"/>

Animation animation=AnimationUtils.loadAnimation(this,R.anim.anim_rotate);
imageView.startAnimation(animation);

Android Code:

RotateAnimation rotateAnimation=new RotateAnimation(0.0, 90.0, 50%, 50%);
rotateAnimation.setDuration(300);
imageView.startAnimation(rotateAnimation);

注:

构造函数:RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
pivotXType:用于描述pivotXValue的类型。
Animation.ABSOLUTE, 一个具体的数值;
Animation.RELATIVE_TO_SELF,相对自身的百分比;
Animation.RELATIVE_TO_PARENT,相对父控件的百分比。
pivotXValue:一个具体的值后百分比,含义由pivotXType决定。

位移动画:
XML:res/anim/anim_translate.xml

<translate
    android:fromXDelta="0%"
    android:toXDelta="100%"
    android:duration="300"/>

Animation animation=AnimationUtils.loadAnimation(this,R.anim.anim_translate);
imageView.startAnimation(animation);

注:
fromXDelta:浮点数与百分比,开始时X轴的偏移量。表达方式:使用像素相对于正常位置,如:”5”;使用百分比相对于元素自身的宽度,如”5%”,或者相对于父控件的宽度,如”5%p”。formYDelta同理。
toXDelta:结束时X轴的偏移量。toYDelta 同理。

Android Code:

TranslateAnimation translateAnimation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f, Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF,0.0f, Animation.RELATIVE_TO_SELF,0.0f);
translateAnimation.setDuration(300);
imageView.startAnimation(translateAnimation);

动画循环:

Android动画之补间动画

注:
duration:动画持续的时间,毫秒;
fillAfter:动画结束时,是否停在最后一帧;
fillBefore:动画结束时,是否停留在第一帧;
repeatCount:动画循环的次数,默认为 0 次不循环,-1 (Animation.INFINITE)为无限循环;
repeatMode:循环的模式,Animation.REVERSE是从一次动画结束开始,Animation.RESTART是从动画的开始处循环;

插值器( Interpolators)
下面的表指定每个插值器使用的资源:
Android动画之补间动画

interpolator
在XML文件中,通过android:interpolator属性使用。如:

<set android:interpolator="@android:anim/accelerate_interpolator">
...
</set>

Android Code:

RotateAnimation rotateAnimation=newRotateAnimation(0.0, 90.0, 50%, 50%);
rotateAnimation.setDuration(300);
rotateAnimation.setInterpolator(new AccelerateInterpolator());//越转越快
imageView.startAnimation(rotateAnimation);

插值器效果:

AccelerateDecelerateInterpolator():开始和结束慢,中间加速。
AccelerateInterpolator():刚开始慢,一直加速。
AnticipateInterpolator():开始落后,然后急速向前。
AnticipateOvershootInterpolator():开始落后,然后急速向前,超过目标值,再回到目标值。
BounceInterpolator():最后反弹。
CycleInterpolator(float cycles):循环指定的次数,速率随正弦曲线变化。
DecelerateInterpolator()、DecelerateInterpolator(float factor):一开始快,然后减速;factor:速度差的程度。
LinearInterpolator():速率不变。
OvershootInterpolator()、OvershootInterpolator(float tension):急速向前,超过目标值,然后返回;tension:超过的量。

问题:

进过补间动画变换的对象只是外表(大小、位置、透明度等)发生了改变,对象的本来属性并未改变,事件响应的位置也为改变。

相关标签: 视图动画