我在Android上玩过一些动画片,和往常一样,它一团糟。首先,代码:
public class ResizeWidthAnimation extends Animation {
private float mStartWeight;
private float mWeight;
private float mStartWeight2;
private float mWeight2;
private int mCurrentWidth;
private int mCurrentWidth2;
private View mView;
private int mFinalWidth2;
private View mSecondFrame;
public ResizeWidthAnimation(View view, View secondFrame, float weight, int finalWidth2) {
mView = view;
mSecondFrame = secondFrame;
mFinalWidth2 = finalWidth2;
mWeight = weight;
mStartWeight = ((LinearLayout.LayoutParams)view.getLayoutParams()).weight;
mWeight2 = weight;
mStartWeight2 = ((LinearLayout.LayoutParams)secondFrame.getLayoutParams()).weight;
mCurrentWidth = view.getMeasuredWidth();
mCurrentWidth2 = secondFrame.getMeasuredWidth();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mView.getLayoutParams();
lp.weight = (float) (mStartWeight + ((mWeight - mStartWeight) * interpolatedTime));
mView.requestLayout();
mCurrentWidth = mView.getMeasuredWidth();
if (mCurrentWidth2 >= mFinalWidth2) {
return;
}
LinearLayout.LayoutParams lp2 = (LinearLayout.LayoutParams) mSecondFrame.getLayoutParams();
lp2.weight = (float) (mStartWeight2 + ((mWeight2 - mStartWeight2) * interpolatedTime));
mSecondFrame.requestLayout();
mCurrentWidth2 = mSecondFrame.getMeasuredWidth();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
public boolean willChangeBounds() {
return true;
}
}
这是基于TWO布局权重的动画(并排放置。一个最初加权0f,另一个是8f,第三个是2f。这应该做的是:
同时做两件事:
a)动画第二个布局(放置在右边)从0f到"围绕"图5f
b)向右滑动第三个布局,当到达a)的末尾时,检查是否有足够的位置绘制第三个布局的32dp宽部分,向右滑动。如果是这样,请停止动画并有效地为第三个布局留下一些位置。
我确实理解requestLayout()调用是昂贵的,并且上面的解决方案并不完美,所以我想知道是否有人能提供更好的解决方案。最糟糕的是,我需要与api v8兼容。