Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have a parent View (a RelativeLayout), with a child View. I increase the scale of the parent View with FillAfter = true, and subsequently I animate the alpha of the child View. During the animation of the child view’s alpha, only the portion of the view covering the original location of the view is animated. To me, it appears that the frame invalidation of the child view animation is not taking into account the parent view’s scale transformation. I’m wondering how I can achieve this – the ability of being able to animate/transform arbitrary layers of my view hierarchy.

RelativeLayout rootLayout = FindViewById<RelativeLayout>(Resource.Id.RootLayout);
rootLayout.SetClipChildren(false);

RelativeLayout parentLayout = new RelativeLayout(this);
parentLayout.SetClipChildren(false);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(500, 500);
layoutParams.LeftMargin = (this.Window.WindowManager.DefaultDisplay.Width - 500) / 2;
layoutParams.TopMargin = (this.Window.WindowManager.DefaultDisplay.Height - 500) / 2;
parentLayout.LayoutParameters = layoutParams;
parentLayout.SetBackgroundColor(new Color(255, 0, 0, 255));
rootLayout.AddView(parentLayout);

View childView = new View(this);
RelativeLayout.LayoutParams viewLayoutParams = new RelativeLayout.LayoutParams(200, 200);
viewLayoutParams.LeftMargin = 150;
viewLayoutParams.TopMargin = 150;
childView.LayoutParameters = viewLayoutParams;
childView.SetBackgroundColor(new Color(0, 255, 0, 255));
parentLayout.AddView(childView);

Button growButton = FindViewById<Button>(Resource.Id.growButton);
growButton.Click += delegate
    {
        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.2f, 1, 1.2f, parentLayout.Width / 2, parentLayout.Height / 2);
        scaleAnimation.Duration = 1000;
        scaleAnimation.FillAfter = true;
        parentLayout.StartAnimation(scaleAnimation);
    };

Button fadeButton = FindViewById<Button>(Resource.Id.fadeButton);
fadeButton.Click += delegate
    {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0.5f);
        alphaAnimation.Duration = 1000;
        alphaAnimation.FillAfter = true;
        childView.StartAnimation(alphaAnimation);
    };

Here is a picture of the result of clicking the grow button followed by the fade button: http://i.stack.imgur.com/Usm9c.png

This scenario is contrived - in my project the parent view has multiple types of animations applied to it, and it overrides the draw method to apply those transformations to itself when the animations are not running. And this parent view contains multiple child views that are themselves animated in many ways (including background color animation via TransitionDrawable).

I recognize there are round-about ways around this – e.g. decoupling the children from the parent, animating them separately, and subclassing the AlphaAnimation to update the child view transformation with the current scale value in ApplyTransformation – but that becomes absurd when extrapolating to more complex animations. And I’m pretty sure I’ll just run into another wall wrt the background animation via TransitionDrawable - unlike Animations I don’t know when I have the opportunity to transform the area it will eventually invalidate (a last resort would be to fall back to overriding AnimationDrawable).

There has to be a better way to do this, I feel like I must be missing something here. I don’t consider these types of animations very complicated; they are trivial to do on WP and iOS. What road do I turn down? Do I need to create my own animation threads and handle my own invalidation? Should I be utilizing SurfaceViews?

I’m targeting Gingerbread, so I haven’t played with Property Animations – but I am curious if they would make this any easier? Switching targets may be an option if it would make development substantially simpler.

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.