Android动画学习笔记

来源:互联网 发布:磁条卡读写软件 编辑:程序博客网 时间:2024/04/20 19:50

今天将安卓动画的知识重新学习了一遍,利用百度脑图,画了一个如下的思维导图,也对安卓动画有了一个更全面的了解。
这里写图片描述
图片必须放大才能看得清…


下面说一些个人学习心得:
1、View Animation执行后,并未改变View的真是布局属性,而Property Animation则会改变。
比如:在屏幕左上方有一个Button,通过View Animation最后显示在屏幕右下方。这个时候点击右下方显示Button不会响应Button的点击事件,点击屏幕左上方原Button所在的位置才会响应点击事件。若执行的是Property Animation,则Button显示在哪,点那就会响应点击事件。

2、Property Animation所使用的类是Animator。通过xml定义动画文件,在java文件中展示的代码如下:

Animator animator = AnimatorInflater.loadAnimator(this, R.anim.view_animator);animator.setTarget(view);animator.start();

而View Animation使用的则是如下代码:

Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_animator);view.startAnimation(animation);

切记不要把AnimationAnimator搞混了。
补充一点,在使用Property Animation的时候,存放动画xml的文件夹名称需为“animator“(View Animation为”anim“),否则编译器会提示(但不报错)。
这里写图片描述

以下是res目录下文件名称及意义

  • animator/ – files defining property animations
  • anim/ - files that define tween animations
  • color/ - files that define a state list of colors
  • drawable/ - image files
  • layout/ - files defining the User Interface layout
  • menu/ - files that define the app’s menus
  • raw/ - files in their raw form
  • values/ - files containing simple values, like strings, dimensions and colors
  • xml/ - any other xml files

3、执行动画时,若不指定Interpolator,则系统默认使用AccelerateDecelerateInterpolator。

protected void ensureInterpolator() {        if (mInterpolator == null) {            mInterpolator = new AccelerateDecelerateInterpolator();        }    }

该Interpolator的说明如下:

/** * An interpolator where the rate of change starts and ends slowly but * accelerates through the middle. */

也就是开始和结尾慢,中间加速的一个差值器。
这会导致一些意料之外的问题,比如:旋转动画的重复执行。
当动画将要执行完第一次,准备执行第二次的时候,AccelerateDecelerateInterpolator的速率是最慢的,这时将会导致视觉效果上给人一种卡顿的现象。若要解决这个问题,直接给动画设置一个匀速的Interpolator即可。

animation.setInterpolator(new LinearInterpolator());

4、android:ordering属性:利用xml定义动画时,可以通过该属性,设置动画的执行顺序(”together” | “sequentially”)。但是在Android Studio中键入“order”的时候,并没有“android:ordering”的代码补全,也不知道是什么原因。该属性只对Property Animation有效。

5、通过AnimationSet或着AnimatorSet设置重播是不起作用的。若要在动画Set中设置重播,则可使用监听方法,在动画执行结束后,重新开始动画。

6、执行属性动画,传入的属性名称,在View或者其子类里,一定有相应的set、get方法。

ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f);  public void setAlpha(float value);  public float getAlpha();  

7、使用AnimatorSet进行动画组合时,调用setDuration方法设置的时间并不是动画的总时间,而是每个子动画的时间。

    /**     * Sets the length of each of the current child animations of this AnimatorSet. By default,     * each child animation will use its own duration. If the duration is set on the AnimatorSet,     * then each child animation inherits this duration.     *     * @param duration The length of the animation, in milliseconds, of each of the child     * animations of this AnimatorSet.     */    @Override    public AnimatorSet setDuration(long duration)

例如如下代码,动画的执行时间实际为2秒。moveIn执行1秒,rotate与fadeInOut并列执行1秒。

ObjectAnimator moveIn = ObjectAnimator.ofFloat(view, "translationX", -500f, 0f);ObjectAnimator rotate = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f);ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f, 1f);AnimatorSet animSet = new AnimatorSet();animSet.play(rotate).with(fadeInOut).after(moveIn);animSet.setDuration(1000);animSet.start();

8、AnimatorSet添加监听时若传入AnimatorListener类,则需要复写onAnimationStart(动画开始的时调用),onAnimationRepeat(动画重复执行的时调用),onAnimationEnd(动画结束的时调用)onAnimationCancel(动画被取消的时调用)4个方法。但是很多情况下,我们可能只需要监听某一种状态,这时还要复写4个方法就显得十分繁杂了。此时可以传入AnimatorListenerAdapter类。AnimatorListenerAdapter将上述方法都空实现了一遍,这时我们想复写什么方法就复写什么方法就好了。

anim.addListener(new AnimatorListenerAdapter() {      @Override      public void onAnimationEnd(Animator animation) {      }  });------------------------------------------------------------------------------/** * This adapter class provides empty implementations of the methods from {@link android.animation.Animator.AnimatorListener}. * Any custom listener that cares only about a subset of the methods of this listener can * simply subclass this adapter class instead of implementing the interface directly. */public abstract class AnimatorListenerAdapter implements Animator.AnimatorListener,        Animator.AnimatorPauseListener

9、ValueAnimator在调用ofFloat、ofObject等方法时,不用传targetObject,而ObjectAnimator需要。即ValueAnimator只改变属性,这些属性跟View是否有关,是否显示在UI上,它不管。而ObjectAnimator则是需要执行在一个具体的Object上。

10、在调用ofObject时,需要传入TypeEvaluator,需要自己定义类,实现evaluate方法。evaluate方法,是告诉系统如何从起始状态走到结束状态。自定义TypeEvaluator类则是比较高阶的用法了,需要时间,在以后的工作学习中慢慢掌握。
在调用ofFloat、ofInt时,系统也会传入默认的FloatEvaluator、IntEvaluator。

0 0
原创粉丝点击