Windows Phone 7 摇动检测

SDK: Windows Phone 7.1 SDK

WS - OtomiiLu 在 09 Aug 2012 创建

摇动支持

public enum ActionEnum
{
None = 0,
LeftRightShake = 1,
UpDownShake = 2
}

accHelper.SupportedAction = ActionEnum.LeftRightShake | ActionEnum.UpDownShake;

(SupportedAction & ActionEnum.LeftRightShake).Equals(ActionEnum.LeftRightShake)

LeftRightShake = 0001,

UpDownShake = 0010

ActionEnum.LeftRightShake | ActionEnum.UpDownShake = 0011

SupportedAction & ActionEnum.LeftRightShake = 0001,去掉了属于UpDownShake的那个1

(SupportedAction & ActionEnum.LeftRightShake).Equals(ActionEnum.LeftRightShake)

重力坐标类Axis

public class Axis
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public DateTime CurrentDateTime { get; set; }
public AccelerationDirection AccDirection { get; set; }
}

public enum ActionEnum
{
None = 0,
LeftRightShake = 1,
UpDownShake = 2
}

重力坐标集合类AxisCollection

AxisCollection是最主要的一个类,提供了很多的方法,为了重力判断的使用.

{
var Gravity = axis.X * axis.X + axis.Y * axis.Y + axis.Z * axis.Z;
if (Gravity < 0.8 || Gravity > 1.2)
{
if (-0.3 > axis.X || axis.X > 0.3)
{
if (axis.X < -0.3) axis.AccDirection = AccelerationDirection.Left;
else if (axis.X > 0.3) axis.AccDirection = AccelerationDirection.Right;
}
else
{
if ((axis.Y * axis.Y + axis.Z * axis.Z > 1.2)) axis.AccDirection = AccelerationDirection.Up;
else if (axis.Y * axis.Y + axis.Z * axis.Z < 0.8) axis.AccDirection = AccelerationDirection.Down;
}
axis.CurrentDateTime = DateTime.Now;
}
}

public bool IsMoving()
{
if (AxisList.Count != 0)
{
if (DateTime.Now.Subtract(AxisList.Last().CurrentDateTime).TotalMilliseconds > 666)
{
return false;
}
}
return true;
}

public void CheckCurrentAction()
{
if (AxisList.Count == 0) CurrentActionEnum = ActionEnum.None;
else
{
if (!(CurrentActionEnum .Equals( ActionEnum.None)))
{
return;
}
if ((SupportedAction & ActionEnum.LeftRightShake).Equals(ActionEnum.LeftRightShake) &&
AxisList.Where(al => al.AccDirection.Equals(AccelerationDirection.Left)).Count() > 2 && AxisList.Where(al => al.AccDirection.Equals(AccelerationDirection.Right)).Count() > 2)
CurrentActionEnum = ActionEnum.LeftRightShake;
else if ((SupportedAction & ActionEnum.UpDownShake).Equals(ActionEnum.UpDownShake) &&
AxisList.Where(al => al.AccDirection.Equals(AccelerationDirection.Up)).Count() > 2 && AxisList.Where(al => al.AccDirection.Equals(AccelerationDirection.Down)).Count() > 2)
CurrentActionEnum = ActionEnum.UpDownShake;
}
}

public bool Reset()
{
try
{
AxisList.Clear();
}
catch (Exception)
{
return false;
}
return true;
}

帮助类AccHelper

AccHelper类的主要功能就是帮助我们进行中心控制,封装一些我们不需要处理的操作. 下面,就是整个WPAcchelper类

namespace AccHelper
{
public class WPAccHelper
{
private Accelerometer accelerometer = new Accelerometer();
private AxisCollection axisCollection = new AxisCollection();
public ActionEnum SupportedAction
{
get { return axisCollection.SupportedAction; }
set { axisCollection.SupportedAction = value; }
}
public WPAccHelper()
{
accelerometer.Start();
}

{
if (!axisCollection.IsMoving())
{
axisCollection.Reset();
return;
}
axisCollection.CheckCurrentAction();
}

public string GetInfo()
{
return axisCollection.CurrentActionEnum.ToString();
}
}
}

GamePage页面

• 声明一个WPAcchelper类,并设定支持的晃动类型是什么
WPAccHelper accHelper;
accHelper = new WPAccHelper();
accHelper.SupportedAction = ActionEnum.LeftRightShake | ActionEnum.UpDownShake;

private void OnDraw(object sender, GameTimerEventArgs e)
{
SharedGraphicsDeviceManager.Current.GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin();
spriteBatch.DrawString(MySprite, accHelper.GetInfo(), new Vector2(0, 0), Color.White);
spriteBatch.End();