How to make an animation listener in MonoDroid?

2020-03-30 08:06发布

I want to take this android code and convert it to 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(). It seems ot want some interface or something.

1条回答
Root(大扎)
2楼-- · 2020-03-30 08:12

Your source Java code is making use of anonymous inner classes:

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:

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.

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).

查看更多
登录 后发表回答