因项目需要,在Android中实现了一个动画,当在Activity中点击特定按钮时,会在屏幕上添加一个ImageView,并按照指定的起点、终点,沿着特定的轨迹运动(例如直线)。
实现方法
实现思路是在Activity的DecorView中添加一个FrameLayout,然后在FrameLayout中添加ImageView,可通过Margin参数指定ImageView的起始位置。然后设置ImageView的动画,使其能运动到终点。当动画结束后,移除FrameLayout。
核心代码如下。项目GitHub链接 https://github.com/jzj1993/AnimationCrash
-
public static boolean startAnim(Activity activity, int fromX, int toX, int fromY, int toY) {
-
try {
-
final ImageView img = new ImageView(activity);
-
img.setImageResource(R.mipmap.ic_launcher);
-
final FrameLayout tempLayout = new FrameLayout(activity);
-
final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
-
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
-
lp.setMargins(fromX, fromY, 0, 0);
-
tempLayout.addView(img, lp);
-
final ViewGroup container = (ViewGroup) activity.getWindow().getDecorView();
-
container.addView(tempLayout, new ViewGroup.LayoutParams(
-
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
-
final Animation anim = new TranslateAnimation(0, toX - fromX, 0, toY - fromY);
-
anim.setDuration(500);
-
anim.setAnimationListener(new Animation.AnimationListener() {
-
@Override
-
public void onAnimationStart(Animation animation) {
-
}
-
@Override
-
public void onAnimationEnd(Animation animation) {
-
container.removeView(tempLayout);
-
}
-
@Override
-
public void onAnimationRepeat(Animation animation) {
-
}
-
});
-
img.startAnimation(anim);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return true;
-
}
错误描述
在大部分安卓手机上运行正常,但是在部分Android 4.0.x系统中(例如Nexus S Android 4.0.3模拟器),如果连续同时执行多个动画,可能会出现NullPointerException,如下:
com.jzj1993.anim E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2488)
at android.view.View.draw(View.java:10981)
at android.widget.FrameLayout.draw(FrameLayout.java:450)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2126)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2026)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
解决方法
将以下代码
@Override
public void onAnimationEnd(Animation animation) {
container.removeView(tempLayout);
}
改为:
@Override
public void onAnimationEnd(Animation animation) {
container.post(new Runnable() {
@Override
public void run() {
container.removeView(tempLayout);
}
});
}
原因分析
以下是官方给出的ViewGroup.removeView方法的JavaDoc注释:
-
public void removeView (View view)
-
Added in API level 1
-
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方法被调用,从而导致错误。没有具体深入的去分析源码,但是经过验证问题确实解决了。