我希望在我的Xamarin C# Android应用程序中使用动画效果,如淡入、缩放、移动等。
我希望在我的Xamarin C# Android应用程序中使用动画效果,如淡入、缩放、移动等。
首先在 "资源" 文件夹下添加一个名为 "anim" 的文件夹,然后您可以将动画资源添加到其中, 例如: 对于淡入动画,在 anim 文件夹下创建一个资源,并将其命名为 "fade_in.xml",然后将以下代码粘贴到其中:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
然后在你的mainlayout.xml文件中添加一个TextView和一个Button。
<TextView
android:text="Text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtMessage"
android:layout_marginBottom="35.3dp" />
并为按钮:
<Button
android:text="fade in"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/fadein" />
在您的Activity的"onCreate"方法中添加以下代码:
Button fadein = FindViewById<Button>(Resource.Id.fadein);
fadein.Click += btn_Click;
接着将此方法添加到您的活动中:
void blink_Click(object sender, EventArgs e)
{
txtMessage = FindViewById<TextView>(Resource.Id.txtMessage);
Button b = sender as Button;
Animation anim = AnimationUtils.LoadAnimation(ApplicationContext,
Resource.Animation.fade_in);
txtMessage.StartAnimation(anim);
}
txtMessage.Alpha = 0.0f;
txtMessage.Animate().Alpha(1.0f).SetDuration(1000).Start();
您还可以对其他属性进行动画处理,例如 ScaleX
、RotationX
、TranslationX
等等...