Joy Rex
Joy Rex

Reputation: 618

Animation not working in Xamarin

I have used the below code snippet in my native platform (android) to animate the value, and it works fine in android.

 public void setValue(double value) {
        if(this.renderer != null)
        {
            ObjectAnimator anim= ObjectAnimator.ofFloat(this.renderer,"Value",(float)this.value,(float)value);
            anim.setDuration(1500);
            anim.start();
        }

        this.value = value;
    }

While try to set the newValue for the Value property (of android) from Xamarin, it sets the newvalue but animation not working in xamarin.

Am i miss something?

Android callable wrappers (ACW) are needed any time the Android runtime needs to invoke managed code, and are required because there is no way to register classes with Dalvik at runtime.reference link

So i have tried to declare the ObjectAnimator anim earlier and animate the value as below,

public void setValue(double value) {
if(this.renderer != null)
{
    anim= ObjectAnimator.ofFloat(this.renderer,"Value",(float)this.value,(float)value);
    anim.setDuration(1500);
    anim.start();
}

this.value = value;

}

But the mentioned issue isn't resolved.

Upvotes: 1

Views: 1708

Answers (1)

Kiliman
Kiliman

Reputation: 20322

The ObjectAnimator is trying to find the method setValue(float) on your Renderer class.

You need to add the [Export] attribute to make it visible to the Java side.

public class Renderer: Java.Lang.Object
{
    private float _value;
    [Export]
    public float getValue()
    {
        return _value;
    }

    [Export]
    public void setValue(float value)
    {
        _value = value;
        Log.Debug("TestRenderer", "Value: {0}", value);
    }
}

Now you'll be able to use the ObjectAnimator like this:

var renderer = new Renderer();
var anim = ObjectAnimator.OfFloat(renderer, "value", 0f, 1f);
anim.SetDuration(2000);
anim.Start();

See ObjectAnimator Proxy to Animate TopMargin can't find setting/getter for more info.

Upvotes: 3

Related Questions