68

I have a LinearLayout and ImageView inside this LinearLayout.

There is a translation effect for ImageView.

// v = ImageView    
ObjectAnimator animation2 = ObjectAnimator.ofFloat(v, "translationY", 200);
                        animation2.setDuration(3000);
                        animation2.setTarget(v);
                        animation2.start();

Animation working but it's disappearing when ImageView go outside of LinearLayout.

How can i fix it without modify LinearLayout's height.

Angel Koh
  • 12,479
  • 7
  • 64
  • 91
Eray
  • 7,038
  • 16
  • 70
  • 120

6 Answers6

89

Find the ViewGroup that the ImageView belongs to and apply ViewGroup.setClipChildren(false). By default, the drawing of the children is limited to the bounds of the parent ViewGroup.

JuniKim
  • 1,067
  • 8
  • 4
  • 5
    Setting LinearLayout's `android:clipChildren="false"`on XML but nothing changed? – Eray Aug 05 '13 at 01:23
  • 60
    I've set `clipChildren="false"` to ALL parents of ImageView (I mean parent of LinearLayout). And it's working. Thank you. – Eray Aug 05 '13 at 01:45
  • 6
    If yet not have scrolled down, see the answer by maxwell below. Set `android:clipToPadding="false"` as well. – Darpan Jul 10 '15 at 12:44
  • @Eray: Can we handle clicks on that ImageView? – Mehul Joisar Jul 13 '15 at 13:14
  • 1
    You may have to set clip children not just for the immediate parent ViewGroup but other ViewGroups up the hierarchy. – RajV Aug 26 '16 at 01:30
  • On my side, I don't have to set this to all parents. Just up to the RecyclerView for me was enough – Tobliug Apr 10 '17 at 13:48
  • after setting setClipChildren == false not able to hide the view – Jarvis Jun 09 '18 at 16:00
  • if i create custom view then this option is not working, with android view like RalativeView---> RelativeView---> Image then its working – Jarvis Jun 11 '18 at 10:10
  • it allows to move the view outside layout but after dropping view outside the layout, I am not able to touch that view again – Bhargav Thanki Jun 23 '18 at 11:20
80

Two attributes exist that may cause this to happen: clipChildren and clipToPadding. You'll need to set clipChildren to false for each parent ViewGroup whose bounds the object will animate out of. You also need to set clipToPadding to the immediate parent (and maybe more, but I haven't seen a case for it yet).

You can set both attributes in the XML

android:clipChildren="false"
android:clipToPadding="false"

or in code

viewGroup.setClipChildren(false);
viewGroup.setClipToPadding(false);
Maxwell
  • 6,532
  • 4
  • 37
  • 55
  • 3
    I personnally had to set clipToPadding to false on every parents too. Weird. – Renaud Cerrato Oct 22 '15 at 16:10
  • 9
    `Each parent ViewGroup` is key! Thanks for this answer. – Justin Pollard Nov 12 '15 at 19:57
  • This was pretty helpful. Thanks mate! – Wahib Ul Haq Nov 18 '16 at 17:24
  • ***Exactly!*** 1) Set **`clipChildren`** to `false` for **each parent *whose bounds the view***. 2) Set **`clipToPadding`** to `false` for **immediate parent**. But still doesn't work for `RelativeLayout` as I tried. See [here](https://stackoverflow.com/questions/40714558/android-why-attribute-clipchildren-doesnt-work-in-relativelayout-but-works-in). – Mir-Ismaili Jul 29 '18 at 00:22
16

My implementation. It can probably help somebody:

Java version:

public static void setAllParentsClip(View v, boolean enabled) {
    while (v.getParent() != null && v.getParent() instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) v.getParent();
        viewGroup.setClipChildren(enabled);
        viewGroup.setClipToPadding(enabled);
        v = viewGroup;
    }
}

call setAllParentsClip(yourView, false); to disable the clipping in all the parents.

Edited:

Kotlin's version as an extension function:

fun View.setAllParentsClip(enabled: Boolean) {
    var parent = parent
    while (parent is ViewGroup) {
        parent.clipChildren = enabled
        parent.clipToPadding = enabled
        parent = parent.parent
    }
}

Call: yourView.setAllParentsClip(false)

Blunderer
  • 934
  • 7
  • 15
ahmed_khan_89
  • 2,755
  • 26
  • 49
  • Are you sure you're applying the setAllParentsClip(yourView, false); to the right view? are you sure you're facing the same problem as the asked question? I am sorry but I honestly don't know why it is not working for you? – ahmed_khan_89 May 22 '18 at 13:33
1

In my case clipChildren did nothing but clipToPadding="false" fixed the problem. Go figure.

josliber
  • 43,891
  • 12
  • 98
  • 133
Opus1217
  • 723
  • 4
  • 17
1

Get the view height, then add a percentage of the height to where it will slide to

public void SlideUp(View view){
     float height = view.getHeight();

     TranslateAnimation animate = new TranslateAnimation(0,0,0,0);   

     animate.setDuration(500);
     animate.setFillAfter(true);

     view.animate().translationY((float)(0-0.62*height)).start(); 
     view.startAnimation(animate);
}
Khawaja Asim
  • 1,327
  • 18
  • 38
0
try to update camera position as in my case below:
 ValueAnimator lockAnimator = ValueAnimator.ofFloat(1, 0);     // value from 0 to 1
                lockAnimator.setDuration(500);
                lockAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator pAnimation) {
                        float value = (Float) (pAnimation.getAnimatedValue());
                        if (value < .6 && flipped) {
                            if (preview != null)
                                mCanvasImage.setImageBitmap(preview);
                            else
                                mCanvasImage.setImageBitmap(imageBitmap);
                            flipped = false;
                        }
                        if (value > .3 && value < .7) {
                            lyt_rlt_container.setCameraDistance(lyt_rlt_container.getCameraDistance() - 100);
                        } else {
                            lyt_rlt_container.setCameraDistance(lyt_rlt_container.getCameraDistance() + 100);
                        }
                        lyt_rlt_container.setRotationY(180 * value);

                    }
                });
                lockAnimator.start();
Prateek Yadav
  • 932
  • 6
  • 8