0
使用ValueAnimator
和定制的ValueAnimator.Update
方法为背景设置动画。在活动onCreate
方法中,用FragmentTransaction
第一个片段调用。并从第一个片段中,另一个FragmetTranstaction
根据用户按钮点击进行调用。在第一个事务中,一切都很完美,但在第二个事务中,ValueAnimator.Update
中使用的活动变量为空。哪里有问题?片段另一个片段的事务调用使活动变量为空
活动onCreate
:上调背景和矩阵变量
private const int TO_LEFT = 0;
private const int TO_RIGHT = 1;
private const long DURATION = 15000;
private int bgAnimDir = TO_LEFT;
private RectF mDisplayRect = new RectF();
private Matrix matrix = new Matrix();
private ValueAnimator bgAnimator;
[BindView(Resource.Id.background)]
ImageView background;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Cheeseknife.Bind(this);
background.Post(() =>
{
animate();
});
fragmentTransaction = FragmentManager.BeginTransaction();
fragmentTransaction.Add(Resource.Id.home_container, new Home(), "Home");
fragmentTransaction.Commit();
}
空指针异常
animate()
功能:在bgAnimator.Update
代表提出
private void animate()
{
updateDisplayRect();
if (bgAnimDir == TO_LEFT)
animate(mDisplayRect.Left, mDisplayRect.Left - (mDisplayRect.Right - background.Width));
else
animate(mDisplayRect.Left, 0.0f);
}
private void animate(float from, float to)
{
bgAnimator = ValueAnimator.OfFloat(from, to);
bgAnimator.Update +=
(object sender, ValueAnimator.AnimatorUpdateEventArgs e) =>
{
float value = (float)e.Animation.AnimatedValue;
matrix.Reset();
matrix.PostTranslate(value, 0);
background.ImageMatrix = matrix;
};
bgAnimator.SetDuration(DURATION);
bgAnimator.AnimationEnd +=
(object sender, EventArgs e) =>
{
bgAnimDir = bgAnimDir == TO_LEFT ? TO_RIGHT : TO_LEFT;
animate();
};
bgAnimator.Start();
}
例外
你想在非活动片段中显示动画,这就是为什么你有这个例外 –