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

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>...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


    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

 

你可能感兴趣的:(通过一张图片不同位置实现动画)