package com.wang;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;public class myGameView extends View {
//定义变量
int changeX=2,changeY=2;
//定义房屋的图片
Bitmap bmpHouse;
//定义精灵的图片
Bitmap bmpSprit; //人物当前坐标
int intSprintX=100;
int intSprintY=250;
//制定上下文
Context myGameViewContext;
//定义精灵的帧数
int intLeftAnimIndex = 0;
//定义魔法的帧数
int intMagicIndex=0;
//定义向左走的数组
Bitmap[] LeftArray = new Bitmap[4];
//定义向左走的数组
Bitmap[] RightArray = new Bitmap[4];
//定义向左走的数组
Bitmap[] UpArray = new Bitmap[4];
//定义向左走的数组
Bitmap[] DownArray = new Bitmap[4];
//定义魔法数组
Bitmap[] MagicArray=new Bitmap[15];
//是否需要放魔法
Boolean boolMagic=false;

//默认走步方向,1为左 2为右 3为上 4为下
int Direction=1;
static final int DirectionLeft = 1;
static final int DirectionRight = 2;
static final int DirectionUP = 3;
static final int DirectionDown = 4;
@Override
protected void onDraw(Canvas canvas) {

canvas.drawColor(Color.WHITE);
Paint myPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
//绘制天龙寺地图
canvas.drawBitmap(bmpHouse, 0,0, myPaint);

Thread MoveThread = new Thread(new MoveHandler());
MoveThread.start();
//绘制精灵
switch(Direction)
{
case DirectionDown:
canvas.drawBitmap(DownArray[intLeftAnimIndex], intSprintX,intSprintY, myPaint);
break;
case DirectionUP:
canvas.drawBitmap(UpArray[intLeftAnimIndex], intSprintX,intSprintY, myPaint);
break;
case DirectionRight:
canvas.drawBitmap(RightArray[intLeftAnimIndex], intSprintX,intSprintY, myPaint);
break;
case DirectionLeft:
canvas.drawBitmap(LeftArray[intLeftAnimIndex], intSprintX,intSprintY, myPaint);
break;
}
//绘制魔法
if(boolMagic)
{
canvas.drawBitmap(MagicArray[intMagicIndex], intSprintX-5,intSprintY, myPaint);
canvas.drawBitmap(MagicArray[intMagicIndex], intSprintX+15,intSprintY, myPaint);
canvas.drawBitmap(MagicArray[intMagicIndex], intSprintX-25,intSprintY, myPaint);
canvas.drawBitmap(MagicArray[intMagicIndex], intSprintX,intSprintY+5, myPaint);
canvas.drawBitmap(MagicArray[intMagicIndex], intSprintX-25,intSprintY-5, myPaint);
canvas.drawBitmap(MagicArray[intMagicIndex], intSprintX-5,intSprintY+8, myPaint);
}
super.onDraw(canvas);
}

public myGameView(Context context) {
super(context);
DataLoad(context);
}
private void DataLoad(Context context) {
//载入房屋
bmpHouse = BitmapFactory.decodeResource(context.getResources(), R.drawable.house2);
//载入精灵
bmpSprit = BitmapFactory.decodeResource(context.getResources(), R.drawable.sprit1);
//载入精灵
//bmpControl = BitmapFactory.decodeResource(context.getResources(), R.drawable.joystick);
myGameViewContext=context;
//载入动画数组
LeftArray[0]=BitmapFactory.decodeResource(context.getResources(), R.drawable.left1);
LeftArray[1]=BitmapFactory.decodeResource(context.getResources(), R.drawable.left2);
LeftArray[2]=BitmapFactory.decodeResource(context.getResources(), R.drawable.left3);
LeftArray[3]=BitmapFactory.decodeResource(context.getResources(), R.drawable.left4);

RightArray[0]=BitmapFactory.decodeResource(context.getResources(), R.drawable.right1);
RightArray[1]=BitmapFactory.decodeResource(context.getResources(), R.drawable.right2);
RightArray[2]=BitmapFactory.decodeResource(context.getResources(), R.drawable.right3);
RightArray[3]=BitmapFactory.decodeResource(context.getResources(), R.drawable.right4);

UpArray[0]=BitmapFactory.decodeResource(context.getResources(), R.drawable.up1);
UpArray[1]=BitmapFactory.decodeResource(context.getResources(), R.drawable.up2);
UpArray[2]=BitmapFactory.decodeResource(context.getResources(), R.drawable.up3);
UpArray[3]=BitmapFactory.decodeResource(context.getResources(), R.drawable.up4);

DownArray[0]=BitmapFactory.decodeResource(context.getResources(), R.drawable.down1);
DownArray[1]=BitmapFactory.decodeResource(context.getResources(), R.drawable.down2);
DownArray[2]=BitmapFactory.decodeResource(context.getResources(), R.drawable.down3);
DownArray[3]=BitmapFactory.decodeResource(context.getResources(), R.drawable.down4);
//载入魔法
MagicArray[0]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic1);
MagicArray[1]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic2);
MagicArray[2]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic3);
MagicArray[3]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic4);
MagicArray[4]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic5);
MagicArray[5]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic6);
MagicArray[6]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic7);
MagicArray[7]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic8);
MagicArray[8]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic9);
MagicArray[9]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic10);
MagicArray[10]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic11);
MagicArray[11]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic12);
MagicArray[12]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic13);
MagicArray[13]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic14);
MagicArray[14]=BitmapFactory.decodeResource(context.getResources(), R.drawable.magic15);
} class MaigcHandler implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<14;i++){
try{
if(intMagicIndex<14){
intMagicIndex++;
}else{
intMagicIndex = 0;
}
Thread.sleep(50);
}catch(Exception e){
e.printStackTrace();
}
postInvalidate();
}
boolMagic = false;
}

}
class MoveHandler implements Runnable{ @Override
public void run() {
// TODO Auto-generated method stub

try{
if(intSprintX<0){
//改变精灵的方向
Direction=DirectionRight;
if(intLeftAnimIndex<3){

intLeftAnimIndex++;
}else{
intLeftAnimIndex=0;
}

changeX=0-changeX;
//魔法攻击生效
boolMagic=true;
Thread MagicThread = new Thread(new MaigcHandler());
MagicThread.start();
}
else if(intSprintX>290){
//改变精灵的方向
Direction=DirectionLeft;
if(intLeftAnimIndex<3){
intLeftAnimIndex++;
}else{
intLeftAnimIndex=0;
}
changeX=0-changeX;
//魔法攻击生效
boolMagic=true;
Thread MagicThread = new Thread(new MaigcHandler());
MagicThread.start();
}
else if(intSprintY<0){
changeY=0-changeY;
//魔法攻击生效
boolMagic=true;
Thread MagicThread = new Thread(new MaigcHandler());
MagicThread.start();
}else if(intSprintY>425){

changeY=0-changeY;
//魔法攻击生效
boolMagic=true;
Thread MagicThread = new Thread(new MaigcHandler());
MagicThread.start();
}
intSprintX+=changeX;
intSprintY+=changeY;
if(changeX>0){
//改变精灵的方向
Direction=DirectionRight;
if(intLeftAnimIndex<3){
intLeftAnimIndex++;
}else{
intLeftAnimIndex=0;
}
System.out.println("Down:   "+intLeftAnimIndex);
}else{
//改变精灵的方向
Direction=DirectionLeft;
if(intLeftAnimIndex<3){
intLeftAnimIndex++;
}else{
intLeftAnimIndex=0;
}
System.out.println("Up:   "+intLeftAnimIndex);
}


Thread.sleep(100);
}
catch(Exception e){
e.printStackTrace();
}
postInvalidate();
}

}
}
代码实现的功能是 一个小人在屏幕上跑,碰壁后释放魔法,然后返回,我现在想写四个小人,在屏幕上碰撞,碰壁后弹回,释放魔法,四个小人运行中可以重合  谢谢