简体   繁体   English

如何在MonoDroid中制作动画监听器?

[英]How to make an animation listener in MonoDroid?

I want to take this android code and convert it to monoDroid 我想把这个Android代码转换为monoDroid

Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_in2);
fade2.setAnimationListener(new AnimationListener() {
    public void onAnimationEnd(Animation animation) {
        startActivity(new Intent(QuizSplashActivity.this,
            QuizMenuActivity.class));
        QuizSplashActivity.this.finish();
    }
});

I have this so far 到目前为止我有这个

Animation fade2 = AnimationUtils.LoadAnimation(this, Resource.Animation.Fade_in2);
 fade2.SetAnimationListener(????);

I don't see new AnimationListener(). 我没有看到新的AnimationListener()。 It seems ot want some interface or something. 它似乎不需要一些界面或东西。

Your source Java code is making use of anonymous inner classes: 您的源代码Java代码正在使用匿名内部类:

fade2.setAnimationListener(new AnimationListener() {...});

C# doesn't support these (C# 3 anonymous types are in no way similar to Java anonymous inner classes), so you need to provide an explicit type and use that instead: C#不支持这些(C#3匿名类型与Java匿名内部类没有任何关系),因此您需要提供一个显式类型并使用它:

class MyAnimationListener : Java.Lang.Object,
        Android.Views.Animations.Animation.IAnimationListener
{
    Activity self;

    public MyAnimation (Activity self)
    {
        this.self = self;
    }

    public void OnAnimationEnd (Animation animation)
    {
        self.StartActivity (new Intent (self, typeof (QuizMenuActivity)));
        self.Finish ();
    }

    public void OnAnimationRepeat (Animation animation)
    {
    }

    public void OnAnimationStart (Animation animation)
    {
    }
}

// ...
fade2.SetAnimationListener (new MyAnimationListener (this));

As seen above, to implement the interface we also inherit from Java.Lang.Object (this implements Android.Runtime.IJavaObject for us), and instead of implicitly referencing QuizSplashActivity.this as is done in Java, we instead need to explicitly capture it as a self field. 如上所示,为了实现接口,我们还继承了Java.Lang.Object (这为我们实现了Android.Runtime.IJavaObject ),而不是像在Java中那样隐式引用QuizSplashActivity.this ,而是需要显式捕获它作为一个self领域。

This could be simplified by providing a helper base type (I imagine that in the Java code the AnimationListener is a helper type, as not all of the Animation.AnimationListener methods were provided, so doing the ~same thing in C# would work as well). 这可以通过提供一个帮助程序基类型来简化(我想在Java代码中, AnimationListener是一个帮助程序类型,因为并非所有的Animation.AnimationListener方法都提供了,所以在C#中执行相同的操作也会有效) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM