Java 离开父级时,Android 视图消失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18048997/
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
Android View Disappearing When Go Outside Of Parent
提问by Eray
I have a LinearLayout and ImageView inside this LinearLayout.
我在这个 LinearLayout 中有一个 LinearLayout 和 ImageView。
There is a translation effect for ImageView.
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.
动画工作但当 ImageView 超出 LinearLayout 时它消失了。
How can i fix it without modify LinearLayout's height.
如何在不修改 LinearLayout 高度的情况下修复它。
采纳答案by JuniKim
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.
找到 ImageView 所属的 ViewGroup 并应用ViewGroup.setClipChildren(false)。默认情况下,子级的绘制仅限于父级 ViewGroup 的边界。
回答by Maxwell
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).
存在两个可能导致这种情况发生的属性:clipChildren 和 clipToPadding。您需要为每个父 ViewGroup 设置 clipChildren 为 false,对象将在其边界外设置动画。您还需要将 clipToPadding 设置为直接父级(也许更多,但我还没有看到它的案例)。
You can set both attributes in the XML
您可以在 XML 中设置这两个属性
android:clipChildren="false"
android:clipToPadding="false"
or in code
或在代码中
viewGroup.setClipChildren(false);
viewGroup.setClipToPadding(false);
回答by Opus1217
In my case clipChildren did nothing but clipToPadding="false"
fixed the problem. Go figure.
在我的例子中,clipChildren 什么也没做,只是clipToPadding="false"
解决了这个问题。去搞清楚。
回答by ahmed_khan_89
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.
调用
setAllParentsClip(yourView, false);
以禁用所有父项中的剪辑。
Edited:
编辑:
Kotlin's version as an extension function:
Kotlin 的版本作为扩展函数:
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)
称呼: yourView.setAllParentsClip(false)
回答by Prateek Yadav
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();
回答by Martin Itotia
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);
}