当View
的可见性设置为GONE
时,我想创建一个Animation
。View
应该“崩溃”,而不是只是失望。我用ScaleAnimation
试过了,但是View
崩溃了,但是布局只会在Animation
停止(或开始)之后(或之前)调整它的空间大小。
如何制作Animation
,以便在动画时,较低的View
%s将直接位于内容的正下方,而不是留出空白?
发布于 2010-10-17 19:06:29
似乎没有一种简单的方法可以通过API来做到这一点,因为动画只是改变视图的渲染矩阵,而不是实际的大小。但我们可以设置一个负边距来欺骗LinearLayout,使其认为视图正在变小。
因此,我建议您创建自己的基于ScaleAnimation的动画类,并覆盖"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),所以这不能保证在未来的安卓版本中也能工作。
发布于 2013-07-03 13:19:57
如果视图不在布局中,则将其放入布局中,并为该布局设置android:animateLayoutChanges="true"
。
发布于 2012-05-14 18:20:21
我使用了和Andy在这里介绍的相同的技术。为此,我编写了自己的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/上有一个完整的例子
https://stackoverflow.com/questions/2634073
复制相似问题