Xamarin,Android:动画有点滞后(使用计时器)

Xamarin,Android:动画有点滞后(使用计时器),android,timer,Android,Timer,我正在尝试从XML设置ImageView的动画。它基本上是从正中移动到屏幕顶部。为了实现这一点,我使用了一个计时器,每x毫秒触发一个函数。然后,此函数将对象进一步向上移动一点。它确实有效,但它是滞后的。我正在三星S7上调试,所以电源应该不是问题。我猜计时器是非常不准确的。你能告诉我一个更好的方法来代替这个吗: public class finalPhoto : Activity { private System.Timers.Timer timer; p

我正在尝试从XML设置ImageView的动画。它基本上是从正中移动到屏幕顶部。为了实现这一点,我使用了一个计时器,每x毫秒触发一个函数。然后,此函数将对象进一步向上移动一点。它确实有效,但它是滞后的。我正在三星S7上调试,所以电源应该不是问题。我猜计时器是非常不准确的。你能告诉我一个更好的方法来代替这个吗:

 public class finalPhoto : Activity
    {
        private System.Timers.Timer timer;
        private ImageView wowThatLooksFantastic;
        private float i = 0f;
        private int test = 0; 
        private int NegativeSpeed = 350; 
        private int frameRate = 17; // 17 = etwa 60fps

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.finalPhoto);

            wowThatLooksFantastic = FindViewById<ImageView>(Resource.Id.text_wowthatlooksfantastic);

            wowThatLooksFantastic.Click += delegate { StartAnimation(); };

            test = Resources.DisplayMetrics.HeightPixels; 


        }
    private void StartAnimation()
    {
        i = wowThatLooksFantastic.GetY();
        CountDown(); 
    }

    public void CountDown()
    {

        timer = new System.Timers.Timer();
        timer.Interval = frameRate;
        timer.Elapsed += OnTimedEvent;
        timer.Start(); 

    }

    protected override void OnResume()
    {
        base.OnResume();
    }


    public void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
    {
        i -= (test/NegativeSpeed); 
        wowThatLooksFantastic.SetY(i);

        if (wowThatLooksFantastic.GetY() <= 50) // Endposition
        {
            timer.Stop(); 
        }

    }
}
公开课最终照片:活动
{
专用系统计时器;
私人图像视图,外观舒适;
专用浮点数i=0f;
私有int检验=0;
私有int NegativeSpeed=350;
专用整数帧速率=17;//17=etwa 60fps
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.finalPhoto);
wowthatslooksfantatic=findviewbyd(Resource.Id.text\u wowthatslooksfantatic);
wowthatslooksfantatic.Click+=委托{StartAnimation();};
测试=Resources.DisplayMetrics.HeightPixels;
}
私有void StartAnimation()
{
i=wowthatsfantastic.GetY();
倒计时();
}
公共空间倒计时()
{
timer=新系统.Timers.timer();
时间间隔=帧率;
timer.appeased+=OnTimedEvent;
timer.Start();
}
受保护的覆盖void OnResume()
{
base.OnResume();
}
TimeDevent上的公共void(对象发送方,System.Timers.ElapsedEventArgs e)
{
i-=(测试/阴性速度);
塞蒂(i);

如果(wowthatsfantastic.GetY()为什么要编写这些代码,只需在参考资料/drawable XML文件中定义以下内容,例如move_up.XML

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

    <translate
        android:fromYDelta="50%p"
        android:toYDelta="0%p"
        android:duration="1000" />
</set>

这意味着它将从屏幕的Y位置的中心开始,并到达顶部。在持续时间中,您将输入希望移动的毫秒数。 而不仅仅是添加到您的imageView

   Animation anim2 = AnimationUtils.LoadAnimation(this.BaseContext, Resource.Drawable.move_up);
   ImageView myImage = FindViewById<ImageView>(Resource.Id.imageView1);
   myImage.StartAnimation(anim2);
Animation anim2=AnimationUtils.LoadAnimation(this.BaseContext,Resource.Drawable.move_向上);
ImageView myImage=findviewbyd(Resource.Id.imageView1);
myImage.StartAnimation(anim2);

谢谢!我下周会试一试!我会回来报告的!:)希望这有帮助,如果你有更多问题,请写:)这很好用。我的建议:使用这个插值器:android:interpolator=“@android:anim/accelerate\u Decreate\u interpolator”它会让动画看起来更流畅!谢谢你尝试使用这个