我如何animationView.setVisibility(GONE)

我想为View获取可见性设置为GONE时制作AnimationView不应该消失, View应该“崩溃”。 我试图用一个ScaleAnimation这个,但是然后View崩溃,但布局将只调整它的Animation停止(或开始)之后(或之前)的空间。

我该如何制作Animation以便在Animation时,较低的View将保留在内容的正下方,而不是空白区域?

似乎没有一种简单的方法可以通过API来实现,因为animation只是改变了视图的渲染matrix,而不是实际的大小。 但是我们可以设置一个负边界,让L​​inearLayout认为视图变小。

所以我build议根据ScaleAnimation创build您自己的Animation类,并重写“applyTransformation”方法来设置新的边距并更新布局。 喜欢这个…

 public class Q2634073 extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.q2634073); findViewById(R.id.item1).setOnClickListener(this); } @Override public void onClick(View view) { view.startAnimation(new MyScaler(1.0f, 1.0f, 1.0f, 0.0f, 500, view, true)); } public class MyScaler extends ScaleAnimation { private View mView; private LayoutParams mLayoutParams; private int mMarginBottomFromY, mMarginBottomToY; private boolean mVanishAfter = false; public MyScaler(float fromX, float toX, float fromY, float toY, int duration, View view, boolean vanishAfter) { super(fromX, toX, fromY, toY); setDuration(duration); mView = view; mVanishAfter = vanishAfter; mLayoutParams = (LayoutParams) view.getLayoutParams(); int height = mView.getHeight(); mMarginBottomFromY = (int) (height * fromY) + mLayoutParams.bottomMargin - height; mMarginBottomToY = (int) (0 - ((height * toY) + mLayoutParams.bottomMargin)) - height; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); if (interpolatedTime < 1.0f) { int newMarginBottom = mMarginBottomFromY + (int) ((mMarginBottomToY - mMarginBottomFromY) * interpolatedTime); mLayoutParams.setMargins(mLayoutParams.leftMargin, mLayoutParams.topMargin, mLayoutParams.rightMargin, newMarginBottom); mView.getParent().requestLayout(); } else if (mVanishAfter) { mView.setVisibility(View.GONE); } } } } 

通常的注意事项适用于:因为我们正在重写一个受保护的方法(applyTransformation),所以不能保证在将来的Android版本中工作。

如果不是,则将视图置于布局中,并为该布局设置android:animateLayoutChanges="true"

我用了和Andy一样的技巧。 我为此写了我自己的animation类,animation边缘的价值,导致项目的效果消失/出现。 它看起来像这样:

 public class ExpandAnimation extends Animation { // Initializations... @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(); } } } 

我有一个完整的例子,在我的博客文章http://udinic.wordpress.com/2011/09/03/expanding-listview-items/

我在这里使用了与Andy相同的技术,并对其进行了细化,使其可以用于无毛刺的展开和折叠,同时使用这里描述的技术: https : //stackoverflow.com/a/11426510/1317564

 import android.view.View; import android.view.ViewTreeObserver; import android.view.animation.ScaleAnimation; import android.view.animation.Transformation; import android.widget.LinearLayout; class LinearLayoutVerticalScaleAnimation extends ScaleAnimation { private final LinearLayout view; private final LinearLayout.LayoutParams layoutParams; private final float beginY; private final float endY; private final int originalBottomMargin; private int expandedHeight; private boolean marginsInitialized = false; private int marginBottomBegin; private int marginBottomEnd; private ViewTreeObserver.OnPreDrawListener preDrawListener; LinearLayoutVerticalScaleAnimation(float beginY, float endY, LinearLayout linearLayout) { super(1f, 1f, beginY, endY); this.view = linearLayout; this.layoutParams = (LinearLayout.LayoutParams) linearLayout.getLayoutParams(); this.beginY = beginY; this.endY = endY; this.originalBottomMargin = layoutParams.bottomMargin; if (view.getHeight() != 0) { expandedHeight = view.getHeight(); initializeMargins(); } } private void initializeMargins() { final int beginHeight = (int) (expandedHeight * beginY); final int endHeight = (int) (expandedHeight * endY); marginBottomBegin = beginHeight + originalBottomMargin - expandedHeight; marginBottomEnd = endHeight + originalBottomMargin - expandedHeight; marginsInitialized = true; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); if (!marginsInitialized && preDrawListener == null) { // To avoid glitches, don't draw until we've initialized everything. preDrawListener = new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { if (view.getHeight() != 0) { expandedHeight = view.getHeight(); initializeMargins(); adjustViewBounds(0f); view.getViewTreeObserver().removeOnPreDrawListener(this); } return false; } }; view.getViewTreeObserver().addOnPreDrawListener(preDrawListener); } if (interpolatedTime < 1.0f && view.getVisibility() != View.VISIBLE) { view.setVisibility(View.VISIBLE); } if (marginsInitialized) { if (interpolatedTime < 1.0f) { adjustViewBounds(interpolatedTime); } else if (endY <= 0f && view.getVisibility() != View.GONE) { view.setVisibility(View.GONE); } } } private void adjustViewBounds(float interpolatedTime) { layoutParams.bottomMargin = marginBottomBegin + (int) ((marginBottomEnd - marginBottomBegin) * interpolatedTime); view.getParent().requestLayout(); } }