【问题标题】:Android ImageView Zoom-in and Zoom-out ContinuouslyAndroid ImageView 不断放大缩小
【发布时间】:2013-08-14 12:05:17
【问题描述】:

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

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 动画似乎运行良好。有没有办法连续实现zoomin和zoomout动画???

谢谢

【问题讨论】:

    标签: android animation imageview


    【解决方案1】:

    用这个代替线程

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

    【讨论】:

    • 太棒了!非常感谢:)) (Y)
    • 我建议在 zoomin 和 zoomout 中添加 android:repeatCount="1" 和 android:repeatMode="reverse" 来缩放参数,效果非常好:)
    • 无需添加动画监听,一个xml就可以搞定,见下方答案。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      最简单的方法是:

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

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

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

      【讨论】:

        【解决方案4】:

        你可以使用像下面和 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);
        
                    }
                });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-07-18
          • 2011-08-12
          • 2017-05-28
          • 2015-01-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多