zoukankan      html  css  js  c++  java
  • 动画的使用—Drawable Animation

    Drawable Animation可以称为帧动画,因为它是通过每次播放一帧Drawable资源实现的。

    Drawable Animation算不上真正意义上的动画,因为它的内部实现是通过定时发送消息更新一个Drawable,

    例如一个背景。所以使用这个动画的时候更像是使用一个背景资源,只不过更新背景的动作不用我们自己进行。

    也许正是因为这个原因,android官方建议我们将这个动画资源放在drawable目录。

    使用帧动画非常之简单,只需要在drawable目录定义个xml文件,

    使用animation-list标签包裹所有组成这个动画的图片文件,设置播放速率。

    然后在java代码中使用。

    定义xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
        <!--每一个 item 中包括一帧组成动画的图片 -->
        <item android:drawable="@drawable/a01" android:duration="80" />
        <item android:drawable="@drawable/a02" android:duration="80" />
        <item android:drawable="@drawable/a03" android:duration="80" />
        <item android:drawable="@drawable/a04" android:duration="80" />
        <item android:drawable="@drawable/a05" android:duration="80" />
        <item android:drawable="@drawable/a06" android:duration="80" />
        <item android:drawable="@drawable/a07" android:duration="80" />
        <item android:drawable="@drawable/a08" android:duration="80" />
        <item android:drawable="@drawable/a09" android:duration="80" />
        <item android:drawable="@drawable/a10" android:duration="80" />
        <item android:drawable="@drawable/a11" android:duration="80" />
        <item android:drawable="@drawable/a12" android:duration="80" />
        <item android:drawable="@drawable/a13" android:duration="80" />
    </animation-list>

    这样就定义好了我们的动画资源,之后就是在activity中调用

    package com.whathecode.drawableanimation;
    
    import android.graphics.drawable.AnimationDrawable;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.MotionEvent;
    import android.widget.ImageView;
    
    public class MainActivity extends ActionBarActivity {
    
        private AnimationDrawable ad;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            /**
             * 主布局文件实际上只是一个相对布局和一个ImageView
             */
            setContentView(R.layout.activity_main);
            
            ImageView img = (ImageView) findViewById(R.id.ali);
            
            /**
             * 获取动画资源,因为他就是一个背景资源
             * 所以可以使用getBackground获取,然后强制转换成AnimationDrawable
             * 也可以这样获取动画资源
             * ad = (AnimationDrawable) getResources().getDrawable(R.drawable.motion);
             * img.setBackgroundDrawable(ad);
             */
             img.setBackgroundResource(R.drawable.motion);
             ad = (AnimationDrawable)img.getBackground();
        }
        
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch(event.getAction())
            {
            case MotionEvent.ACTION_UP:
                //播放动画
                ad.start();
                return true;
            }
            return super.onTouchEvent(event);
        }
    }

    这些,就是AnimationDrawable的全部

    AnimationDrawable

  • 相关阅读:
    删除ubuntu自带软件 及 WPS 安装(转)WPS字体已备份
    youtube-dl 下载爱奇异视频 不能删除
    電影嘗試三 webtorrent-desktop在線觀看電影magnet
    wget 命令
    什么是X window
    查看磁盘分区
    root
    错误:org.hibernate.exception.GenericJDBCException: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA ....
    Git 版本回退
    oracle密码过期问题解决
  • 原文地址:https://www.cnblogs.com/ai-developers/p/4380557.html
Copyright © 2011-2022 走看看