Android 动画方法applyTransformation在单击任何布局之前不会触发
这太奇怪了,我有一个动画代码:Android 动画方法applyTransformation在单击任何布局之前不会触发,android,android-layout,android-animation,Android,Android Layout,Android Animation,这太奇怪了,我有一个动画代码: public class ExpandAnimation extends Animation { private View mAnimatedView; private MarginLayoutParams mViewLayoutParams; private int mMarginStart, mMarginEnd; private boolean mWasEndedAlready = false; /** * Initialize the animation
public class ExpandAnimation extends Animation {
private View mAnimatedView;
private MarginLayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mWasEndedAlready = false;
/**
* Initialize the animation
* @param view The layout we want to animate
* @param duration The duration of the animation, in ms
*/
public ExpandAnimation(View view, int duration) {
setDuration(duration);
mAnimatedView = view;
mViewLayoutParams = (MarginLayoutParams) view.getLayoutParams();
mMarginStart = mViewLayoutParams.rightMargin;
mMarginEnd = (mMarginStart == 0 ? (0- view.getWidth()) : 0);
view.setVisibility(View.VISIBLE);
mAnimatedView.requestLayout();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
// Calculating the new bottom margin, and setting it
mViewLayoutParams.rightMargin = mMarginStart
+ (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
// Invalidating the layout, making us seeing the changes we made
mAnimatedView.requestLayout();
// Making sure we didn't run the ending before (it happens!)
} else if (!mWasEndedAlready) {
mViewLayoutParams.rightMargin = mMarginEnd;
mAnimatedView.requestLayout();
mWasEndedAlready = true;
}
}
}
此动画切换隐藏/显示布局
默认情况下,它是隐藏的。当我单击时,动画工作并显示出来。当我再次单击时,它会正确收缩。但第三次,它什么也不做。我已经调试过了,发现构造函数名为,而不是有人知道什么时候触发applyTransformation吗?我不明白为什么,但当我单击任何布局或对其执行任何操作时,动画最终开始。因此,我通过编程添加了一个变通方法。我的布局中有一个滚动视图,因此我移动滚动位置:
hscv.scrollTo(hscv.getScrollX()+1, hscv.getScrollY()+1);
这就在containerMenu.startAnimation(动画)之后代码>
这就行了,我不明白为什么
此外,我发现有些动画在android>4上运行得完美无缺,但在2.3上,它也有同样的问题,可以扩展,也可以收缩,但不能第二次扩展
parent.invalidate();
我成功了。我使用了相同的代码,遇到了完全相同的问题。applyTranformation永远不会被称为第三次或以后的时间。这就像动画需要以某种方式在视图上重置一样。我尝试调用ContainerManu.clearAnimation(),但没有效果。我已经和它斗争了好几个小时,但仍然没有结果…@NetWolf我设法找到了一个解决办法。我将把它作为一个答案发布。我的在一个片段中,它在一个ViewPager中。我可以通过获取活动的viewpager实例并滚动1个像素,然后在调用动画后向后滚动来解决这个问题。谢谢你的提示@我刚刚添加了parent.invalidate()代码>。似乎在2.3设备上工作。我刚刚发现了同样的东西,现在我要在一部旧手机上进行测试。谢谢你的更新。正是这个解决方案节省了我的时间!!
parent.invalidate();