Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clipChildren is not working even though set to false?

In my application I am trying to move images using animation. When I try to animate the image is cutting even though I use clipChildren false in every xml block

    <RelativeLayout
        android:id="@+id/baselayout"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/imageView1"
        android:clipChildren="false"
        android:clipToPadding="false" >
like image 868
Vamshi Avatar asked Apr 08 '14 07:04

Vamshi


4 Answers

One of the parents of your RelativeLayout might be clipping children (sometimes compatibility libraries add a mystery ViewGroup such as NoSaveStateFrameLayout for example). I've used something like this in the past with success to disable clip on all parents of a view:

public void disableClipOnParents(View v) {
    if (v.getParent() == null) {
        return;
    }

    if (v instanceof ViewGroup) {
        ((ViewGroup) v).setClipChildren(false);
    }

    if (v.getParent() instanceof View) {
        disableClipOnParents((View) v.getParent());
    }
}
like image 69
roflharrison Avatar answered Oct 28 '22 08:10

roflharrison


I know its late, but this is the simplest complete answer:

just put on every layout :

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

Note @roflharrison mentioned: this won't work in the cases where the operating system wraps/injects your view in ViewGroup's that's aren't in your layout

like image 43
mhdjazmati Avatar answered Oct 28 '22 09:10

mhdjazmati


The final reason is "RelativeLayout". So to solve it, don't use RelativeLayout as your Larger-than-parent control's parent. FrameLayout instead, like:

<!-- it's ok as grand parent -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipChildren="false">

    <!-- parent must not be a RelativeLayout -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="9dp">

        <!-- Your Larger-than-parent View -->
        <View
            android:layout_width="56dp"
            android:layout_height="138dp"
            android:layout_gravity="center"
            android:background="@android:color/black" />

    </FrameLayout>

</RelativeLayout>
like image 11
Xavier.S Avatar answered Oct 28 '22 07:10

Xavier.S


The idea of @roflharrison is quite good, however, the codes has some problem: Here we disable clipChildren recursively, but when we reached the root view, it's v.getParents() would be null, the method returns immediately, and its ClipChildren attribute won't be disabled.

What's more, for the following line:

if (v.getParent() instanceof View)

?? Shouldn't the parent of the view be a ViewGroup? And shouldn't we disable ViewGroup's clip attributes, not View's? So I change the code to the following, and it worked quite well:

public void disableClipOnParents(View v) {
    if (v == null) {
        return;
    }
    if (v instanceof ViewGroup) {
        ((ViewGroup) v).setClipChildren(false);
    }
    disableClipOnParents((View) v.getParent());
}
like image 4
Kehui Li Avatar answered Oct 28 '22 07:10

Kehui Li