android 电视关闭动画,Activity 展开和关闭动画

平常从一个 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) 方法中调用动画即可。

效果图

b5b9b04eaa80?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

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

android:duration="1500"

android:fromYDelta="100%p"

android:toYDelta="0"/>

android:fromAlpha="0.2"

android:toAlpha="1.0"

android:duration ="1500"/>

activity_close_from_top_to_bottom.xml

android:duration="1500"

android:fromYDelta="25"

android:toYDelta="100%p"/>

android:fromAlpha="1.0"

android:toAlpha="0.2"

android:duration ="1500"/>

activity_keep_status.xml

android:duration="1500"

android:fromYDelta="0"

android:toYDelta="10%p"/>

android:fromAlpha="1.0"

android:toAlpha="0.8"

android:duration ="1500"/>

然后在Activity的代码中,用 overridePendingTransition(int enterAnim, int exitAnim) 方法调用这三个文件。

在启动 Activity 的地方:

public static void startMe(Activity activity, String title, ArrayList 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.

*

*

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作为无动画。

关于

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值