我正在尝试从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();
}
}
}
答案 0 :(得分:1)
为什么所有代码,只需在Resources / 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);