Android clipChildren 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22930626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
clipChildren is not working
提问by Vamshi
In my application i am trying to move images using animation.
When i try to animate the image (imageView2
or imageView4
) to imageView1
, the image is cutting when it is moving out of the Layout.
Here Source image is imageView2
or imageView4
and destination image is imageView1
在我的应用程序中,我试图使用动画移动图像。当我尝试将图像(imageView2
或imageView4
)动画化为 时,图像imageView1
在移出布局时正在剪切。这里源图像是imageView2
或imageView4
目标图像是imageView1
Below is my XML:
下面是我的 XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/animRL"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/deskcard" />
<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" >
<RelativeLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:clipChildren="false"
android:clipToPadding="false"
android:gravity="bottom|center" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageView4"
android:layout_marginLeft="118dp"
android:clipChildren="false"
android:clipToPadding="false"
android:src="@drawable/test" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="104dp"
android:clipChildren="false"
android:clipToPadding="false"
android:src="@drawable/test1" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
And animation code is below:
动画代码如下:
imageView4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int sourceCoords[] = new int[2];
int targetCoords[] = new int[2];
int xDelta;
int yDelta;
imageView4.getLocationOnScreen(sourceCoords);
image.getLocationOnScreen(targetCoords);
xDelta = targetCoords[0] - sourceCoords[0];
yDelta = targetCoords[1] - sourceCoords[1];
TranslateAnimation animation = new TranslateAnimation(0, xDelta, 0, yDelta);
animation.setDuration(400);
animation.setFillAfter(false);
animation.setAnimationListener(new MyAnimationListener());
imageView4.startAnimation(animation);
}
});
回答by roflharrison
One of the parents of your RelativeLayout might be clipping children (sometimes compatibility libraries add a mystery ViewGroup such as NoSaveStateFrameLayoutfor example). I've used something like this in the past with success to disable clip on all parents of a view:
您的 RelativeLayout 的父母之一可能正在剪辑孩子(有时兼容性库会添加一个神秘的 ViewGroup,例如NoSaveStateFrameLayout)。我过去使用过类似的东西,成功地禁用了视图的所有父级的剪辑:
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());
}
}
回答by Xavier.S
The final reason is "RelativeLayout". So to solve it, don't use RelativeLayout as your Larger-than-parent control's parent. FrameLayout instead, like:
最后一个原因是“RelativeLayout”。所以要解决这个问题,不要使用RelativeLayout 作为你的Larger-than-parent 控件的父控件。FrameLayout 代替,例如:
<!-- 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>
回答by mhdjazmati
i know its late , but this is the simplest complete answer :
我知道它晚了,但这是最简单的完整答案:
just put on every layout :
只需穿上每个布局:
android:clipChildren="false"
android:clipToPadding="false"
回答by Kehui Li
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.
@roflharrison 的想法很不错,但是代码有问题:这里我们递归禁用clipChildren,但是当我们到达根视图时,它v.getParents()
会为null,该方法立即返回,并且不会禁用它的ClipChildren 属性.
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:
?? 视图的父级不应该是 ViewGroup 吗?我们不应该禁用 ViewGroup 的剪辑属性,而不是 View 的吗?所以我将代码更改为以下内容,并且效果很好:
public void disableClipOnParents(View v) {
if (v == null) {
return;
}
if (v instanceof ViewGroup) {
((ViewGroup) v).setClipChildren(false);
}
disableClipOnParents((View) v.getParent());
}
回答by Yun.Lu
Hi~ if your layout includes "paddingXXX" tags, you can remove them and try again. it's work for me~
嗨~如果你的布局包含“paddingXXX”标签,你可以删除它们并重试。对我有用~
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:gravity="center_vertical"
android:padding="8dp"
android:orientation="horizontal">
code above will clip childView and next will not:
上面的代码将剪辑 childView,接下来不会:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:gravity="center_vertical"
android:orientation="horizontal">