次のようなカスタム アニメーション クラスを作成する必要があります。
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;
}
}