原理是继承animation 然后改变他的margintop 和marginbottom 形成2个效果
ExpandTopAnimation
public class ExpandTopAnimation extends Animation { private View mAnimatedView; private LayoutParams mViewLayoutParams; private int mMarginStart, mMarginEnd; private boolean mIsVisibleAfter = false; private boolean mWasEndedAlready = false; public ExpandTopAnimation(View view, int duration) { setDuration(duration); mAnimatedView = view; mViewLayoutParams = (LayoutParams) view.getLayoutParams(); // if the bottom margin is 0, // then after the animation will end it‘ll be negative, and invisible. mIsVisibleAfter = (mViewLayoutParams.topMargin == 0); mMarginStart = mViewLayoutParams.topMargin; 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 < 1.0f) { // Calculating the new bottom margin, and setting it mViewLayoutParams.topMargin = 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.topMargin = mMarginEnd; mAnimatedView.requestLayout(); if (mIsVisibleAfter) { mAnimatedView.setVisibility(View.GONE); } mWasEndedAlready = true; } } }
ExpandBottomAnimation
public class ExpandBottomAnimation extends Animation { private View mAnimatedView; private LayoutParams mViewLayoutParams; private int mMarginStart, mMarginEnd; private boolean mIsVisibleAfter = false; private boolean mWasEndedAlready = false; public ExpandBottomAnimation(View view, int duration) { setDuration(duration); mAnimatedView = view; mViewLayoutParams = (LayoutParams) view.getLayoutParams(); // if the bottom margin is 0, // then after the animation will end it‘ll be negative, and invisible. 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 < 1.0f) { // Calculating the new bottom margin, and setting it mViewLayoutParams.bottomMargin = 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.bottomMargin = mMarginEnd; mAnimatedView.requestLayout(); if (mIsVisibleAfter) { mAnimatedView.setVisibility(View.GONE); } mWasEndedAlready = true; } } }
MainActivity
public class MainActivity extends Activity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.lv_item); for (int i = 0; i < 30; i++) { adapter.add("wiki"+i); } lv.setAdapter(adapter); } public void top(View view){ // trans(lv); ExpandTopAnimation ea = new ExpandTopAnimation(lv, 200); view.startAnimation(ea); } public void bottom(View view){ ExpandBottomAnimation ea = new ExpandBottomAnimation(lv, 200); view.startAnimation(ea); } }
top效果
bottom效果
时间: 2024-10-21 09:21:41