Android 中的动画有哪几类,它们的特点和区别是什么

如题所述

Android3.0(即API Level11)前Android仅支持2种画:别Frame Animation(逐帧画)Tween Animation(补间画)3.0Android支持种新画系统称:Property Animation(属性画)

、Frame Animation:(逐帧画)

理解帧帧播放图片利用眼视觉残留原理给我带画觉原理GIF图片、电影播放原理

1.定义逐帧画比较简单要使用元素定义所播放帧即

(1) android:oneshot 设置否仅播放

(2) android:drawable 设置每帧图片

(3) android:duration 设置图片间切换间隔

2.习惯AnimationDrawable设置ImageView背景

android:background=@anim/frame_anim

我java代码获取AnimationDrawable象

AnimationDrawable anim = (AnimationDrawable)imageView.getBackground();

(需要注意AnimationDrawable默认播放调用其start()始播放stop停止播放)

3.面画文件通xml文件配置喜欢通java代码创建AnimationDrawable象通addFrame(Drawable frame, int duration)向画添加帧start()

二、Tween Animation:(补间画)

补间画我需指定始、结束关键帧变化其帧由系统计算必自帧帧定义

1. Android使用Animation代表抽象画包括四种类:AlphaAnimation(透明度画)、ScaleAnimation(缩放画)、TranslateAnimation(位移画)、RotateAnimation(透明度画)Android面允许java创建Animation类象般都采用画资源文件定义画界面与逻辑离

(set同定义画起执行)

2. android:interpolator=@android:anim/linear_interpolator控制画期间需要补入少帧简单说控制画速度些翻译插值Interpolator几种实现类:LinearInterpolator、AccelerateInterpolator、AccelerateDecelerateInterpolator、CycleInterpolator、DecelerateInterpolator具体使用参考官API Demo

3. 定义anim文件我通AnimationUtils工具类加载加载功返Animation通ViewstartAnimation(anim)始执行画

Animation anim = AnimationUtils.loadAnimation(this, R.anim.anim);
//设置画结束保留结束状态
anim.setFillAfter(true);
//设置插值效
anim.setInterpolator(interpolator);
//view执行画
view. startAnimation(anim);

三、Property Animation:(属性画)

属性画Android 3.0才引进直接更改我象属性面提Tween Animation更改View绘画效View真实属性改变假设用Tween画Button左边移右边论点击移Button都没反应点击移前Button位置才反应Button位置属性木改变Property Animation则直接改变View象属性值让我少做些处理工作提高效率与代码读性

(1)ValueAnimator:包含Property Animation画所核功能画间始、结束属性值相应间属性值计算等应用ValueAnimator两步骤

1计算属性值

2根据属性值执行相应作改变象某属性

我主第二步需要实现ValueAnimator.onUpdateListener接口接口函数onAnimationUpdate()要改变View象属性事情该接口do

animation.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//do your work
}
});

(2)ObjectAnimator:继承自ValueAnimator要指定象及该象属性属性值计算完自设置该象相应属性即完Property Animation全部两步操作实际应用般都用ObjectAnimator改变某象某属性用ObjectAnimator定限制要想使用ObjectAnimator应该满足条件:

1.象应该setter函数:set(驼峰命名)

2面例像ofFloat类工场第参数象名第二属性名面参数变参数values…参数设置值假定目值属性值变化范围前值目值获前值该象要相应属性getter:get

3getter其应返值类型应与相应setter参数类型致

ObjectAnimator oa=ObjectAnimator.ofFloat(tv, alpha, 0f, 1f);
oa.setDuration(3000);
oa.start();

满足面条件我能乖乖使用ValueAnimator创建画

(3)Animator.AnimatorListener:Animator设置画监听需要重写面四

onAnimationStart()
onAnimationEnd()
onAnimationRepeat()
onAnimationCancel()

我实现AnimatorListenerAdapter处用定义想监听事件用实现每函数却定义空函数体:

anim.addListener(new AnimatorListenerAdapter() {
public void on AnimationEnd(Animator animation){
//do your work
}
});

(4)AnimationSet:组合画共同工作

AnimatorSet bouncer = new AnimatorSet();
bouncer.play(anim1).before(anim2);
bouncer.play(anim2).with(anim3);
bouncer.play(anim2).with(anim4)
bouncer.play(anim5).after(amin2);
animatorSet.start();

面代码意思: 首先播放anim1;同播放anim2,anim3,anim4;播放anim5

(5)TimeInterplator:与Tweeninterpolator类似几种

AccelerateInterpolator      加速始慢间加速

DecelerateInterpolator       减速始快减速

AccelerateDecelerateInterolator  先加速减速始结束慢间加速

AnticipateInterpolator       反向 先向相反向改变段再加速播放

AnticipateOvershootInterpolator  反向加弹先向相反向改变再加速播放超目值缓慢移至目值

BounceInterpolator        跳跃快目值值跳跃目值100面值能依8577708090100

CycleIinterpolator         循环画循环定数值改变弦函数:Math.sin(2 * mCycles * Math.PI * input)

LinearInterpolator         线性线性均匀改变

OvershottInterpolator       弹超目值缓慢改变目值

TimeInterpolator         接口允许自定义interpolator几都实现接口

(6)Keyframes:让我定义除始结束外关键帧KeyFrame抽象类要通ofInt(),ofFloat(),ofObject()获适KeyFrame通PropertyValuesHolder.ofKeyframe获PropertyValuesHolder象:

Keyframe kf0 = Keyframe.ofInt(0, 400);
Keyframe kf1 = Keyframe.ofInt(0.25f, 200);
Keyframe kf2 = Keyframe.ofInt(0.5f, 400);
Keyframe kf4 = Keyframe.ofInt(0.75f, 100);
Keyframe kf3 = Keyframe.ofInt(1f, 500);
PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(width, kf0, kf1, kf2, kf4, kf3);
ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(btn, pvhRotation);
述代码意思:设置btn象width属性值使其:始 Width=400画始1/4 Width=200画始1/2 Width=400画始3/4 Width=100画结束 Width=500

(7)ViewPropertyAnimator:View同改变种属性非推荐用种该类属性画进行优化合并些invalidate()减少刷新视图且使用起非简便要求API LEVEL 12即Android 3.1仅需要行代码即完水平、竖直移

myView.animate().translationX(50f). translationY(100f);

(8)需要改变些属性:

translationX,translationY: View相于原始位置偏移量

rotation,rotationX,rotationY: 旋转rotation用于2D旋转角度3D用两

scaleX,scaleY: 缩放比

x,y: View终坐标Viewlefttop位置加translationXtranslationY

alpha: 透明度

四、自总结三种画优缺点:

(1)Frame Animation(帧画)主要用于播放帧帧准备图片类似GIF图片优点使用简单便、缺点需要事先准备每帧图片;

(2)Tween Animation(补间画)仅需定义始与结束关键帧变化间帧由系统补优点用准备每帧缺点改变象绘制没改变View本身属性改变按钮位置需要点击原按钮所位置才效

(3)Property Animation(属性画)3.0推画优点使用简单、降低实现复杂度、直接更改象属性、几乎适用于任何象仅非View类缺点需要3.0API支持限制较目前外源库提供低版本支持
温馨提示:答案为网友推荐,仅供参考
相似回答