zoukankan      html  css  js  c++  java
  • android Frame动画animationlist

       大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它。它的使用更加简单,只需要创建一个AnimationDrawabledF对象来表示Frame动画,然后通过addFrame 方法把每一帧要显示的内容添加进去,最后通过start 方法就可以播放这个动画了,同时还可以通过 setOneShot方法设置是否重复播放。 下面就是一个用Frame动画模拟日全食的效果。先看看效果。 

     

     

     

     

    Activity01 

    Java代码 
    package xiaohang.zhimeng;  

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.Window;
    import android.view.WindowManager;

    public class Activity01 extends Activity {
    private GameView mGameView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置无标题栏
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // 设置为全屏模式
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mGameView = new GameView(this);

    setContentView(mGameView);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (mGameView == null) {
    return false;
    }
    mGameView.onKeyUp(keyCode, event);
    return true;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (mGameView == null) {
    return false;
    }
    if (keyCode == KeyEvent.KEYCODE_BACK) {
    // 关闭Activity
    this.finish();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }
    }


    GameView 

    Java代码 
    package xiaohang.zhimeng;  

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.drawable.AnimationDrawable;
    import android.graphics.drawable.Drawable;
    import android.view.KeyEvent;
    import android.view.View;

    public class GameView extends View{

    //定义AnimationDrawable动画
    private AnimationDrawable frameAnimation = null;
    Context mContext = null;

    //定义一个Drawable对象
    Drawable mBitAnimation = null;

    public GameView(Context context) {
    super(context);

    mContext = context;

    //实例化AnimationDrawable对象
    frameAnimation = new AnimationDrawable();

    /*装载资源*/
    //这里用一个循环装载所有名字类似的资源
    //如"a1...........15.png"的图片
    for(int i = 1; i <= 15; i++){
    int id = getResources().getIdentifier("a" + i, "drawable", mContext.getPackageName());
    //此方法返回一个可绘制的对象与特定的资源ID相关联
    mBitAnimation = getResources().getDrawable(id);
    /*为动画添加一帧*/
    //参数mBitAnimation是该帧的图片
    //参数500是该帧显示的时间,按毫秒计算
    frameAnimation.addFrame(mBitAnimation, 500);
    }
    /*上边用到了Resources的getIdentifier方法 方法返回一个资源的唯一标识符,如果没有这个资源就返回0
    * 0不是有效的标识符,在说说这个方法几个参数的含义
    * 第一个 就是我们的资源名称了。
    * 第二个 就是我们要去哪里找我们的资源 我们的图片在drawable 下 所以为drawable
    * 第三个 我们用了Context的getPackageName返回应用程序的包名
    *
    */

    //设置播放模式是否循环播放,false表示循环,true表示不循环
    frameAnimation.setOneShot(false);

    //设置本类将要显示的这个动画
    this.setBackgroundDrawable( frameAnimation );
    }

    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    }

    public boolean onKeyUp(int keyCode, KeyEvent event){
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_UP:
    //当按手机的上方向键时开始播放
    frameAnimation.start();
    break;
    }
    return true;
    }
    }

       
      同样Frame 也可以用xml文件实现,大家去看 api吧。

    1、animation-list配置,预先将一个动画按照每帧分解成的多个图片所组成的序列。然后再在Android的配置文件中将这些图片配置到动画里面。

    Xml代码 
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
    android:oneshot
    ="false">
    <item android:drawable="@drawable/explode1" android:duration="50" />
    <item android:drawable="@drawable/explode2" android:duration="50" />
    <item android:drawable="@drawable/explode3" android:duration="50" />
    <item android:drawable="@drawable/explode4" android:duration="50" />
    </animation-list>

    但是由此带来的不便也是显而易见的:drawable目录下拥挤了过多的动画帧文件。如果游戏大起来,动画效果丰富,那么drawable目录下将拥有数量庞大的图片文件,这将是开发人员的灾难(见下图)。



    转自:http://byandby.iteye.com/blog/833037


  • 相关阅读:
    聚合支付里各扫码支付的返回报文样例
    短信平台接口安全控制
    「美团外卖APP签约快捷支付」流程体验
    多模块项目提示“Module ** must not contain source root **. The root already belongs to module **”的解决办法
    比较两种方式的form请求提交
    Linux screen命令和系统日志
    Linux 守护进程
    Linux 进程的通信方式与信号:kill命令
    Linux 进程的控制与进程之间的关系
    Linux 使用ps和top命令查看进程
  • 原文地址:https://www.cnblogs.com/shanzei/p/2413284.html
Copyright © 2011-2022 走看看