Android:动画被父视图裁剪

31

我目前有一个ImageView,我正在对其应用缩放动画。这个视图位于一个相对布局内,该布局的layout_height和layout_width属性设置为wrap_content。问题是,当动画开始时,它会使ImageView变大,超出其父布局的范围,然后图片被裁剪掉了。有没有解决这个问题的方法?

以下是一个可工作的示例:

XML文件 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip" 
    android:layout_gravity="center_horizontal">
    <ImageView
        android:id="@+id/imgO"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/circle"/>
    </RelativeLayout>
</LinearLayout>

Java文件 -

ImageView imgO;

ScaleAnimation makeSmaller;
ScaleAnimation makeBigger;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.titlescreen);

    //Animation
    imgO = (ImageView)findViewById(R.id.imgO);

    makeSmaller = new ScaleAnimation((float)10.0, (float)1.0, (float)10.0, (float)1.0, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
    makeSmaller.setAnimationListener(new MyAnimationListener());
    makeSmaller.setFillAfter(true);
    makeSmaller.setDuration(500);
    makeBigger = new ScaleAnimation((float)1.0, (float)10.0, (float)1.0, (float)10.0, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
    makeBigger.setAnimationListener(new MyAnimationListener());
    makeBigger.setFillAfter(true);
    makeBigger.setDuration(750);
    imgO.startAnimation(makeBigger);
}

class MyAnimationListener implements AnimationListener {

    public void onAnimationEnd(Animation animation) {

        ScaleAnimation sa = (ScaleAnimation)animation;

        if (sa.equals(makeSmaller))
            imgO.startAnimation(makeBigger);
        else
            imgO.startAnimation(makeSmaller);
    }

    public void onAnimationRepeat(Animation animation) {
    }

    public void onAnimationStart(Animation animation) {
    }

}

谢谢。

3个回答

80

虽然有些晚了,但是对于所有寻找答案的人:您可以在封装的ViewGroups(例如RelativeLayoutLinearLayout)上调用setClipChildren(false)


11
非常好的答案!如果您使用XML,还可以清理您的代码: android:clipChildren="false" - Ricky
谢谢,如果这些孩子是按钮,你会如何保留onClick事件监听器?我的事件监听器总是消失。 - tricknology
2
非常出色的答案!谢谢!值得一提的是,在主父级(根布局)上将clipChildren设置为“false”就足够了。 - daxgirl

1
我正在为子视图应用翻译和透明度动画,但父视图正在剪切子视图的边界。
对我来说,@HerrHo的答案本身并没有起作用,但是一旦我添加了

,问题就得到解决了。
android:clipChildren="false"
android:clipToPadding="false"

一起,它开始工作了!


我还需要添加 android:clipToPadding="false"。感谢您提供的额外信息! - Randy

0

我通过将所有内容填充父级,然后水平和垂直居中图像来解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接