Android:动画被父视图剪裁

JDx*_*JDx 24 layout animation android

我目前有一个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>
Run Code Online (Sandbox Code Playgroud)

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) {
    }

}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Her*_*rHo 73

对你而言有点晚了,但对于每个寻找答案的人:你可以调用setClipChildren(false)封闭的ViewGroups(比如RelativeLayoutLinearLayout).

  • 优秀的anwser!如果使用XML,也可以清理代码:android:clipChildren ="false" (10认同)
  • 优秀的答案!谢谢!值得一提的是,在主父级(根布局)上将 clipChildren 设置为“false”就足够了。 (2认同)

JDx*_*JDx 0

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