Ok, so i checked out http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html
He says you can animate the property of an object in a given time. A
I think a better way to accomplish this would be to use View's scaleX and scaleY property which are defined all the way down in View class, so it would be valid with practically any view or layout.
For example, consider this
ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(MyLinearLayout,"scaleX",0f,1f);
objectAnimator.setDuration(300);
objectAnimator.start();
It works.
You have made a mistake
params.weight = myWidth;
I think it is params.width not params.weight
For what it's worth this works. After you change the width on the layout params you have to reset the layout params object.
private class WidthEvaluator extends IntEvaluator {
private View v;
public WidthEvaluator(View v) {
this.v = v;
}
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
int num = (Integer)super.evaluate(fraction, startValue, endValue);
ViewGroup.LayoutParams params = v.getLayoutParams();
params.width = num;
v.setLayoutParams(params);
return num;
}
}
// your client code
ValueAnimator.ofObject(new WidthEvaluator(box), box.getWidth(), v.getWidth()).start();
I've created a small library ViewPropertyObjectAnimator that can do that in a very simple way (and uses a similar approach to this proposed by Tomer Weller).
You could achieve this animation with (assuming the mLinearLayout
is the animated View
and mEndWidth
is the end value of the View's
width):
ViewPropertyObjectAnimator.animate(mLinearLayout).width(mEndWidth).start();
In situations where there isn't a simple property getter/setter you should use ValueAnimator and perform the animation manually.
Assuming:
Your code should look something like this:
ValueAnimator anim = ValueAnimator.ofInt(v.getMeasuredWidth(), END_WIDTH);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
layoutParams.width = val;
v.setLayoutParams(layoutParams);
}
});
anim.setDuration(DURATION);
anim.start();