Android ImageView连续放大和缩小

时间:2013-08-14 12:05:18

标签: android animation imageview

有没有办法在Android中连续放大和缩小ImageView。我尝试使用下面的代码,但只有一个Zoom功能正常工作。

zoomin.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" > 
    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="20000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="3"
        android:toYScale="3" >
    </scale>

</set>

zoomout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" > 
    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="20000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" >
    </scale>

</set>

我所在的Activity课程:

Animation zoomin, zoomout; //declared as public

@Override
public void onCreate(Bundle savedInstanceState) {
   // animation
    zoomin = AnimationUtils.loadAnimation(this, R.anim.zoomin);
    zoomout = AnimationUtils.loadAnimation(this, R.anim.zoomout);
    bgImage.setAnimation(zoomin);
    bgImage.setAnimation(zoomout);
    Thread t = new Thread(new Zoom());
    t.start();
}
private class Zoom implements Runnable {
    @Override
    public void run() {
        while (true) {              
            bgImage.startAnimation(zoomin);
            try {
                Thread.sleep(8000);
            } catch (InterruptedException e) {
                                    e.printStackTrace();
            }               
            bgImage.startAnimation(zoomout);
        }
    }
}

这里zoomin动画似乎工作正常。有没有办法连续实现zoominzoomout动画???

谢谢

4 个答案:

答案 0 :(得分:18)

使用此而不是线程

 zoomin.setAnimationListener(new AnimationListener() {

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

        }

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

        }

        @Override
        public void onAnimationEnd(Animation arg0) {
            bgImage.startAnimation(zoomout); 

        }
    });

 zoomout.setAnimationListener(new AnimationListener() {

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

        }

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

        }

        @Override
        public void onAnimationEnd(Animation arg0) {
            bgImage.startAnimation(zoomin); 

        }
    });

答案 1 :(得分:0)

仅在动画xml中使用:

 android:repeatMode="restart"
    android:repeatCount="infinite"

答案 2 :(得分:0)

最简单的方法是:

continuous_zoom_out_zoom_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:repeatMode="reverse"
    android:shareInterpolator="true">

    <scale
        android:duration="500"
        android:fillAfter="true"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:startOffset="0"
        android:toXScale="0.8"
        android:toYScale="0.8" />

    <scale
        android:duration="500"
        android:fillAfter="true"
        android:fromXScale="0.8"
        android:fromYScale="0.8"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:startOffset="1000"
        android:toXScale="1"
        android:toYScale="1" />
</set>

简单地使用

imageView.loadAnimation(AnimationUtils.loadAnimation(root.context, R.anim.continuous_zoom_out_zoom_in))

不需要额外的回调或类似的事情。

它将创建连续放大缩小动画

答案 3 :(得分:-1)

您可以使用下面的内容和Sanket提到的

<强> Zommin.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >
    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5"
        >
    </scale>

</set>

<强> Zoomout.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >
    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromXScale="1.5"
        android:fromYScale="1.5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" >
    </scale>

</set>

代码:

zoomin.setAnimationListener(new Animation.AnimationListener() {

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

            }

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

            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                imageView.startAnimation(zoomout);

            }
        });