zoukankan      html  css  js  c++  java
  • frame动画

    package com.example.examples_05_17;

     

    import android.R.drawable;

    import android.R.integer;

    import android.content.Context;

    import android.graphics.Canvas;

    import android.graphics.drawable.AnimationDrawable;

    import android.graphics.drawable.Drawable;

    import android.view.View;

    import android.widget.Toast;

     

    public class GameView extends View {

     

    //定义AnimationDrawable

    public AnimationDrawable frameAnimation=null;

     

    Context mContext;

     

    //定义一个Drawable对象

    Drawable mBitAnimation=null;

    public GameView(Context context) {

    super(context);

    // TODO Auto-generated constructor stub

    mContext=context;

    //创建实例

    frameAnimation=new AnimationDrawable();

     

    //装载资源

    /*

     * 这里用一个for循环装载所有名字类型的资源

     * "a1......a15.png"的图片

     *  这个方法的用处很大

     */

    for (int i = 1; i <= 15; i++) {

    int id=getResources().getIdentifier("a"+i, "drawable", mContext.getPackageName());

    //Toast.makeText(mContext, id+"", Toast.LENGTH_SHORT).show();

    mBitAnimation=getResources().getDrawable(id);

    //为动画添加一帧

    //mBitAnimation该帧的图片

    //参数500是该帧的显示时间,以毫秒算

    frameAnimation.addFrame(mBitAnimation, 500);

    }

    //这里播放是否循环,true为循环,false为不循环

    frameAnimation.setOneShot(true);

    //设置本类将要显示这个动画

    this.setBackgroundDrawable(frameAnimation);

    }

    }

     

     

    package com.example.examples_05_17;

     

    import android.os.Bundle;

    import android.app.Activity;

    import android.view.KeyEvent;

     

    public class MainActivity extends Activity {

     

    GameView gameView=null;

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            gameView=new GameView(this);

            setContentView(gameView);

        }

        public boolean onKeyDown(int keyCode,KeyEvent event) {

    if(keyCode==KeyEvent.KEYCODE_DPAD_UP)

    {

    //开启播放

    gameView.frameAnimation.start();

    }

    return true;

    }

    }

  • 相关阅读:
    在react-native中使用es7语法中的decorator装饰器
    chrome从版本55开始,不再支持设置网页内容编码
    react-native 获取组件的宽度和高度
    手动安装Android Support Library(23.0.1)
    ubuntu 14.04 git clone 出现 fatal: Unable to find remote helper for 'https'
    React-Native 在android写不支持gif的解决方案!
    游戏大世界场景(胡诌篇)
    多线程先判断再操作免锁设计方案
    Mysql语句优化
    JVM GC Q&A(补充ing)
  • 原文地址:https://www.cnblogs.com/danmao/p/3808649.html
Copyright © 2011-2022 走看看