我想动画查看它被添加到父级后(正如DrawerLayout)。问题是视图大小不一,动画目标位置取决于该大小。简化的示例代码:动画新创建的未知大小的视图
AnimatingView extends View {
public int offsetX;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int screenWidth = MeasureSpec.getSize(widthMeasureSpec);
final int screenHeight = MeasureSpec.getSize(heightMeasureSpec);
offsetX = calculateOffset(screenWidth);
...
}
}
代码与此类似触发动画:
@Override
public void onClick(View v) {
AnimatingView animatingView = new AnimatingView(getContext());
parentLayout.addView(animatingView);
animatingView.animate().x(animatingView.offsetX).setDuration(500).start();
}
在这种情况下onMeasure()
animate()
后会发生,所以动画失败。做什么取决于视图测量的正确方法是什么?
简单的&愚蠢的方式会像animateOnceAfterMeasuring()
基于isInitialized
标志,但我不认为它是这样做的正确方法。
你为什么不u使用'Android版:在你的父布局XML –
放'animatingView.animate(animateLayoutChanges'属性).x(animatingView.offsetX).setDuration(500).start();'parent line in parentLayout.post(new Runnable(){});' –
@sohanshetty在这种情况下,父ViewGroup会将孩子布置在默认位置,而不是在小孩实际上应该停止它的动作的位置nt – Levinthann