关于Activity的跳转动画

在1.6之后,也就是从API 5开始,加入了一个overridePendingTransition函数,是用来处理Activity跳转时实现动画效果的。在网上,很多人发帖或者转发,只是很简单的提供了一种写法:
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) {

overridePendingTransition(R.anim.push_up_in,R.anim.push_up_out);

}

但是这种写法在项目中使用的话,会出现问题,在1.6的测试机器上会出现一个VerifyError的错误,因为overridePendingTransition 会在加载类加载时调用,在1.6的机器上会进行一个预编译,直接这样写就会出现错误。
在国外某论坛搜索了一下,发现了这样一段话:
The VM will attempt to find overridePendingTransition() when the class is loaded, not when that if() statement is executed.
Instead, you should be able to do this:

if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONUT) {
SomeClassDedicatedToThisOperation.overridePendingTransition(this, ...);
}

where the implementation of overridePendingTransition() in SomeClassDedicatedToThisOperation just calls overridePendingTransition() on the supplied Activity.

So long as SomeClassDedicatedToThisOperation is not used anywhere else, its class will not be loaded until you are inside your if() test, and you will not get the VerifyError.

这里已经很明确的给出了解决办法。
处理方式:
public class AnimationModel {
private Activity context;
public AnimationModel(Activity context){
this.context = context;
}
/**
* call overridePendingTransition() on the supplied Activity.
* @param a
* @param b
*/
public void overridePendingTransition(int a, int b){
context.overridePendingTransition(a, b);
}

}


调用方式:
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.DONU) {
(new AnimationModel(Profile.this))
.overridePendingTransition(R.anim.push_up_in,
R.anim.push_up_out);
}

这样,你引用跳转动画函数的模型类是生成了,但是里边的函数是不会被调用的。这样就不会被加载到jvm中去。问题也就解决了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值