您将需要创建一个自定义动画类,如下所示:
public class ExpandAnimation extends Animation {
private View mAnimatedView;
private LayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mIsVisibleAfter = false;
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 = (LayoutParams) view.getLayoutParams();
mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0);
mMarginStart = mViewLayoutParams.bottomMargin;
mMarginEnd = (mMarginStart == 0 ? (0 - view.getHeight()) : 0);
view.setVisibility(View.VISIBLE);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 0.5f) {
mViewLayoutParams.bottomMargin = mMarginStart + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
mAnimatedView.requestLayout();
} else if (!mWasEndedAlready) {
mViewLayoutParams.bottomMargin = mMarginEnd;
mAnimatedView.requestLayout();
if (mIsVisibleAfter) {
mAnimatedView.setVisibility(View.GONE);
}
mWasEndedAlready = true;
}
}
}
然后将此动画应用于所需的 x,y 坐标。
假设您在某个 x,y 坐标处有一个按钮,单击它时我们会为视图设置动画,然后将其向后滚动。你将不得不做这样的事情:
private View previous = null;
private void doTransformation() {
if (previous != null) {
((LinearLayout.LayoutParams) previous.getLayoutParams()).bottomMargin = -200;
ExpandAnimation anim = new ExpandAnimation(previous, 300);
previous.startAnimation(anim);
previous = null;
} else {
View yourlayout= findViewById(R.id.your_layout);
ExpandAnimation anim = new ExpandAnimation(yourLayout, 300);
detailLayout.startAnimation(anim);
previous = yourLayout;
}
}