2017-09-26 74 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