结论:
overridePendingTransition代码方式在手机动画关闭的状态下是无效的;
自定义theme方式下,Animation.Activity的4个属性中,在动画关或者不关闭的状态下只有两个有效;(推荐使用)
windowEnterAnimation
windowExitAnimation
windowShowAnimation//无效
windowHideAnimation//无效
具体操作方法不赘述了:
2.说说我碰到的问题:
首先是xml方式,因为Activity继承自AppCompatActivity,在继承主题时会报
You need to use a Theme.AppCompat theme (or descendant) with this activity.
错误,需要取用合适的theme
这并不是重点,重点是程序运行起来之后根本没有动画效果,我在确认动画xml文件的参数后,依然没有效果(上面链接的博文中的倒数第二个xml文件的参数设置是有问题的)
//应该是
fromXDelta="100%"
回到重点,无奈之下,查看style.xml源代码,发现了Animation.Activity的一系列参数:
@anim/activity_open_enter
@anim/activity_open_exit
@anim/activity_close_enter
@anim/activity_close_exit
@anim/task_open_enter
@anim/task_open_exit
@anim/launch_task_behind_target
@anim/launch_task_behind_source
@anim/task_close_enter
@anim/task_close_exit
@anim/task_open_enter
@anim/task_open_exit
@anim/task_close_enter
@anim/task_close_exit
@anim/wallpaper_open_enter
@anim/wallpaper_open_exit
@anim/wallpaper_close_enter
@anim/wallpaper_close_exit
@anim/wallpaper_intra_open_enter
@anim/wallpaper_intra_open_exit
@anim/wallpaper_intra_close_enter
@anim/wallpaper_intra_close_exit
@animator/fragment_open_enter
@animator/fragment_open_exit
@animator/fragment_close_enter
@animator/fragment_close_exit
@animator/fragment_fade_enter
@animator/fragment_fade_exit
逐一实验,发现其中只有两项参数对单一的activity有效果:
windowEnterAnimation和windowExitAnimation
@anim/activity_in_left
@anim/activity_out_right
如果想得到没有titlebar的activity主题,需要这样写:
@style/AnimationActivity
true
@anim/activity_in_left
@anim/activity_out_right
另外,代码方式,我试过,也未见效果,最后才发现是调试的手机动画效果没开。。。。
其实在自己写动画的过程中利用Android自带的动画库可以实习部分位移效果,只是不满足activity连续画面的要求,下面贴出我写的位移AnimUtil类的部分代码:
/** *@return */
private static TranslateAnimation moveFromTo(float fromX, float toX, float fromY, float toY) {
TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fromX,
Animation.RELATIVE_TO_SELF, toX, Animation.RELATIVE_TO_SELF,
fromY, Animation.RELATIVE_TO_SELF, toY);
mHiddenAction.setDuration(CData.AnimaDura);
return mHiddenAction;
}
/** *@return */
private static TranslateAnimation moveFromTo(float fromX, float toX, float fromY, float toY, long duration) {
TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, fromX,
Animation.RELATIVE_TO_SELF, toX, Animation.RELATIVE_TO_SELF,
fromY, Animation.RELATIVE_TO_SELF, toY);
mHiddenAction.setDuration(duration);
return mHiddenAction;
}
public static void hideFromLeftToRight(View view) {
view.setAnimation(AnimatorU.moveFromTo(0, 1, 0, 0));
view.setVisibility(View.INVISIBLE);
}
public static void showFromRightToLeft(View view) {
view.setVisibility(View.VISIBLE);
view.setAnimation(AnimatorU.moveFromTo(1, 0, 0, 0));
}
public static void hideFromRightToLeft(View view) {
view.setAnimation(AnimatorU.moveFromTo(0, -1, 0, 0));
view.setVisibility(View.INVISIBLE);
}
public static void showFromLeftToRight(View view) {
view.setVisibility(View.VISIBLE);
view.setAnimation(AnimatorU.moveFromTo(-1, 0, 0, 0));
}
如果不是切换activity,而是让布局中的某个控件有位移效果的话,这个AnimatorU足够使用了。
关键是参数中的(fromX,toX,fromY,toY)结合type参数,相信稍微开动一下脑筋就能明白其中原理