在1.6之后,也就是从API 5开始,加入了一个overridePendingTransition函数,是用来处理Activity跳转时实现动画效果的。在网上,很多人发帖或者转发,只是很简单的提供了一种写法:
但是这种写法在项目中使用的话,会出现问题,在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.
这里已经很明确的给出了解决办法。
处理方式:
调用方式:
这样,你引用跳转动画函数的模型类是生成了,但是里边的函数是不会被调用的。这样就不会被加载到jvm中去。问题也就解决了。
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中去。问题也就解决了。
解决Activity动画兼容性问题
本文介绍了如何在不同Android版本间正确使用overridePendingTransition方法来实现Activity间的过渡动画,并提供了一个兼容1.6版本及以上的解决方案。
866

被折叠的 条评论
为什么被折叠?



