Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android View Disappearing When Go Outside Of Parent

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.

like image 376
Eray Avatar asked Aug 05 '13 00:08

Eray


3 Answers

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.

like image 178
JuniKim Avatar answered Nov 14 '22 13:11

JuniKim


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);
like image 20
Maxwell Avatar answered Nov 14 '22 13:11

Maxwell


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)

like image 16
ahmed_khan_89 Avatar answered Nov 14 '22 15:11

ahmed_khan_89