Android View disappears when it goes beyond the parent

I have 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 works, but it disappears when ImageView goes beyond LinearLayout.

Here you can see the problem: http://screenr.com/zoAH

How can I fix this without changing the height of LinearLayout.

+59
java android animation android-animation
Aug 05 '13 at 0:19
source share
6 answers

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

+79
Aug 05 '13 at 1:15 on
source share

There are two attributes that can cause this: clipChildren and clipToPadding. You will need to set clipChildren to false for each parent ViewGroup, the borders of which the object will animate from. You also need to install clipToPadding for the immediate parent (and maybe more, but I haven't seen it for him yet).

You can set both attributes in XML

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

or in code

 viewGroup.setClipChildren(false); viewGroup.setClipToPadding(false); 
+74
Sep 13 '13 at 15:58
source share

My implementation. This might help someone:

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); disable clipping for all parents.

Edited by:

Kotlin 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)

+15
Mar 24 '16 at 16:29
source share

In my case, clipChildren did nothing but clipToPadding="false" fixed the problem. Hover over your mouse.

+1
Jun 11. '15 at 16:21
source share

Get the height of the view, then add a percentage of the height to which it will move.

 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); } 
+1
Jan 26 '18 at 10:53
source share
 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(); 
0
Jan 03 '17 at 10:50
source share



All Articles