码迷,mamicode.com
首页 > 移动开发 > 详细

Android 学习之逐帧动画(Frame)

时间:2017-05-11 12:55:55      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:利用   div   imageview   one   html   通过   技术分享   .com   content   

帧动画就是将一些列图片。依次播放。

利用肉眼的“视觉暂留”的原理,给用户的感觉是动画的错觉,逐帧动画的原理和早期的电影原理是一样的。

a:须要定义逐帧动画,能够通过代码定义。也能够通过XML文件定义。一般XML文件定义比較直观

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> <!--false是循环播放  -->
    <!-- drawables是每一张图片。 duration是图片持续的时间 -->
    <item android:drawable="@drawable/g1" android:duration="200" />
    <item android:drawable="@drawable/g2" android:duration="200" />
    <item android:drawable="@drawable/g3" android:duration="200" />
    <item android:drawable="@drawable/g4" android:duration="200" />
    <item android:drawable="@drawable/g5" android:duration="200" />
    <item android:drawable="@drawable/g6" android:duration="200" />
    <item android:drawable="@drawable/g7" android:duration="200" />
    <item android:drawable="@drawable/g8" android:duration="200" />
    <item android:drawable="@drawable/g9" android:duration="200" />
    <item android:drawable="@drawable/g10" android:duration="200" />
    <item android:drawable="@drawable/g11" android:duration="200" />
</animation-list>

当中oneshot代表的是否循环播放,false是循环播放,true是仅仅播放一次

b:将上述的XMLd定义的资源,设置为ImageView的背景

        //找到imageview
        ImageView iv = (ImageView) findViewById(R.id.iv);
        //将帧动画的资源文件设置为imageview的背景
        iv.setBackgroundResource(R.drawable.frameanimation);

c:获得AnimationDrawable对象

        //获取AnimationDrawable对象
        AnimationDrawable ad = (AnimationDrawable) iv.getBackground();

d:開始播放动画就ok

    //開始播放动画
        ad.start();

Activity整个代码:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //找到imageview
        ImageView iv = (ImageView) findViewById(R.id.iv);
        //将帧动画的资源文件设置为imageview的背景
        iv.setBackgroundResource(R.drawable.frameanimation);
        //获取AnimationDrawable对象
        AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
        //開始播放动画
        ad.start();
    }
}

演示效果:

技术分享

Android 学习之逐帧动画(Frame)

标签:利用   div   imageview   one   html   通过   技术分享   .com   content   

原文地址:http://www.cnblogs.com/mthoutai/p/6840329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!