android模拟摇杆绘制
2014-11-12 22:34
204 查看
[java] view
plaincopyprint?
package com.rp;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
public class Rocker {
double degreesByNormalSystem = Double.NaN;//一般坐标系下的角度
double rad = Double.NaN;//当前遥感的弧度
int rockerColor = Color.GREEN;
//定义两个圆形的中心点坐标与半径
private static final float rockerCenterXMarginRight4ScreenWidthPercent = 0.05f;//摇杆右边界宽度相对于屏幕宽度百分比
private static final float rockerCenterYMarginBottom4ScreenHeightPercent = 0.05f;//摇杆右边界高度相对于屏幕百分比
private static final float rockerR4ScreenWidthPercent = 0.1f;
private float smallCenterX = 120, smallCenterY = 120, smallCenterR = 20;
private float BigCenterX = 120, BigCenterY = 120, BigCenterR = 40;
Paint paint;
/*
* 因为要考虑适应屏幕,所以需要将屏幕宽高放进来
* */
public Rocker(int screenWidth,int screenHeight) {
super();
paint = new Paint();
paint.setAntiAlias(true);
BigCenterR = screenWidth*rockerR4ScreenWidthPercent;
BigCenterX = smallCenterX = screenWidth - BigCenterR*1.5f - rockerCenterXMarginRight4ScreenWidthPercent*screenWidth;
BigCenterY = smallCenterY = screenHeight - BigCenterR*1.5f - rockerCenterYMarginBottom4ScreenHeightPercent*screenHeight;
smallCenterR = BigCenterR/2;
}
void draw(Canvas canvas){
paint.setColor(rockerColor);
paint.setAlpha(0x77);
canvas.drawCircle(BigCenterX, BigCenterY, BigCenterR, paint);
canvas.drawCircle(smallCenterX, smallCenterY, smallCenterR, paint);
paint.setColor(Color.BLACK);
canvas.drawText("原点在左上坐标系下的弧度:"+rad, 20, 20, paint);
canvas.drawText("由该弧度计算得出角度:"+(rad*180/Math.PI), 20, 40, paint);
canvas.drawText("原点在左下坐标系角度:"+degreesByNormalSystem, 20, 60, paint);
}
float getDegrees(float firstX,float firstY,float secondX,float secondY ){
float ret = (float)Math.atan((firstY-secondY)/(firstX-secondX))*180/(float)Math.PI;
if(firstX<secondX){
ret += 180;
}else {
ret += 360;
}
ret = ret >= 360 ? ret - 360: ret;
return ret;
}
/**
* 小圆针对于大圆做圆周运动时,设置小圆中心点的坐标位置
* @param centerX
* 围绕的圆形(大圆)中心点X坐标
* @param centerY
* 围绕的圆形(大圆)中心点Y坐标
* @param R
* 围绕的圆形(大圆)半径
* @param rad
* 旋转的弧度
*/
public void setSmallCircleXY(float centerX, float centerY, float R, double rad) {
//获取圆周运动的X坐标
smallCenterX = (float) (R * Math.cos(rad)) + centerX;
//获取圆周运动的Y坐标
smallCenterY = (float) (R * Math.sin(rad)) + centerY;
}
/**
* 得到两点之间的弧度
* @param px1 第一个点的X坐标
* @param py1 第一个点的Y坐标
* @param px2 第二个点的X坐标
* @param py2 第二个点的Y坐标
* @return
*/
public double getRad(float px1, float py1, float px2, float py2) {
//得到两点X的距离
float dx = px2 - px1;
//得到两点Y的距离
float dy = py1 - py2;
//算出斜边长
float Hypotenuse = (float) Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
//得到这个角度的余弦值(通过三角函数中的定理 :邻边/斜边=角度余弦值)
float cosAngle = dx / Hypotenuse;
//通过反余弦定理获取到其角度的弧度
float rad = (float) Math.acos(cosAngle);
//当触屏的位置Y坐标<摇杆的Y坐标我们要取反值-0~-180 ????
if (py2 < py1) {
rad = -rad;
}
return rad;
}
boolean WORKING = false;
public void reset() {
smallCenterX = BigCenterX;
smallCenterY = BigCenterY;
WORKING = false;
}
public void update(int pointX,int pointY) {
if(WORKING){
//判断用户点击的位置是否在大圆内
if (Math.sqrt(Math.pow((BigCenterX - pointX), 2) + Math.pow((BigCenterY - pointY), 2)) <= (BigCenterR )) {
//让小圆跟随用户触点位置移动
smallCenterX = pointX;
smallCenterY = pointY;
this.rad = getRad(BigCenterX, BigCenterY, pointX, pointY);
}
else {
this.rad = getRad(BigCenterX, BigCenterY, pointX, pointY);
setSmallCircleXY(BigCenterX, BigCenterY, BigCenterR, rad);
}
degreesByNormalSystem = getDegrees(BigCenterX,BigCenterY,smallCenterX,smallCenterY);
}
}
public void begin(int pointX,int pointY) {
if (Math.sqrt(Math.pow((BigCenterX - pointX), 2) + Math.pow((BigCenterY - pointY), 2)) <= (BigCenterR )) {
WORKING = true;
update(pointX,pointY);
} else{
WORKING = false;
}
}
}
这类方法很多,网上一大堆,我给大家找了一个,但是这个还没完善如何判断按键是左还是右。于是我加了点自己的笨方法,有好的方法大家评论指出,谢谢
[mw_shl_code=java,true]#include "cocos2d.h"
using namespace cocos2d;
class Joystick :
public CCLayer
{//本文出自小柒的博客<A >http://blog.csdn.net/cq361106306</A> [/mw_shl_code]
[mw_shl_code=java,true]public:
Joystick(void);
~Joystick(void);
public:
CCPoint centerPoint; // 摇杆中心
CCPoint currentPoint; // 摇杆当前位置
bool active; // 是否激活摇杆
float radius; // 摇杆半径
CCSprite *jsSprite; // 摇杆实例
//************************************
// Method: Active
// FullName: Joystick::Active
// Access: public
// Returns: void
// Qualifier: 设置摇杆功能可用
//************************************
void Active();
//************************************
// Method: Inactive
// FullName: Joystick::Inactive
// Access: public
// Returns: void
// Qualifier: 设置摇杆功能不可用
//************************************
void Inactive();
//************************************
// Method: getDirection
// FullName: Joystick::getDirection
// Access: public
// Returns: cocos2d::CCPoint
// Qualifier: 获取摇杆方向,这里返回的是单位向量
//************************************
CCPoint getDirection();
//************************************
// Method: getVelocity
// FullName: Joystick::getVelocity
// Access: public
// Returns: float
// Qualifier: 获取摇杆的力度
//************************************
float getVelocity();
//************************************
// Method: updatePos
// FullName: Joystick::updatePos
// Access: public
// Returns: void
// Qualifier: 刷新函数,交给日程管理器
// Parameter: ccTime dt
//************************************
void updatePos(ccTime dt);
//************************************
// Method: JoystickWithCenter
// FullName: Joystick::JoystickWithCenter
// Access: public static
// Returns: Joystick*
// Qualifier: 初始化摇杆
// Parameter: CCPoint aPoint 摇杆中心
// Parameter: float aRadius 摇杆半径
// Parameter: CCSprite * aJsSprite 摇杆控制点
// Parameter: CCSprite * aJsBg 摇杆背景
//************************************
static Joystick* JoystickWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg);
Joystick* initWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg);
// 以下是复写Touch响应函数
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
LAYER_NODE_FUNC(Joystick);
};[/mw_shl_code]
[mw_shl_code=java,true]#include "myCircle.h"
Joystick::Joystick(void)
{
}
Joystick::~Joystick(void)
{
}
void Joystick::updatePos(ccTime dt)
{
jsSprite->setPosition(ccpAdd(jsSprite->getPosition(), ccpMult(ccpSub(currentPoint, jsSprite->getPosition()), 0.5)));
}
void Joystick::Active()
{
if(!active)
{
active = true;
schedule(schedule_selector(Joystick::updatePos)); // 添加刷新函数
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false); // 添加触摸委托
}
}
void Joystick::Inactive()
{
if(active)
{
active = false;
this->unschedule(schedule_selector(Joystick::updatePos)); // 删除刷新函数
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); // 删除委托
}
}
bool Joystick::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
if(!active)
return false;
CCPoint touchPoint = pTouch->locationInView(pTouch->view());
touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
if(ccpDistance(touchPoint, centerPoint) > radius)
return false;
currentPoint = touchPoint;
return true;
}
void Joystick::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint touchPoint = pTouch->locationInView(pTouch->view());
touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
if(ccpDistance(touchPoint, centerPoint) > radius)
{
currentPoint = ccpAdd(centerPoint, ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius));
}
else
{
currentPoint = touchPoint;
}
}
void Joystick::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
currentPoint = centerPoint;
}
CCPoint Joystick::getDirection()
{
return ccpNormalize(ccpSub(centerPoint, currentPoint));
}
float Joystick::getVelocity()
{
return ccpDistance(centerPoint, currentPoint);
}
Joystick* Joystick:: JoystickWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg)
{
Joystick *jstick=Joystick::node();
jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg);
return jstick;
}
Joystick* Joystick::initWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg)
{
active = false;
radius = aRadius;
centerPoint = aPoint;
currentPoint = centerPoint;
jsSprite = aJsSprite;
jsSprite->setPosition(centerPoint);
aJsBg->setPosition(centerPoint);
this->addChild(jsSprite);
this->addChild(aJsBg);
return this;
}[/mw_shl_code]
下面给大家调用这个东西的效果
[mw_shl_code=java,true]//判断方向
CCPoint direct=myCircle->getDirection();
CCPoint right=CCPoint(1,0);
CCPoint left=CCPoint(-1,0);
if(ccpAngle(direct,left)<0.707 &&myCircle->currentPoint.x>myCircle->centerPoint.x)
{
CCLog("left");
}
if(ccpAngle(direct,right)<0.707&&myCircle->currentPoint.x<myCircle->centerPoint.x)
{
CCLog("right");
}
[/mw_shl_code]
plaincopyprint?
package com.rp;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
public class Rocker {
double degreesByNormalSystem = Double.NaN;//一般坐标系下的角度
double rad = Double.NaN;//当前遥感的弧度
int rockerColor = Color.GREEN;
//定义两个圆形的中心点坐标与半径
private static final float rockerCenterXMarginRight4ScreenWidthPercent = 0.05f;//摇杆右边界宽度相对于屏幕宽度百分比
private static final float rockerCenterYMarginBottom4ScreenHeightPercent = 0.05f;//摇杆右边界高度相对于屏幕百分比
private static final float rockerR4ScreenWidthPercent = 0.1f;
private float smallCenterX = 120, smallCenterY = 120, smallCenterR = 20;
private float BigCenterX = 120, BigCenterY = 120, BigCenterR = 40;
Paint paint;
/*
* 因为要考虑适应屏幕,所以需要将屏幕宽高放进来
* */
public Rocker(int screenWidth,int screenHeight) {
super();
paint = new Paint();
paint.setAntiAlias(true);
BigCenterR = screenWidth*rockerR4ScreenWidthPercent;
BigCenterX = smallCenterX = screenWidth - BigCenterR*1.5f - rockerCenterXMarginRight4ScreenWidthPercent*screenWidth;
BigCenterY = smallCenterY = screenHeight - BigCenterR*1.5f - rockerCenterYMarginBottom4ScreenHeightPercent*screenHeight;
smallCenterR = BigCenterR/2;
}
void draw(Canvas canvas){
paint.setColor(rockerColor);
paint.setAlpha(0x77);
canvas.drawCircle(BigCenterX, BigCenterY, BigCenterR, paint);
canvas.drawCircle(smallCenterX, smallCenterY, smallCenterR, paint);
paint.setColor(Color.BLACK);
canvas.drawText("原点在左上坐标系下的弧度:"+rad, 20, 20, paint);
canvas.drawText("由该弧度计算得出角度:"+(rad*180/Math.PI), 20, 40, paint);
canvas.drawText("原点在左下坐标系角度:"+degreesByNormalSystem, 20, 60, paint);
}
float getDegrees(float firstX,float firstY,float secondX,float secondY ){
float ret = (float)Math.atan((firstY-secondY)/(firstX-secondX))*180/(float)Math.PI;
if(firstX<secondX){
ret += 180;
}else {
ret += 360;
}
ret = ret >= 360 ? ret - 360: ret;
return ret;
}
/**
* 小圆针对于大圆做圆周运动时,设置小圆中心点的坐标位置
* @param centerX
* 围绕的圆形(大圆)中心点X坐标
* @param centerY
* 围绕的圆形(大圆)中心点Y坐标
* @param R
* 围绕的圆形(大圆)半径
* @param rad
* 旋转的弧度
*/
public void setSmallCircleXY(float centerX, float centerY, float R, double rad) {
//获取圆周运动的X坐标
smallCenterX = (float) (R * Math.cos(rad)) + centerX;
//获取圆周运动的Y坐标
smallCenterY = (float) (R * Math.sin(rad)) + centerY;
}
/**
* 得到两点之间的弧度
* @param px1 第一个点的X坐标
* @param py1 第一个点的Y坐标
* @param px2 第二个点的X坐标
* @param py2 第二个点的Y坐标
* @return
*/
public double getRad(float px1, float py1, float px2, float py2) {
//得到两点X的距离
float dx = px2 - px1;
//得到两点Y的距离
float dy = py1 - py2;
//算出斜边长
float Hypotenuse = (float) Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
//得到这个角度的余弦值(通过三角函数中的定理 :邻边/斜边=角度余弦值)
float cosAngle = dx / Hypotenuse;
//通过反余弦定理获取到其角度的弧度
float rad = (float) Math.acos(cosAngle);
//当触屏的位置Y坐标<摇杆的Y坐标我们要取反值-0~-180 ????
if (py2 < py1) {
rad = -rad;
}
return rad;
}
boolean WORKING = false;
public void reset() {
smallCenterX = BigCenterX;
smallCenterY = BigCenterY;
WORKING = false;
}
public void update(int pointX,int pointY) {
if(WORKING){
//判断用户点击的位置是否在大圆内
if (Math.sqrt(Math.pow((BigCenterX - pointX), 2) + Math.pow((BigCenterY - pointY), 2)) <= (BigCenterR )) {
//让小圆跟随用户触点位置移动
smallCenterX = pointX;
smallCenterY = pointY;
this.rad = getRad(BigCenterX, BigCenterY, pointX, pointY);
}
else {
this.rad = getRad(BigCenterX, BigCenterY, pointX, pointY);
setSmallCircleXY(BigCenterX, BigCenterY, BigCenterR, rad);
}
degreesByNormalSystem = getDegrees(BigCenterX,BigCenterY,smallCenterX,smallCenterY);
}
}
public void begin(int pointX,int pointY) {
if (Math.sqrt(Math.pow((BigCenterX - pointX), 2) + Math.pow((BigCenterY - pointY), 2)) <= (BigCenterR )) {
WORKING = true;
update(pointX,pointY);
} else{
WORKING = false;
}
}
}
这类方法很多,网上一大堆,我给大家找了一个,但是这个还没完善如何判断按键是左还是右。于是我加了点自己的笨方法,有好的方法大家评论指出,谢谢
[mw_shl_code=java,true]#include "cocos2d.h"
using namespace cocos2d;
class Joystick :
public CCLayer
{//本文出自小柒的博客<A >http://blog.csdn.net/cq361106306</A> [/mw_shl_code]
[mw_shl_code=java,true]public:
Joystick(void);
~Joystick(void);
public:
CCPoint centerPoint; // 摇杆中心
CCPoint currentPoint; // 摇杆当前位置
bool active; // 是否激活摇杆
float radius; // 摇杆半径
CCSprite *jsSprite; // 摇杆实例
//************************************
// Method: Active
// FullName: Joystick::Active
// Access: public
// Returns: void
// Qualifier: 设置摇杆功能可用
//************************************
void Active();
//************************************
// Method: Inactive
// FullName: Joystick::Inactive
// Access: public
// Returns: void
// Qualifier: 设置摇杆功能不可用
//************************************
void Inactive();
//************************************
// Method: getDirection
// FullName: Joystick::getDirection
// Access: public
// Returns: cocos2d::CCPoint
// Qualifier: 获取摇杆方向,这里返回的是单位向量
//************************************
CCPoint getDirection();
//************************************
// Method: getVelocity
// FullName: Joystick::getVelocity
// Access: public
// Returns: float
// Qualifier: 获取摇杆的力度
//************************************
float getVelocity();
//************************************
// Method: updatePos
// FullName: Joystick::updatePos
// Access: public
// Returns: void
// Qualifier: 刷新函数,交给日程管理器
// Parameter: ccTime dt
//************************************
void updatePos(ccTime dt);
//************************************
// Method: JoystickWithCenter
// FullName: Joystick::JoystickWithCenter
// Access: public static
// Returns: Joystick*
// Qualifier: 初始化摇杆
// Parameter: CCPoint aPoint 摇杆中心
// Parameter: float aRadius 摇杆半径
// Parameter: CCSprite * aJsSprite 摇杆控制点
// Parameter: CCSprite * aJsBg 摇杆背景
//************************************
static Joystick* JoystickWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg);
Joystick* initWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg);
// 以下是复写Touch响应函数
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
LAYER_NODE_FUNC(Joystick);
};[/mw_shl_code]
[mw_shl_code=java,true]#include "myCircle.h"
Joystick::Joystick(void)
{
}
Joystick::~Joystick(void)
{
}
void Joystick::updatePos(ccTime dt)
{
jsSprite->setPosition(ccpAdd(jsSprite->getPosition(), ccpMult(ccpSub(currentPoint, jsSprite->getPosition()), 0.5)));
}
void Joystick::Active()
{
if(!active)
{
active = true;
schedule(schedule_selector(Joystick::updatePos)); // 添加刷新函数
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, false); // 添加触摸委托
}
}
void Joystick::Inactive()
{
if(active)
{
active = false;
this->unschedule(schedule_selector(Joystick::updatePos)); // 删除刷新函数
CCTouchDispatcher::sharedDispatcher()->removeDelegate(this); // 删除委托
}
}
bool Joystick::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
if(!active)
return false;
CCPoint touchPoint = pTouch->locationInView(pTouch->view());
touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
if(ccpDistance(touchPoint, centerPoint) > radius)
return false;
currentPoint = touchPoint;
return true;
}
void Joystick::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
CCPoint touchPoint = pTouch->locationInView(pTouch->view());
touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);
if(ccpDistance(touchPoint, centerPoint) > radius)
{
currentPoint = ccpAdd(centerPoint, ccpMult(ccpNormalize(ccpSub(touchPoint, centerPoint)), radius));
}
else
{
currentPoint = touchPoint;
}
}
void Joystick::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
currentPoint = centerPoint;
}
CCPoint Joystick::getDirection()
{
return ccpNormalize(ccpSub(centerPoint, currentPoint));
}
float Joystick::getVelocity()
{
return ccpDistance(centerPoint, currentPoint);
}
Joystick* Joystick:: JoystickWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg)
{
Joystick *jstick=Joystick::node();
jstick->initWithCenter(aPoint,aRadius,aJsSprite,aJsBg);
return jstick;
}
Joystick* Joystick::initWithCenter(CCPoint aPoint, float aRadius, CCSprite* aJsSprite, CCSprite* aJsBg)
{
active = false;
radius = aRadius;
centerPoint = aPoint;
currentPoint = centerPoint;
jsSprite = aJsSprite;
jsSprite->setPosition(centerPoint);
aJsBg->setPosition(centerPoint);
this->addChild(jsSprite);
this->addChild(aJsBg);
return this;
}[/mw_shl_code]
下面给大家调用这个东西的效果
[mw_shl_code=java,true]//判断方向
CCPoint direct=myCircle->getDirection();
CCPoint right=CCPoint(1,0);
CCPoint left=CCPoint(-1,0);
if(ccpAngle(direct,left)<0.707 &&myCircle->currentPoint.x>myCircle->centerPoint.x)
{
CCLog("left");
}
if(ccpAngle(direct,right)<0.707&&myCircle->currentPoint.x<myCircle->centerPoint.x)
{
CCLog("right");
}
[/mw_shl_code]
相关文章推荐
- android 虚拟摇杆绘制
- android 虚拟摇杆绘制
- android 如何使用surfaceview绘制类似游戏手柄的摇杆?
- android游戏引擎andengine学习系列三:绘制游戏虚拟摇杆
- Android OpenGL学习笔记(二)之----三角形的绘制.
- Android中使用硬盘模拟SD卡
- iPhone游戏中模拟摇杆的教程
- Android如何绘制视图
- Android开发指南-用户界面-绘制视图
- Java模拟桌球打击处理及绘制
- android中模拟按键的另外一种方法
- 用android做的一个简单的电话拨号器的错误的解决(用android模拟两个人打电话,虽然不能通话)
- Android自动化测试初探(五): 再述模拟键盘鼠标事件(adb shell 实现)
- android 模拟按键
- android 应用模拟按键
- 在android中模拟键盘消息(shell命令的方法)
- iPhone上模拟摇杆 控制对象的代码例子下载
- Android 技术专题系列之六 --如何安装.apk到模拟
- Android OpenGL实战一——开发框架搭建、三角形和四边形的绘制
- android模拟sd卡的问题