我有一种情况,希望为Android.Support.V4.App.Fragment
的某些元素设置动画。我的片段在MainActivity内部创建,如下所示,并注入到FrameLayout
:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
var activeFragment = new TapAnswerFragment();
var fragmentTx = SupportFragmentManager.BeginTransaction();
fragmentTx.Add(Resource.Id.answerFragmentFrameLayout, activeFragment);
fragmentTx.Commit();
}
在TapAnswerFragment
内部,我有一个点击处理程序:
AlphaAnimation translationFadeIn = new AlphaAnimation(0.5f, 1.0f);
private void TapAnswerRelativeLayout_Click(object sender, System.EventArgs e)
{
tapAnswerTextView.StartAnimation(translationFadeIn);
}
触发事件后,动画将不会发生。是否可以在片段内运行动画?
答案是肯定的。在上面的代码中,您只是忘记设置动画的持续时间。
AlphaAnimation translationFadeIn = new AlphaAnimation(0.5f, 1.0f);
private void TapAnswerRelativeLayout_Click(object sender, System.EventArgs e)
{
translationFadeIn.Duration = 2000;// Animation duration,Milliseconds in units
tapAnswerTextView.StartAnimation(translationFadeIn);
}