Avoiding Clipping when Animating a View on top of a SurfaceView

I recently changed to using a SurfaceView for one part of an app (Android 2.2).  At certain points, I animate another view on top of this view  I noticed that after I had changed to using the SurfaceView, the animation was getting clipped by a rectangle that looked like it was about the size of the view without any animation effects.  This looked silly, so I was about to consider using a simpler scale/alpha animation that would avoid hitting this, when I came across a stackoverflow post (http://stackoverflow.com/questions/4772663/why-are-view-animations-sometimes-clipped/6012120#6012120) from someone having the same problem earlier this year.

It turns out that simply adding an empty view to the overarching FrameLayout that includes the animated view apparently fixes the problem.  I haven't looked into exactly why this is.  And if you remove the extra empty view after adding it, the problem comes back (I wanted to try that to avoid having another view in the hierarchy, in case the add was just getting some kind of machinery to kick in that would stay).

So, as indicated in the stackoverflow post, all you need to do is this in your activity:

myFrameLayout.addView(new View(this));

and voila, the clipping is clipped.

Last year,  this guy even wrote a modified version of FrameLayout to get around some aspects of the problem, and he pointed at the method "ViewGroup.gatherTransparentRegion" as the problem.  Well, this method was apparently written to deal with SurfaceViews in the first place; here's the javadoc in View:

View.gatherTransparentRegion javadoc:
This is used by the RootView to perform an optimization when the view hierarchy contains one or several SurfaceView.

OK, back on the road to Ithaca.

Popular Posts