2017-09-26 75 views
0

我想从XML动画化我的ImageView。它基本上是从中心positino移动到屏幕的顶部。做到这一点,我使用了一个定时器,每x毫秒发射一个函数。这个函数然后将物体进一步向上移动。它确实有效,但它是滞后的。我正在调试三星S7,所以电源应该不成问题。我猜计时器是非常不准确的。你能告诉我做这件事,而不是这个更好的方法:Xamarin,Android:动画有点迟钝(使用定时器)

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(); 
     } 

    } 
} 

回答

1

为什么所有的代码,只需在资源定义例如move_up.xml /绘制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位置的中心开始并到达顶部。在你持续多少毫秒你想要移动。 而不只是到您的图像查看添加此

Animation anim2 = AnimationUtils.LoadAnimation(this.BaseContext, Resource.Drawable.move_up); 
    ImageView myImage = FindViewById<ImageView>(Resource.Id.imageView1); 
    myImage.StartAnimation(anim2); 
+0

谢谢!我会在下周给它一个镜头!我会报告回来! :) – MrMee

+0

希望这可以帮助,如果你有更多的问题,请写:) – Merian

+1

这工作出色。我的提示:使用这个插值器:android:interpolator =“@ android:anim/accelerate_decelerate_interpolator”它会使动画看起来更流畅! – MrMee