activity的打开关闭动画

Activity的打开关闭或者说相互跳转之间可以设置动画的。默认的打开关闭直接消失或出现,比较不优美,但是有的手机Rom对这个默认做了修改,比如红米HM1,默认的就是新页面自右向左滑动出现,自左向右滑动消失。

设置动画有两种方法:

1。利用Activity的方法在代码中设置:

public void overridePendingTransition (int enterAnim, int exitAnim)
Call immediately after one of the flavors of startActivity(Intent) or finish() to specify an explicit transition animation to perform next.
enterAnimA resource ID of the animation resource to use for the incoming activity. Use 0 for no animation.
exitAnimA resource ID of the animation resource to use for the outgoing activity. Use 0 for no animation.
enterAnimA在此Actvity是将要出现的Activity时的进入动画。
exitAnimA在此Actvity是当前将要退出的Activity时的退出动画。

这个方法一定要在activity的start和finish之后立即调用。

 @Override
    public void finish() {
        super.finish();
        if (isCloseAnim) {
            this.overridePendingTransition(0, R.anim.activity_out_to_up);
        }

    }
finish
Intent intent = new Intent(context, QuizActivity.class);
        intent.putExtra(EXTRA_SCHEME_ID, schemeId);
        context.startActivity(intent);
        //设置切换动画,自下而上进入,自上而下退出
        ((Activity) context).overridePendingTransition(R.anim.activity_in_from_down, 0);
start

 

2。设置Activity的主题风格,在xml中实现:

在AndroidManifest里面,对于application和activity标签可以定义theme属性。如果对Application定义了某一个属性,那么会对所有的activity产生影响,当然可以在activity中覆盖它,为这个Activity添加自己的特定属性。

<application android:theme="@style/ThemeActivity"> 

然后在values/themes.xml中定义ThemeActivity这个主题

<style name="ThemeActivity" parent="Theme.AppCompat.Light.NoActionBar"> 
<item name="android:windowAnimationStyle">@style/AnimationActivity</item> 
<item name="android:windowNoTitle">true</item>
</style>
在values/styles.xml中定义AnimationActivity的style
<style name="AnimationActivity" parent="@android:style/Animation.Activity" > 
<item name="android:activityOpenEnterAnimation">@anim/push_left_in</item> 
<item name="android:activityOpenExitAnimation">@anim/push_left_out</item> 
<item name="android:activityCloseEnterAnimation">@anim/push_right_in</item> 
<item name="android:activityCloseExitAnimation">@anim/push_right_out</item> 
</style> 

至于anim中的动画在res/anim下用xml的set属性定义好就可以了。各个动画item的意义如下:

android:activityCloseEnterAnimation    When closing the current activity, this is the animation that is run on the next activity (which is entering the screen).

android:activityCloseExitAnimation    When closing the current activity, this is the animation that is run on the current activity (which is exiting the screen).

android:activityOpenEnterAnimation    When opening a new activity, this is the animation that is run on the next activity (which is entering the screen).

android:activityOpenExitAnimation    When opening a new activity, this is the animation that is run on the previous activity (which is exiting the screen).

Dialog和PopupWindow也可以设置动画。

比如在定义PopupWindow时指定动画:

setAnimationStyle(R.style.mypopwindow_anim_bottom_style);
posted @ 2015-11-30 15:57  Matrix_Ran  阅读(5285)  评论(0编辑  收藏  举报