码迷,mamicode.com
首页 > 移动开发 > 详细

ANDROID动画分类

时间:2015-02-14 23:45:54      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

Android 平台提供了一套完整的动画框架,在Android3.0之前有两种动画Tween Animation(补间动画)和Frame Animation(帧动画),

对应SDK中的View Animation和Drawable Animation。

在Android3.0之后,新增了一种动画Property Animation(属性动画)。

 

一: 补间动画(res/anim/ )

Tween Animation可以对view实现一系列的转换,给出两个关键帧,通过一些算法将给定属性值在给定的时间内在两个关键帧间渐变。可以通过  代码  和  xml 定义,代码中定义用: AlphaAnimation…

特点:

Tween Animation只能应用于View对象,并且只支持一部分属性。它并不会改变属性的值,只是改变了View对象绘制的位置。比如,一个按钮在动画过后,不再原来的位置,但是触发点击事件的任然是原坐标。

xml中必须有一个跟节点,可以是渐变<alpha>, 伸缩<scale>, 移动<translate>, 旋转<rotate>, 或者 <set>。<set>是一个动画集,可以包含前面的一个或多个。

<!-- XML中定义 – -->
<set android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set android:interpolator="@android:anim/decelerate_interpolator">
        <scale
           android:fromXScale="1.4"
           android:toXScale="0.0"
           android:fromYScale="0.6"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400"
           android:fillBefore="false" />
        <rotate
           android:fromDegrees="0"
           android:toDegrees="-45"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400" />
    </set>
</set>

代码中使用:

ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

定义动画的三个步骤 1. 创建动画对象 2. 设置时长 ,3 . 开启动画 、

Interpolators 插值器

可以让动画按照一定的频率运动,实现加速、减速、重复、回弹等效果。

XML 文件存放在:res/anim 下,并定义android:interpolator="@android:anim/.."来使用

二: 帧动画(res/drawable/)

Frame Animation是一系列的图片按顺序显示,来模拟动画的效果。 代码中通过AnimationDrawable来定义。

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>

必须以<animation-list>为根元素,以<item>表示要轮换显示的图片,duration属性表示各项显示的时间。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.setBackgroundResource(R.drawable.drawable_anim);
        anim = (AnimationDrawable) imageView.getBackground();
    }

    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            anim.stop();
            anim.start();
            return true;
        }
        return super.onTouchEvent(event);
    }

注意事项:

  • 要在代码中调用Imageview的setBackgroundResource方法,如果直接在XML布局文件中设置其src属性当触发动画时会FC
  • 在动画start()之前要先stop(),不然在第一次动画之后会停在最后一帧,这样动画就只会触发一次
  • 最后一点是SDK中提到的,不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()

 

三 : 属性动画

Property Animation更改的是对象的实际属性,如Button的缩放,Button的位置与大小属性值都改变了。它不止可以应用于View,还可以应用于almost任何对象,在任何时候(即使没有draws to the screen)。Property Animation只是表示一个值在一段时间内的改变,当值改变时要做什么事情完全是你自己决定的。

Property Animation允许我们定义的属性:

  • Duration :时长 ,default length is 300 ms.
  • Time interpolation :属性值的计算方式,如先快后慢
  • Repeat count and behavior:重复次数与方式
  • Animator sets :动画集合,存放几个动画,这些动画可以同时播放也可以设置不同的偏移
  • Frame refresh delay:多少时间刷新一次 ,default is 10 ms ,受系统进程调度与硬件的影响

一: ValueAnimator :包含Property Animation动画的所有核心功能,如动画时间,开始、结束属性值,相应时间属性值计算方法等。

应用ValueAnimator有两个步聚:

  1. 计算属性值
  2. 根据属性值执行相应的动作,如改变对象的某一属性。

第一步 ValuAnimiator 已经完成了,我们只需要实现ValueAnimator.onUpdateListener接口

ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setDuration(1000);
animation.addUpdateListener(new AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
       TextView tv = new TextView(getApplication());
              tv.setTranslationX((Float) animation.getAnimatedValue());
    }
});
animation.setInterpolator(new CycleInterpolator(3));
animation.start();

二:ObjectAnimator :它是ValueAnimator的子类,它完成了ValueAnimator中前两步 操作

为了让 ObjectAnimator 正确的更新属性, 我们必须做下面的操作:

  • 对象应该有一个setter函数:set<PropertyName>(驼峰命名法),例如, 如果属性是foo, 你必须有一个 setFoo() 方法,如果没有,则只能使用ValueAnimator
  • 如上面的例子中,像ofFloat之类的工场方法,第一个参数为对象名,第二个为属性名,后面的参数为可变参数,如果values…参数只设置了一个值的话,那么会假定为目的值,属性值的变化范围为当前值到目的值,为了获得当前值,该对象要有相应属性的getter方法:get<PropertyName>
  • 如果有getter方法,其应返回值类型应与相应的setter方法的参数类型一致。
tv=(TextView)findViewById(R.id.textview1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    ObjectAnimator oa=ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f);
    oa.setDuration(3000);
    oa.start();
  }
});

在有些情况下,我们可能需要在onAnimationUpdate() callback中 执行invalidate() 方法来强制屏幕重绘。

三:AnimatorSet动画集

在动画集中,我们可以设置组中动画的时序关系,如同时播放,顺序播放等。

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();

四: 动画监听

Animator.AnimatorListener中定义了动画的一些公共接口

onAnimationStart()

onAnimationEnd()

onAnimationRepeat()

//当动画被取消时调用,同时会调用onAnimationEnd().
onAnimationCancel()

注意:

通过观察我们发现AnimatorListenerAdapter这个类 实现了Animator.AnimatorListener接口{每个方法的内部实现都是空} ,我们可以通过继承这个类 {只复写需要复写的方法},而不是实现接口,来达到精简代码的操作。

ObjectAnimator oa=ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f);
oa.setDuration(3000);
oa.addListener(new AnimatorListenerAdapter(){
    public void on AnimationEnd(Animator animation){
        Log.i("Animation","end");
    }
});
oa.start();

ANDROID动画分类

标签:

原文地址:http://www.cnblogs.com/BoBoMEe/p/4292254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!