15

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

放大.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>

缩小.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

4 回答 4

20

用这个代替线程

 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); 

        }
    });
于 2013-08-14T12:09:04.360 回答
2

只需在您的动画 xml 中使用:

 android:repeatMode="restart"
    android:repeatCount="infinite"
于 2018-07-14T09:01:32.950 回答
1

最简单的方法是:

连续_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))

不需要额外的回调或类似的东西。

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

于 2021-05-18T13:22:15.893 回答
-2

您可以使用类似下面的东西,正如 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>

缩小.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);

            }
        });
于 2017-06-18T08:26:19.917 回答