zoukankan      html  css  js  c++  java
  • Android 杂谈---帧动画

    Android中的动画有

    大体思路

    1.需要定义存放每一帧的xml文件,放在drawable文件夹下

    设置图片路径和duration,以及shot属性,false--->只播放一次,true----->播放多次

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <animation-list android:oneshot="false"
     3 
     4   xmlns:android="http://schemas.android.com/apk/res/android">
     5     <item android:duration="50" android:drawable="@drawable/progressbar34" />
     6     <item android:duration="50" android:drawable="@drawable/progressbar33" />
     7     <item android:duration="50" android:drawable="@drawable/progressbar32" />
     8     <item android:duration="50" android:drawable="@drawable/progressbar31" />
     9     <item android:duration="50" android:drawable="@drawable/progressbar30" />
    10     <item android:duration="50" android:drawable="@drawable/progressbar29" />
    11     <item android:duration="50" android:drawable="@drawable/progressbar28" />
    12     <item android:duration="50" android:drawable="@drawable/progressbar27" />
    13     <item android:duration="50" android:drawable="@drawable/progressbar26" />
    14     <item android:duration="50" android:drawable="@drawable/progressbar25" />
    15     <item android:duration="50" android:drawable="@drawable/progressbar24" />
    16     <item android:duration="50" android:drawable="@drawable/progressbar23" />
    17     <item android:duration="50" android:drawable="@drawable/progressbar22" />
    18     <item android:duration="50" android:drawable="@drawable/progressbar21" />
    19     <item android:duration="50" android:drawable="@drawable/progressbar20" />
    20     <item android:duration="50" android:drawable="@drawable/progressbar19" />
    21     <item android:duration="50" android:drawable="@drawable/progressbar18" />
    22     <item android:duration="50" android:drawable="@drawable/progressbar17" />
    23     <item android:duration="50" android:drawable="@drawable/progressbar16" />
    24     <item android:duration="50" android:drawable="@drawable/progressbar15" />
    25     <item android:duration="50" android:drawable="@drawable/progressbar14" />
    26     <item android:duration="50" android:drawable="@drawable/progressbar13" />
    27     <item android:duration="50" android:drawable="@drawable/progressbar12" />
    28     <item android:duration="50" android:drawable="@drawable/progressbar11" />
    29     <item android:duration="50" android:drawable="@drawable/progressbar10" />
    30     <item android:duration="50" android:drawable="@drawable/progressbar9" />
    31     <item android:duration="50" android:drawable="@drawable/progressbar8" />
    32     <item android:duration="50" android:drawable="@drawable/progressbar7" />
    33     <item android:duration="50" android:drawable="@drawable/progressbar6" />
    34     <item android:duration="50" android:drawable="@drawable/progressbar5" />
    35     <item android:duration="50" android:drawable="@drawable/progressbar4" />
    36     <item android:duration="50" android:drawable="@drawable/progressbar3" />
    37     <item android:duration="50" android:drawable="@drawable/progressbar2" />
    38     <item android:duration="50" android:drawable="@drawable/progressbar1" />
    39 </animation-list>
    progress.xml

    2.指定存放该动画的容器,使用imageView来充当

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     tools:context="com.jing.www.loginlightdemo.MainActivity">
     7     <ImageView
     8         android:id="@+id/animation_iv"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:layout_gravity="center"
    12         android:layout_margin="10dp"
    13         android:src="@drawable/progress" />
    14 
    15 </android.support.constraint.ConstraintLayout>
    activity_main.xml

    3.使用代码来播放帧动画

    注意:不能再onCreate()方法中进行动画的播放,动画依附于window,在onCreate()时期,window还没有初始化完成

    会使动画没有运行而是停留在第一帧,那是因为AnimationDrawable播放动画是依附在window上面,而在Activity onCreate方法中调用时Window还未初始化完毕,所有才会停留在第一帧,要想实现播放必须在onWindowFocusChanged中添加如下代码:

     1 public class MainActivity extends AppCompatActivity {
     2 
     3     private ImageView imageView;
     4 
     5     @Override
     6     protected void onCreate(Bundle savedInstanceState) {
     7         super.onCreate(savedInstanceState);
     8         setContentView(R.layout.activity_main);
     9         imageView = (ImageView) findViewById(R.id.animation_iv);
    10 
    11     }
    12 
    13     @Override
    14     public void onWindowFocusChanged(boolean hasFocus) {
    15         super.onWindowFocusChanged(hasFocus);
    16         imageView.setImageResource(R.drawable.progress);
    17         AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
    18         animationDrawable.start();
    19 
    20     }
    21 }
    MainActivity.java
  • 相关阅读:
    cocos2d-x之物理引擎初试
    cocos2d-x之猜数字游戏
    cocos2d-x之加法计算器
    cocos2d-x之悦动的小球
    cocos2d-x之多个移动的小球
    cocos2d-x之json文件读取初试
    cocos2d-x之xml文件读取初试
    cocos2d-x之使用plist文件初试
    cocos2d-x之文件读写
    cocos2d-x之首选项数据初试
  • 原文地址:https://www.cnblogs.com/fanfusuzi/p/6957210.html
Copyright © 2011-2022 走看看