zoukankan      html  css  js  c++  java
  • android之frame动画详解

    上一篇我们说了android中的tween动画,这一篇我们说说frame动画,frame动画主要是实现了一种类似于gif动画的效果,就是多张图按预先设定好的时间依次连续显示。
    新建一个android项目,名字叫做frameTest,在res文件夹下新建一个文件夹叫做anim,我们的frame动画的xml文件就放在这里。
    在anim中新建一个frame.xml文件,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
        android:oneshot="false"><!-- true表示只播放一次,false表示无限循环播放 -->
        <item android:drawable="@drawable/girl_1" android:duration="100" />
        <item android:drawable="@drawable/girl_2" android:duration="100" />
        <item android:drawable="@drawable/girl_3" android:duration="100" />
        <item android:drawable="@drawable/girl_4" android:duration="100" />
        <item android:drawable="@drawable/girl_5" android:duration="100" />
        <item android:drawable="@drawable/girl_6" android:duration="300" />
        <item android:drawable="@drawable/girl_7" android:duration="400" />
        <item android:drawable="@drawable/girl_8" android:duration="300" />
        <item android:drawable="@drawable/girl_9" android:duration="100" />
        <item android:drawable="@drawable/girl_10" android:duration="100" />
        <item android:drawable="@drawable/girl_11" android:duration="100" />
    </animation-list>

    这里是11图片,前面的android:oneshot属性表示该动画执行的次数,false表示该动画反复循环播放,true则表示该动画值播放一次,duration表示每张图片显示的时间,以毫秒计。

    然后看看MainActivity中的代码:

    public class MainActivity extends Activity {
    
        private ImageView iv;
        private AnimationDrawable ad;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv = (ImageView) this.findViewById(R.id.iv);
    //      iv.setBackgroundResource(R.anim.frame);
    //      ad = (AnimationDrawable) iv.getBackground();
            //上面两句,可以用下面两句代替,效果是一样的
            iv.setImageResource(R.anim.frame);
            ad = (AnimationDrawable) iv.getDrawable();
        }
        public void start(View v){
            //如果ad正在运行,就先让它停止下来
            if(ad.isRunning())
                ad.stop();
            ad.start();
        }
    }

    先拿到一个ImageView,然后把frame动画设置为它的背景,最后拿到这个图片的背景并强转为AnimationDrawable,当点击该图片时,如果动画已经在运行,就让它先停止,再重新运行,否则直接运行即可。

  • 相关阅读:
    VS 2008潜在强大的功能:提取EXE文件中的ICO等资源
    园友们注意:淘宝网上QQ会员 4钻 3元 等都为骗子行为
    Comet Async Process Request Handler
    WCF(Sender) to MSMQ to WCF(Receiver)
    ASP.NET Web Form GridView DetailsView Query Edit
    WCF NetTcp AsyncQueue Service
    Xml CDATA 序列化
    Sync Invoke Remoting Async Invoke
    .Net 4.0 Remoting ConcurrentQueue
    Socket Async Receive Data to LinkedList Buffer (telnet proxy server)
  • 原文地址:https://www.cnblogs.com/lenve/p/4517980.html
Copyright © 2011-2022 走看看