package handshank.demo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.view.MotionEvent;
/**
* @author Administrator
* @version 创建时间:2011-6-27 下午03:38:11 类说明:手柄类
*/
public class HandShank {
public static final int JOYSTICK_UP = 0;
public static final int JOYSTICK_DOWN = 1;
public static final int JOYSTICK_LEFT = 2;
public static final int JOYSTICK_RIGHT = 3;
public static final int JOYSTICK_SHOOT = 4;
public static final int JOYSTICK_JUMP = 5;
/**
* 最大点击触屏个数
*/
public static final int MAX_TOUCHPOINTS = 4;
Bitmap joystick_roundImg = null; // 摇杆圆
Bitmap[] joystick_upImg = null; // 上
Bitmap[] joystick_downImg = null;// 下
Bitmap[] joystick_leftImg = null; // 左
Bitmap[] joystick_rightImg = null; // 右
Bitmap[] joystick_shootImg = null; // 攻击
Bitmap[] joystick_jumpImg = null; // 跳
int[] isFocused;// 用来存储模拟按键的按键状态
PointF pc,p1,p2,p3,p4,p5,shootPc,jumpPc;
float roundH,roundW,roundr;
float d;
float shootImgR,jumpImgR;
Matrix matrix;
public HandShank(Context context) {
joystick_roundImg = MyResource.getResImg(context, "joystick_round");
joystick_upImg = new Bitmap[2];
joystick_upImg[0] = MyResource.getResImg(context, "joystick_up");
joystick_upImg[1] = MyResource.getResImg(context, "joystick_upd");
joystick_downImg = new Bitmap[2];
joystick_downImg[0] = MyResource.getResImg(context, "joystick_down");
joystick_downImg[1] = MyResource.getResImg(context, "joystick_downd");
joystick_leftImg = new Bitmap[2];
joystick_leftImg[0] = MyResource.getResImg(context, "joystick_left");
joystick_leftImg[1] = MyResource.getResImg(context, "joystick_leftd");
joystick_rightImg = new Bitmap[2];
joystick_rightImg[0] = MyResource.getResImg(context, "joystick_right");
joystick_rightImg[1] = MyResource.getResImg(context, "joystick_rightd");
joystick_shootImg = new Bitmap[2];
joystick_shootImg[0] = MyResource.getResImg(context, "joystick_shoot");
joystick_shootImg[1] = MyResource.getResImg(context, "joystick_shootd");
joystick_jumpImg = new Bitmap[2];
joystick_jumpImg[0] = MyResource.getResImg(context, "joystick_jump");
joystick_jumpImg[1] = MyResource.getResImg(context, "joystick_jumpd");
isFocused = new int[7];
initData();
}
public void initData() {
roundH = joystick_roundImg.getHeight();
roundW = joystick_roundImg.getWidth();
d = (float) (Math.sqrt(2.0) * roundW / 4f);
roundr = roundW / 2f;
shootImgR = joystick_shootImg[0].getWidth() / 2;
jumpImgR = joystick_jumpImg[0].getWidth() / 2 + 10;
float roundy = MySurfaceView.screenH - roundH / 2f;
pc = new PointF(roundW / 2, roundy);
p1 = new PointF(roundr, roundy - roundr);
p2 = new PointF(0, roundy - roundr);
p3 = new PointF(0, roundy + roundr);
p4 = new PointF(2 * roundr, roundy + roundr);
p5 = new PointF(2 * roundr, roundy - roundr);
shootPc = new PointF(MySurfaceView.screenW - 3 * shootImgR,
MySurfaceView.screenH - shootImgR);
jumpPc = new PointF(MySurfaceView.screenW - 1 * jumpImgR,
MySurfaceView.screenH - jumpImgR - shootImgR);
matrix = new Matrix();
}
public void touch(MotionEvent event) {
int pointerCount = event.getPointerCount();
if (pointerCount > MAX_TOUCHPOINTS) {
pointerCount = MAX_TOUCHPOINTS;
}
for (int i = 0; i < isFocused.length; i++) {
isFocused[i] = 0;
}
for (int i = 0; i < pointerCount; i++) {
float x = event.getX(i);
float y = event.getY(i);
PointF p = new PointF(x, y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (ModelCheck.inTriangle(p, p3, p2, pc)) {// 左
isFocused[JOYSTICK_LEFT] = 1;
} else if (ModelCheck.inTriangle(p, p4, p5, pc)) {// 右
isFocused[JOYSTICK_RIGHT] = 1;
} else if (ModelCheck.inTriangle(p, p2, p5, pc)) {// 上
isFocused[JOYSTICK_UP] = 1;
}else if(ModelCheck.inTriangle(p,p3,p4,pc)){
isFocused[JOYSTICK_DOWN] =1;
}
else if (ModelCheck.pointInCircle(p, shootPc, shootImgR)) {// AT
isFocused[JOYSTICK_SHOOT] = 1;
} else if (ModelCheck.pointInCircle(p, jumpPc, jumpImgR)) {// JUMP
isFocused[JOYSTICK_JUMP] = 1;
}
}
}
}
/**
* 恢复虚拟按键状态
*/
public void reJoystickStatus() {
for (int i = 0; i < isFocused.length; i++) {
isFocused[i] = 0;
}
}
/**
* 绘制手柄
*
* @param canvas
* @param x
* @param y
* @param paint
* 千万要注意:此时绘制的位置数据已经是缩放了的数据了,所以需要返回到原来的数据,
* 因为利用了双缓存,最外层还套了一成缩放,所以需要除掉缩放了得数据
* 放置此处的原因:因为绘图不需要对数据进行大量的修改,逻辑也是没有的,所以放在此处返回最不容易将数据弄错
*/
public void draw(Canvas canvas, Paint paint) {
canvas.save();
canvas.drawBitmap(joystick_roundImg, pc.x - roundr, pc.y - roundr,
paint);
matrix.setTranslate(pc.x - d + 7, pc.y - roundr + 9);
canvas.drawBitmap(joystick_upImg[isFocused[JOYSTICK_UP]], matrix, paint);
matrix.setTranslate(pc.x - d + 7, pc.y + 2);
canvas.drawBitmap(joystick_downImg[isFocused[JOYSTICK_DOWN]], matrix,
paint);
matrix.setTranslate(pc.x - roundr + 9, pc.y - d + 7);
canvas.drawBitmap(joystick_leftImg[isFocused[JOYSTICK_LEFT]], matrix,
paint);
matrix.setTranslate(pc.x + 2, pc.y - d + 7);
canvas.drawBitmap(joystick_rightImg[isFocused[JOYSTICK_RIGHT]], matrix,
paint);
canvas.drawBitmap(joystick_shootImg[isFocused[JOYSTICK_SHOOT]],
shootPc.x - shootImgR, shootPc.y - shootImgR, paint);
canvas.drawBitmap(joystick_jumpImg[isFocused[JOYSTICK_JUMP]], jumpPc.x
- jumpImgR, jumpPc.y - jumpImgR, paint);
canvas.restore();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
游戏手柄代码
共51个文件
class:20个
png:15个
java:6个
4星 · 超过85%的资源 需积分: 32 95 下载量 43 浏览量
2012-03-29
00:02:37
上传
评论 5
收藏 344KB RAR 举报
温馨提示
游戏手柄代码
资源推荐
资源详情
资源评论
收起资源包目录
HandShank.rar (51个子文件)
HandShank
bin
classes.dex 10KB
res
drawable
icon.png 3KB
handshank
demo
R$layout.class 382B
MyResource.class 2KB
R$drawable.class 388B
HandShank.class 7KB
R$attr.class 328B
R.class 465B
Main.class 765B
ModelCheck.class 1KB
MySurfaceView.class 3KB
R$string.class 415B
classes
handshank
demo
R$layout.class 382B
MyResource.class 2KB
R$drawable.class 388B
HandShank.class 5KB
R$attr.class 328B
R.class 465B
Main.class 765B
ModelCheck.class 1KB
MySurfaceView.class 3KB
R$string.class 415B
resources.ap_ 106KB
HandShank.apk 113KB
res
drawable
icon.png 3KB
values
strings.xml 155B
layout
main.xml 382B
assets
joystick_right.png 5KB
joystick_jump.png 7KB
joystick_up.png 8KB
joystick_upd.png 10KB
joystick_jumpd.png 9KB
joystick_left.png 5KB
joystick_rightd.png 8KB
joystick_downd.png 7KB
joystick_down.png 5KB
joystick_shoot.png 9KB
joystick_shootd.png 13KB
joystick_round.png 6KB
joystick_leftd.png 8KB
default.properties 364B
gen
handshank
demo
R.java 636B
src
handshank
demo
MySurfaceView.java 2KB
HandShank.java 7KB
ModelCheck.java 1KB
Main.java 695B
MyResource.java 685B
.project 845B
.classpath 288B
project.properties 360B
AndroidManifest.xml 737B
共 51 条
- 1
资源评论
- 穆康星2015-03-17并不好使啊,初学者,求指导
- cookieyan2014-09-18楼主应该说明一下这个是使用什么语言的啊!我看了是Java,不是我想要的。
ihrthk
- 粉丝: 157
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Surfer,线性函数
- MyBatis 的动态 SQL 是其核心特性之一.txt
- 时代的sdddsddsddsd
- 基于哈希链表的简单人员信息管理系统
- 其他类别JdonFramework开源框架 v5.1 Build20071025-jdonframework-5.1.rar
- 2001~2022年上市公司数字赋能指数.dta
- 2001~2022年上市公司数字赋能指数.xlsx
- 信息办公石大在线财务管理系统(含源码)-shidacaiwu.rar
- 信息办公电信计费系统完整代码-netctossconformity.rar
- matlab实现TD-SCDMA中初始同步捕捉DwPTS下行同步导频时隙的仿真.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功