进入Activity、动画卡顿问题的解决方案

有时我们使用activity转场动画的时候,没有再demo流畅,原因可能是因为动画进行时,有同时加载数据等进行主线程操作,所以会导致动画不够流畅.
解决办法就是在动画结束后再进行数据加载等相关操作.
activity里有一个方法

/** 
 * Activities cannot draw during the period that their windows are animating in. In order 
 * to know when it is safe to begin drawing they can override this method which will be 
 * called when the entering animation has completed. 
 */  
public void onEnterAnimationComplete() {  
}  

这个就是在动画结束后的回调.这样效果就会好一些.

这里写图片描述
但是要注意的是该接口API21的,所以之前的旧版本就不能这么用了.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Activity进入和退出时的动画效果可以通过在res目录下的anim文件夹中定义xml文件来实现。其中,进入动画文件一般以slide_in_开头,退出动画文件一般以slide_out_开头。下面分别介绍一下两种动画的实现方式。 进入动画: 1. slide_in_left.xml:从左侧滑入 ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="300" /> </set> ``` 2. slide_in_right.xml:从右侧滑入 ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="300" /> </set> ``` 3. slide_in_top.xml:从顶部滑入 ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="-100%p" android:toYDelta="0" android:duration="300" /> </set> ``` 4. slide_in_bottom.xml:从底部滑入 ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="300" /> </set> ``` 退出动画: 1. slide_out_left.xml:从左侧滑出 ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="300" /> </set> ``` 2. slide_out_right.xml:从右侧滑出 ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="300" /> </set> ``` 3. slide_out_top.xml:从顶部滑出 ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="0" android:toYDelta="-100%p" android:duration="300" /> </set> ``` 4. slide_out_bottom.xml:从底部滑出 ``` <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="300" /> </set> ``` 使用时,在Activity中的onCreate()方法中调用setContentView()方法之前,调用overridePendingTransition()方法,传入进入动画和退出动画的资源id即可。例如,在进入Activity时使用从右侧滑入的动画,退出时使用从左侧滑出的动画,代码如下: ``` startActivity(intent); overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值