Android Animation实现元素在屏幕上按照指定轨迹运动,以及出现NullPointerException的解决方案

因项目需要,在Android中实现了一个动画,当在Activity中点击特定按钮时,会在屏幕上添加一个ImageView,并按照指定的起点、终点,沿着特定的轨迹运动(例如直线)。

实现方法

实现思路是在Activity的DecorView中添加一个FrameLayout,然后在FrameLayout中添加ImageView,可通过Margin参数指定ImageView的起始位置。然后设置ImageView的动画,使其能运动到终点。当动画结束后,移除FrameLayout。

核心代码如下。项目GitHub链接 https://github.com/jzj1993/AnimationCrash

  1. public static boolean startAnim(Activity activity, int fromX, int toX, int fromY, int toY) {

  2. try {

  3. final ImageView img = new ImageView(activity);

  4. img.setImageResource(R.mipmap.ic_launcher);

  5. final FrameLayout tempLayout = new FrameLayout(activity);

  6. final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(

  7. FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);

  8. lp.setMargins(fromX, fromY, 0, 0);

  9. tempLayout.addView(img, lp);

  10. final ViewGroup container = (ViewGroup) activity.getWindow().getDecorView();

  11. container.addView(tempLayout, new ViewGroup.LayoutParams(

  12. ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

  13. final Animation anim = new TranslateAnimation(0, toX - fromX, 0, toY - fromY);

  14. anim.setDuration(500);

  15. anim.setAnimationListener(new Animation.AnimationListener() {

  16. @Override

  17. public void onAnimationStart(Animation animation) {

  18. }

  19. @Override

  20. public void onAnimationEnd(Animation animation) {

  21. container.removeView(tempLayout);

  22. }

  23. @Override

  24. public void onAnimationRepeat(Animation animation) {

  25. }

  26. });

  27. img.startAnimation(anim);

  28. } catch (Exception e) {

  29. e.printStackTrace();

  30. }

  31. return true;

  32. }

错误描述

在大部分安卓手机上运行正常,但是在部分Android 4.0.x系统中(例如Nexus S Android 4.0.3模拟器),如果连续同时执行多个动画,可能会出现NullPointerException,如下:

  1. com.jzj1993.anim E/AndroidRuntime﹕ FATAL EXCEPTION: main
  2. java.lang.NullPointerException
  3. at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2488)
  4. at android.view.View.draw(View.java:10981)
  5. at android.widget.FrameLayout.draw(FrameLayout.java:450)
  6. at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2126)
  7. at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026)
  8. at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
  9. at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
  10. at android.os.Handler.dispatchMessage(Handler.java:99)
  11. at android.os.Looper.loop(Looper.java:137)
  12. at android.app.ActivityThread.main(ActivityThread.java:4424)
  13. at java.lang.reflect.Method.invokeNative(Native Method)
  14. at java.lang.reflect.Method.invoke(Method.java:511)
  15. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  16. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  17. at dalvik.system.NativeStart.main(Native Method)

解决方法

将以下代码

  1. @Override
  2. public void onAnimationEnd(Animation animation) {
  3. container.removeView(tempLayout);
  4. }

改为:

  1. @Override
  2. public void onAnimationEnd(Animation animation) {
  3. container.post(new Runnable() {
  4. @Override
  5. public void run() {
  6. container.removeView(tempLayout);
  7. }
  8. });
  9. }

原因分析

以下是官方给出的ViewGroup.removeView方法的JavaDoc注释:

  1. public void removeView (View view)

  2. Added in API level 1

  3. Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

猜测在同时执行多个动画时,前一个动画的onAnimationEnd被回调时,后一个动画正在执行,因此DecorView的onDraw方法在执行,可能会导致DecorView.onDraw执行时,onAnimationEnd回调中的DecorView.removeView方法被调用,从而导致错误。没有具体深入的去分析源码,但是经过验证问题确实解决了。