Possible to animate a View inside a ViewGroup inside a LinearLayout?
I have a class that extends LinearLayout defined in main.xml. Programmatically I create x number of classes that extend ViewGroup. I have overridden canAnimate() to return true. Inside of that there are some more views also created programmatically. What I am trying to do is tap on one of those nested child views and have it animate (y-axis rotation ObjectAnimator). At the end of animation I hide the tapped view in the onAnimationEnd listener.
The touch event works, and I can see the view that was tapped hide after the given duration of the animation. The problem is that the view doesn't actually animate. If I use the same animation code on the ViewGroup extension, I can see that animate as you would expect.
What it seems like is that you can't animate child views that are nested more than 2 levels deep. Can anyone confirm this or give an example of how to animate a View of a ViewGroup of a LinearLayout?
This is in Honeycomb.
Thanks.
Update: I can nest 4 LinearLayouts and a View in XML and see it animate. I changed the class that extends ViewGroup to extend LinearLayout but still no animation with the child view. What could be set inflating a view that I'm not setting programmatically to allow animation of children?
public class MyViewGroup extends ViewGroup implements ViewGroup.OnTouchListener {
private View viewToAnimate;
public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setup(context);
}
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
setup(context);
}
public MyViewGroup(Context context) {
super(context);
setup(context);
}
private void setup(Context context)
{
viewToAnimate = new View(context);
viewToAnimate.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
viewToAnimate.setBackgroundColor(Color.YELLOW);
viewToAnimate.setOnTouchListener(this);
addView(viewToAnimate);
}
@Override
protected boolean canAnimate()
{
return true;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// Left out for brevity
}
public boolean onTouch(View v, MotionEvent event)
{
animateViewRotation();
return true;
}
private Interpolator accelerator = new A开发者_JS百科ccelerateInterpolator();
private Interpolator decelerator = new DecelerateInterpolator();
private void animateViewRotation()
{
// Tried all of the following:
/*
ObjectAnimator rotationY = ObjectAnimator.ofFloat(viewToAnimate, "rotationY", 0f, 90f);
AnimatorSet translations = new AnimatorSet();
translations.setDuration(1000);
translations.play(rotationY);
translations.start();
*/
/*
RotateAnimation ta = new RotateAnimation(0f, 90f, 0, 100);
ta.setDuration(1000);
viewToAnimate.startAnimation(ta);
*/
// Copied/adapted from API Demos code
ObjectAnimator animation = ObjectAnimator.ofFloat(viewToAnimate, "rotationY", 0f, 90f);
animation.setDuration(1000);
//animation.setInterpolator(accelerator);
animation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator anim) {
viewToAnimate.setVisibility(View.GONE);
}
});
animation.start();
}
}
精彩评论