Android 2.3:增长/缩小动画Bug?

5
对于我的应用程序,我试图为布局中的每个ImageView添加[增长/缩小+透明度更改]动画。 我设法让动画起作用,并通过为我的两个XML文件(grow.xml和shrink.xml)设置fillAfter =“true”来使每个动画在完成后都持续存在。 但是,当我为shrink.xml设置fillAfter =“true”时,似乎会出现一些奇怪的动画错误,导致未选定的图像变大,然后“弹回”到正常大小! 让我解释一下应用程序的工作方式,然后给出一个场景,以便更加清楚:
最初,所有图像的alpha级别都设置为50%。 当我单击特定图像时,它将增长到120%,其alpha级别将变为100%(“点亮”效果)。 当我单击另一个图像时,先前选择的图像将缩小回到100%,其alpha级别将返回到50%,当前选择的图像将按上述方式增长。
在我的布局中,我有三个相同大小的图像排成一行。 我单击第一个图像,然后单击第二个图像,然后再次单击第一个图像。 好的,没有问题。 现在,我单击第三个图像,我遇到了第一个图像的奇怪弹跳问题。 有什么办法可以解决这个问题吗?
我已经尝试过:
1.使用image.setAlpha(...)来避免在shrink.xml中设置alpha级别,然后调用fillAfter =“true”,但不幸的是,那是一个API 11调用
2.仅将alpha标记的fillAfter属性设置为true在shrink.xml中
3.在缩小动画之后立即调用image.startAnimation(fadeOut),但看起来很可怕。
4.重写onAnimationEnd(),但这个调用从未到达(??)

shrink.xml:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fillAfter="true"> <scale android:fromXScale="1.2" android:toXScale="1.0" android:fromYScale="1.2" android:toYScale="1.0" android:duration="300" android:pivotX="50%" android:pivotY="50%"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.5" android:duration="300"/> </set>

grow.xml

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fillAfter="true"> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="1.0" android:toXScale="1.20" android:fromYScale="1.0" android:toYScale="1.20" android:duration="300" android:pivotX="50%" android:pivotY="50%" /> <alpha android:fromAlpha="0.5" android:toAlpha="1.0" android:duration="300"/> </set>

fade_out.xml:

<?xml version="1.0" encoding="UTF-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:startOffset="300" android:fromAlpha="1.0" android:toAlpha="0.5" android:fillAfter="true"> </alpha>

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <ImageView android:id="@+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dip" android:paddingRight="20dip" android:src="@drawable/image1"/> <ImageView android:id="@+id/image2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dip" android:paddingRight="20dip" android:src="@drawable/image2"/> <ImageView android:id="@+id/image3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dip" android:paddingRight="20dip" android:src="@drawable/image3"/> </LinearLayout>

Test.java:

    public class Test extends Activity {
    private View mSelected;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Animation fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out);
    final Animation grow = AnimationUtils.loadAnimation(this, R.anim.grow);
    final Animation shrink = AnimationUtils.loadAnimation(this, R.anim.shrink);

    OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (mSelected == v) 
                return;

            if (mSelected != null) 
                mSelected.startAnimation(shrink);

            mSelected = v;
            mSelected.startAnimation(grow);             
        }
    };

    ImageView image1 = (ImageView)findViewById(R.id.image1);
    image1.startAnimation(fadeOut);
    image1.setOnClickListener(listener); 

    ImageView image2 = (ImageView)findViewById(R.id.image2);
    image2.startAnimation(fadeOut);
    image2.setOnClickListener(listener);

    ImageView image3 = (ImageView)findViewById(R.id.image3);
    image3.startAnimation(fadeOut);
    image3.setOnClickListener(listener);
}}

1个回答

2

问题在于你的缩小动画仍然分配给了其他视图。 当你调用 mSelected.startAnimation() 时,你启动的是动画对象,它附加到其他视图,因此它们也会动画。 你可以通过将 mSelected.startAnimation(shrink); 更改为以下内容来创建一个新的动画实例:

mSelected.startAnimation(AnimationUtils.loadAnimation(Test.this, R.anim.shrink));

解决问题的简单(但效率低下)方法是通过取消视图中的动画来自行管理动画周期 (mSelected.setAnimation(null))。


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