import android.content.Context;
import android.content.Intent;
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.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
//定义走路的方向
public class RpgView extends View implements Runnable
{
// 定义房屋的图片
Bitmap bmpHouse;
// 定义精灵的图片
Bitmap bmpSprit;
// 定义虚拟的手柄
Bitmap bmpControl;
// 指定虚拟手柄的位置
Rect rKeyup = new Rect(101, 368, 111, 384);
Rect rKeyDown = new Rect(98, 394, 116, 410);
Rect rKeyLeft = new Rect(86, 382, 100, 397);
Rect rKeyRight = new Rect(113, 384, 125, 395);
Rect rKeyAttack = new Rect(196, 392, 213, 410);
Rect rKeyDefine = new Rect(220, 390, 239, 409);
// 人物当前坐标
int intSprintX = 100;
int intSprintY = 250;
// 指定上下文
Context RpgViewContext;
// 定义精灵的帧数
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);
// 绘制人物
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);
}
// 绘制手柄,让手柄具中
canvas.drawBitmap(bmpControl,
(bmpHouse.getWidth() - bmpControl.getWidth()) / 3, 340, myPaint);
//线程,当前线程运行一段时间后出错,没有方法解决
//new Thread(this).start();
super.onDraw(canvas);
}
public RpgView(Context context)
{
super(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);
// 得到手机屏幕大小
// DisplayMetrics是一个描述普通显示信息的结构,
// 例如显示大小、密度、字体大小
DisplayMetrics displaysMetrics = new DisplayMetrics();
// 获取手机窗口的Display来初始化DisplayMetrics对象
RpgViewContext = 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);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
int ax = (int) event.getX();
int ay = (int) event.getY();
/**
* Toast.makeText(getContext(), "您点击的坐标是X:"+ax+"|"+"Y:"+ay,
* Toast.LENGTH_SHORT).show();
*/
if (rKeyup.contains(ax, ay))
{
//
// Toast.makeText(getContext(), "点击向上成功",
// Toast.LENGTH_SHORT).show();
//向上移到5个像素
intSprintY -= 5;
//指定方向
Direction=DirectionUp;
// 定义帧数索引
if (intLeftAnimIndex < 3)
{
intLeftAnimIndex++;
} else
{
intLeftAnimIndex = 0;
}
}
if (rKeyDown.contains(ax, ay))
{
// Toast.makeText(getContext(), "点击向下成功", Toast.LENGTH_SHORT)
// .show();
intSprintY += 5;
Direction=DirectionDown;
// 定义帧数索引
if (intLeftAnimIndex < 3)
{
intLeftAnimIndex++;
} else
{
intLeftAnimIndex = 0;
}
}
if (rKeyLeft.contains(ax, ay))
{
// Toast.makeText(getContext(), "点击向左成功", Toast.LENGTH_SHORT)
// .show();
// 向左移动10个像素
intSprintX -= 5;
Direction=DirectionLeft;
// 定义帧数索引
if (intLeftAnimIndex < 3)
{
intLeftAnimIndex++;
} else
{
intLeftAnimIndex = 0;
}
}
if (rKeyRight.contains(ax, ay))
{
// Toast.makeText(getContext(), "点击向右成功", Toast.LENGTH_SHORT)
// .show();
intSprintX += 5;
Direction=DirectionRight;
// 定义帧数索引
if (intLeftAnimIndex < 3)
{
intLeftAnimIndex++;
} else
{
intLeftAnimIndex = 0;
}
}
if (rKeyAttack.contains(ax, ay))
{
//让魔法攻击生效
boolMagic=true;
Thread MagicThread = new Thread(new MaigcHandler());
MagicThread.start();
Toast.makeText(getContext(), "魔法攻击", Toast.LENGTH_SHORT)
.show();
}
if (rKeyDefine.contains(ax, ay))
{
Toast.makeText(getContext(), "点击防御成功", Toast.LENGTH_SHORT)
.show();
}
postInvalidate();// 刷新屏幕
}
return super.onTouchEvent(event);
}
class MaigcHandler implements Runnable
{
@Override
public void run()
{
for(int i=0;i<14;i++)
{
try
{
// 定义帧数索引
if (intMagicIndex < 14)
{
intMagicIndex++;
} else
{
intMagicIndex = 0;
}
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
postInvalidate();
}
//魔法攻击失效
boolMagic=false;
}
}
@Override
public void run()
{
while (!Thread.currentThread().isInterrupted())
{
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
Thread.currentThread().interrupt();
}
// 使用postInvalidate可以直接在线程中更新界面
postInvalidate();
}
}
联系客服