在动画期间更改ImageView图像

时间:2012-12-06 12:47:32

标签: android android-animation android-imageview

我有ImageView。我想:
1 - 收缩视图
2 - 更改图像资源
3 - 缩小视图

我在下面的代码部分尝试了这个,但这不起作用。它表现得像第3行不存在。有人能告诉我我做错了吗?

Animation anim = AnimationUtils.loadAnimation(this, R.anim.scale);
Animation animo = AnimationUtils.loadAnimation(this, R.anim.scale_out);
i.startAnimation(anim);
i.setImageResource(R.drawable.logoc);
i.startAnimation(animo);

scale.xml:

 <?xml version="1.0" encoding="utf-8"?>
 <scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0" 
android:toXScale="0.125"
android:fromYScale="1.0" 
android:toYScale="0.125" 
android:pivotX="50%"
android:pivotY="50%" 
android:startOffset="0" 
android:duration="400"
android:fillBefore="true" />

scale_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.0" 
android:toXScale="4.0"
android:fromYScale="1" 
android:toYScale="4.0" 
android:startOffset="400" 
android:pivotX="50%"
android:pivotY="50%"
android:duration="800"
 />

1 个答案:

答案 0 :(得分:2)

我认为你应该尝试这样做:

i.startAnimation(anim);
if (anim.hasEnded())
{
    i.setImageResource(R.drawable.logoc);
    i.startAnimation(animo);
}

编辑:

好的,我错了:这有效:

public class MyAnimationListener implements AnimationListener {

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub

            i.setImageResource(R.drawable.bg);
            i.startAnimation(animo);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

    }

MyAnimationListener mListener = new MyAnimationListener();
        anim.setAnimationListener(mListener);
        i.startAnimation(anim);