Search code examples
androidandroid-layoutandroid-animationandroid-viewgroup

Trespass ViewGroup's Limits when using animations


I have a ImageView inside a RelativeLayout. The ImageView is filling the whole RelativeLayout. The RelativeLayout cannot become bigger.

I want to make the ImageView bigger using ScaleAnimation like that:

final ScaleAnimation scaleAnimationGoBigger = new ScaleAnimation(1, 1.5f, 1, 1.5f,        Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
scaleAnimationGoBigger.setDuration(1000);
scaleAnimationGoBigger.setFillAfter(true);
myImageView.startAnimation(scaleAnimationGoBigger);

The RelativeLayout's boudaries do not allow the whole new bigger ImageView to be shown, showing only the parts that fits the RelativeLayout (making it to looks like a zoom effect).

So my question is: is there a way to tell a View inside a ViewGroup to not obey (to trespass) the ViewGroup's boundaries where it lies in (at least during the animation) ?


Solution

  • is there a way to tell a View inside a ViewGroup to not obey (to trespass) the ViewGroup's boundaries where it lies in ....

    Yes. Lets say you have a View (myImageView) inside some ViewGroup (viewGroupParent). Just called setClipChildren(false)

        ViewGroup viewGroupParent = (ViewGroup) myImageView.getParent();
        viewGroupParent.setClipChildren(false);
    

    More info here : http://developer.android.com/reference/android/view/ViewGroup.html#attr_android:clipChildren

    (at least during the animation) ?

    Use Animation.AnimationListener, altogether, it should look something like this:

    final ViewGroup viewGroupParent = (ViewGroup) myImageView.getParent();
    viewGroupParent.setClipChildren(false);
    
    final ScaleAnimation scaleAnimationGoBigger = new ScaleAnimation(1, 1.5f, 1, 1.5f,        Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
    scaleAnimationGoBigger.setDuration(1000);
    scaleAnimationGoBigger.setFillAfter(true);
    scaleAnimationGoBigger.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                viewGroupParent.setClipChildren(false);
            }
    
            @Override
            public void onAnimationEnd(Animation animation) {
                // uncomment here if you want to restore clip : viewGroupParent.setClipChildren(true);
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
                // do nothing
            }
        });
    myImageView.startAnimation(scaleAnimationGoBigger);
    

    HTHS!