android 设置activity启动退出动画 | 解决设置activity 动画不生效问题

前提:

如果有动画,最好把activity 设置成透明,不然会有黑屏!

    

方法一:代码里面设置

                        getActivity().startActivity(intent,bundle);
                        getActivity().overridePendingTransition( R.anim.push_left_in,0);
    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.push_right_out);


    }

注意:overridePendingTransition一定要在startActivity 或者finish 之后调用,否则没有效果!而且可能会有各种其他问题!

具体原因得看源码。我们可以先看下overridePendingTransition 的注释:

/**
     * Call immediately after one of the flavors of {@link #startActivity(Intent)}
     * or {@link #finish} to specify an explicit transition animation to
     * perform next.
     *
     * 

As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative * to using this with starting activities is to supply the desired animation * information through a {@link ActivityOptions} bundle to * {@link #startActivity(Intent, Bundle) or a related function. This allows * you to specify a custom animation even when starting an activity from * outside the context of the current top activity. * * @param enterAnim A resource ID of the animation resource to use for * the incoming activity. Use 0 for no animation. * @param exitAnim A resource ID of the animation resource to use for * the outgoing activity. Use 0 for no animation. */ public void overridePendingTransition(int enterAnim, int exitAnim) {

方法二:给activity 设置theme

        
    
    
    

left_in xml



    
 

right_out xml



    

总结:

1.使用theme 的方式在有些手机上有问题,可能动画不是你设置的,但是代码的没有问题。
2.有些手机room 自己修改了动画的方向,所以,不一定是你的代码生效了,而是默认就是这种动画。
比如小米手机启动activity ,不设置就是左进右出。
3.代码代用顺序问题。顺序不对,动画不生效,而且还会产生问题。
一定要先startActivity,或者 先finish,再调用overridePendingTransition

为什么一定要先startActivity,或者 先finish?

可以跟一下源码
Activity 的overridePendingTransition

    public void overridePendingTransition(int enterAnim, int exitAnim) {
        try {
            ActivityManager.getService().overridePendingTransition(
                    mToken, getPackageName(), enterAnim, exitAnim);
        } catch (RemoteException e) {
        }
    }

可以看到会调用 ActivityManager.getService().overridePendingTransition,也就是ActivityManagerService的overridePendingTransition

    @Override
    public void overridePendingTransition(IBinder token, String packageName,
            int enterAnim, int exitAnim) {
        synchronized(this) {
            ActivityRecord self = ActivityRecord.isInStackLocked(token);
            if (self == null) {
                return;
            }

            final long origId = Binder.clearCallingIdentity();

            if (self.state == ActivityState.RESUMED
                    || self.state == ActivityState.PAUSING) {
                mWindowManager.overridePendingAppTransition(packageName,
                        enterAnim, exitAnim, null);
            }

            Binder.restoreCallingIdentity(origId);
        }
    }

又会调用mWindowManager (WindowManagerService) 的overridePendingAppTransition

    @Override
    public void overridePendingAppTransition(String packageName,
            int enterAnim, int exitAnim, IRemoteCallback startedCallback) {
        synchronized(mWindowMap) {
            mAppTransition.overridePendingAppTransition(packageName, enterAnim, exitAnim,
                    startedCallback);
        }
    }

最终调用 mAppTransition.overridePendingAppTransition。也就是AppTransition 这个类。

    void overridePendingAppTransition(String packageName, int enterAnim, int exitAnim,
            IRemoteCallback startedCallback) {
        if (isTransitionSet()) {
            clear();
            mNextAppTransitionType = NEXT_TRANSIT_TYPE_CUSTOM;
            mNextAppTransitionPackage = packageName;
            mNextAppTransitionEnter = enterAnim;
            mNextAppTransitionExit = exitAnim;
            postAnimationCallback();
            mNextAppTransitionCallback = startedCallback;
        } else {
            postAnimationCallback();
        }
    }
    boolean isTransitionSet() {
        return mNextAppTransition != TRANSIT_UNSET;
    }

可以看到这里面有一个isTransitionSet() 方法,默认是TRANSIT_UNSET,只有startActivity 或者finish 之后,这个mNextAppTransition 才不是TRANSIT_UNSET。所以需要先startActivity 或者finish,然后overridePendingTransition才会有作用。每次动画结束,这个值又会被值为TRANSIT_UNSET。

你可能感兴趣的:(Android)