如何客制化某两个activity切换时的窗口动画?

[DESCRIPTION]

两个acitivity在做切换时会有一个过渡的窗口动画效果,默认情况下这个窗口动画的动画效果是由framework统一控制的。如果想客制化某两个窗口切换时的动画效果,应该怎么做?

[SOLUTION]

Activity.java中提供了一个可供ap端override窗口切换动画效果的方法:

public void overridePendingTransition(int enterAnim, int exitAnim)

 

以Launcher中使用这个API的代码为例:

// Launcher.java

public void onClickVoiceButton(View v) {

        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

 

        try {

            final SearchManager searchManager =

                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

            ComponentName activityName = searchManager.getGlobalSearchActivity();

            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            if (activityName != null) {

                intent.setPackage(activityName.getPackageName());

            }

            startActivity(null, intent, "onClickVoiceButton");

 

   // R.anim.fade_in_fast和R.anim.fade_out_fast分别表示要启动的activity 窗口的enterAnim资源ID和exitAnim资源ID

            overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);

        } catch (ActivityNotFoundException e) {

            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivitySafely(null, intent, "onClickVoiceButton");

        }

    }

上一篇:018 Android Activity界面移入与移出的动画效果


下一篇:让蔡徐坤来教你实现游戏中的帧动画(中)