`
flyouting
  • 浏览: 14277 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

关于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中去。问题也就解决了。
   
2
6
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics