Android视图在离开父母时消失



我在这个LinearLayout中有一个LinearLayout和ImageView。

ImageView有翻译效果。

// v = ImageView    
ObjectAnimator animation2 = ObjectAnimator.ofFloat(v, "translationY", 200);
                        animation2.setDuration(3000);
                        animation2.setTarget(v);
                        animation2.start();

动画正在工作,但当ImageView超出LinearLayout时,它正在消失。

如何在不修改LinearLayout高度的情况下修复它。

找到ImageView所属的ViewGroup并应用ViewGroup.setClipChildren(false)。默认情况下,子对象的绘制仅限于父对象ViewGroup的边界。

存在两个可能导致这种情况发生的属性:clipChildren和clipToPadding。对于对象将在其边界之外设置动画的每个父ViewGroup,您需要将clipChildren设置为false。您还需要将clipToPadding设置为直接父对象(也许还有更多,但我还没有看到这样的情况)。

您可以在XML 中设置这两个属性

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

或代码

viewGroup.setClipChildren(false);
viewGroup.setClipToPadding(false);

我的实现。它可能会帮助一些人:

Java版本:

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;
    }
}

呼叫setAllParentsClip(yourView, false);以禁用所有父对象中的剪辑。

编辑:

Kotlin作为扩展函数的版本:

fun View.setAllParentsClip(enabled: Boolean) {
    var parent = parent
    while (parent is ViewGroup) {
        parent.clipChildren = enabled
        parent.clipToPadding = enabled
        parent = parent.parent
    }
}

调用:yourView.setAllParentsClip(false)

在我的案例中,clipChildren什么也没做,但clipToPadding="false"解决了这个问题。转到图。

获取视图高度,然后将高度的百分比添加到它将滑动到的位置

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);
}
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();

最新更新