通过一张图片不同位置实现动画

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...DemoActivity:

package com.ping;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class DemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        ViewGroup layout = new LinearLayout(this);

  // 这里装载动画资源图片
  Log.d(getClass().getSimpleName(), "装载小雪行走动画资源图片");
  Bitmap xiaoxueWalkSerBitmap = BitmapFactory.decodeResource(this
    .getResources(), R.drawable.xiaoxue);
//  for( int lay = 0; lay<4; lay++ ) {
   layout.addView(new PlayerWalkView(this,xiaoxueWalkSerBitmap, 0),
     PlayerConst.PLAYER_XIAOXUE_WALK_WIDTH,
     PlayerConst.PLAYER_XIAOXUE_WALK_HEIGHT);
//  }//for,小雪行走动画有4层
  
  setContentView(layout);
          
    }
}

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>PlayerWalkView:

package com.ping;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;

public class PlayerWalkView extends View {

 private AnimationDrawable animationDrawable;

 public PlayerWalkView(Context context, Bitmap xiaoxueWalkSerBitmap, int lay) {
  super(context);
  // TODO Auto-generated constructor stub
  animationDrawable = new AnimationDrawable();
  Bitmap[] bitmaps = new Bitmap[PlayerConst.PLAYER_XIAOXUE_WALK_FRAME];
  for (int frame = 0; frame < bitmaps.length; frame++) {
   Bitmap bitmap = Bitmap.createBitmap(xiaoxueWalkSerBitmap,
     frame*PlayerConst.PLAYER_XIAOXUE_WALK_WIDTH,
     lay*PlayerConst.PLAYER_XIAOXUE_WALK_HEIGHT,
     PlayerConst.PLAYER_XIAOXUE_WALK_WIDTH,
     PlayerConst.PLAYER_XIAOXUE_WALK_HEIGHT);
   animationDrawable.addFrame(new BitmapDrawable(bitmap),100);
  }// for,每层有 PLAYER_XIAOXUE_WALK_FRAME 帧
  animationDrawable.setOneShot(false);
  setBackgroundDrawable(animationDrawable);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);
  animationDrawable.start();// 动画开始
 }
}

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>PlayerConst:

public class PlayerConst {

 public static final int PLAYER_XIAOXUE_WALK_FRAME = 8;// 小雪角色行走动作的帧数
 public static final int PLAYER_XIAOXUE_WALK_WIDTH = 120; // 小雪角色行走动作宽度
 public static final int PLAYER_XIAOXUE_WALK_HEIGHT = 150;// 小雪角色行走动作高度
 public static final int PLAYER_XIAOXUE_WALK_DURATION = 50;// 延时50毫秒

}

 

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
</LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值