平常从一个 Activity A打开另个一个Activity B ,一般 Activity B 都是从屏幕右边出来。而关闭 Activity B 返回Activity A ,则 Activity B 会从左边沿向右边沿消失。有时候,想让新的 Activity 从屏幕的底部弹出,或者屏幕的中间向弹出,或者想让Activity从屏幕的中间收回。本文章讲的是这中需要效果。
实现原理
主要是 Activity 类的 overridePendingTransition(int enterAnim, int exitAnim) 方法的调用,在 res/anim目录下创建所需的动画文件,然后在 overridePendingTransition(int enterAnim, int exitAnim) 方法中调用动画即可。
效果图
show_activity_anim具体代码
在 res/anim 目录下新建你需要的动画文件,例如,我需要新的 Activity 从顶部向上弹出,然后从顶部向底部收回。那么我创建了activity_open_from_bottom_to_top.xml 文件,activity_close_from_top_to_bottom.xml 文件和 activity_keep_status.xml 文件。如下
activity_open_from_bottom_to_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1500"
android:fromYDelta="100%p"
android:toYDelta="0"/>
<alpha
android:fromAlpha="0.2"
android:toAlpha="1.0"
android:duration ="1500"/>
</set>
activity_close_from_top_to_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1500"
android:fromYDelta="25"
android:toYDelta="100%p"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.2"
android:duration ="1500"/>
</set>
activity_keep_status.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1500"
android:fromYDelta="0"
android:toYDelta="10%p"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.8"
android:duration ="1500"/>
</set>
然后在Activity的代码中,用 overridePendingTransition(int enterAnim, int exitAnim) 方法调用这三个文件。
在启动 Activity 的地方:
public static void startMe(Activity activity, String title, ArrayList<String> imageList, int position) {
Intent intent = new Intent(activity, PhotoViewGroupActivity.class);
intent.putExtra(TAG_IMAGE_LIST, imageList);
intent.putExtra(TAG_POSITION, position);
intent.putExtra(TAG_TITLE, title);
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.activity_photo_center_to_edge, R.anim.activity_photo_keep_status);
}
在退出 Activity 的地方:
@Override
public void finish() {
super.finish();
overridePendingTransition(0, R.anim.activity_photo_edge_to_center);
}
关于overridePendingTransition(enterAnim,exitAnim)方法
关于此方法,我们可以点进去看一下源码的注释怎么说。
/**
* Call immediately after one of the flavors of {@link #startActivity(Intent)}
* or {@link #finish} to specify an explicit transition animation to
* perform next.
*
* <p>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) {
try {
ActivityManager.getService().overridePendingTransition(
mToken, getPackageName(), enterAnim, exitAnim);
} catch (RemoteException e) {
}
}
根据此处的说明,第一个参数 enterAnim 是指即将要到来的 Activity 的动画资源文件,第二个参数exitAnim是即将要离开 Activity 的动画资源文件。两个参数都可以传入0作为无动画。
关于
随手笔记,若有错误,请指摘。
我的Github: https://github.com/EKwongChum
网友评论