检测绕Y轴的Android的Java旋转 [英] detect rotation around Y axis Android Java
问题描述
如何检测手机周围的Y轴旋转?
我在Android的新手。我想探测180度的旋转。我想检测,例如,如果用户的翻盖手机它坐落在一个桌子或如果用户旋转他的电话在他的口袋里。
我已经读了很多文章,但我真的不知道如何得到手机的位置,然后另一个位置之间计算角度。
我发现例如这篇文章,但我不知道该怎么命名的方向阵列做的:
<一个href=\"http://stackoverflow.com/questions/20339942/android-get-device-angle-by-using-getorientation-function\">Android - 通过使用getOrientation功能获取设备角度
谢谢!
//这里是我的解决方案。不完全合乎逻辑的,但著作颇丰:
公共类FlipListener实现SensorEventListener { 的SensorManager sensorMgr;
FlipEventReceiver接收机; 公共FlipListener(上下文的背景下,FlipEventReceiver接收器){
this.receiver =接收器;
sensorMgr =(的SensorManager)context.getSystemService(Activity.SENSOR_SERVICE);
sensorMgr.registerListener(这一点,sensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE),SensorManager.SENSOR_DELAY_UI);
} 公共无效onResume(){
sensorMgr.registerListener(这一点,sensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE),SensorManager.SENSOR_DELAY_UI);
} 公共无效的onPause(){
sensorMgr.unregisterListener(本);
clearStack();
} 私有静态最终诠释IGNORE_FLIPS_AFTER_FLIP = 2500;
私有静态最终诠释SAMPLING_INTERVAL = 60;
私有静态最终诠释MINIMAL_STACK_SIZE_TO_FLIP = 2; //应该不会低于2
私有静态最终浮动FLIP_RADIANS =(浮点)Math.toRadians(140);
私有静态最终诠释STACK_MAX_SIZE = 38; 私人列表&LT;浮球GT;堆栈=新的ArrayList&LT;浮球GT;();
私人长期lastAdd = 0;
私人长期lastFlip = 0; @覆盖
公共无效onSensorChanged(SensorEvent事件){
如果(event.sensor.getType()== Sensor.TYPE_GYROSCOPE){
rotationRateAroundYChanged((浮点)event.values [1]);
}
} @覆盖
公共无效onAccuracyChanged(传感器传感器,精度INT){
} 私人无效rotationRateAroundYChanged(浮动rotationRateAroundY){
长currentTime的= System.currentTimeMillis的(); 如果(lastFlip = 0&放大器;&放大器;!(currentTime的 - lastFlip)下; IGNORE_FLIPS_AFTER_FLIP){
返回;
} 如果((currentTime的 - lastAdd)&GT; = SAMPLING_INTERVAL){
如果(Math.abs(rotationRateAroundY)GT; 0.3){//较小的值是不重要的。他们可以只作乱。
addToStack(rotationRateAroundY);
checkForFlip();
}
}
} 私人无效checkForFlip(){ INT的stackSize = stack.size();
如果(的stackSize&LT; MINIMAL_STACK_SIZE_TO_FLIP)回报;
浮approximateAngleSummary = 0;
浮VAL; 的for(int i = 0; I&LT;的stackSize;我++){
VAL = Math.abs(stack.get(ⅰ).floatValue());
//+ Math.pow(VAL / 4.58,2))没有意义。只要它与它更好。
approximateAngleSummary + =((VAL + Math.pow(VAL / 4.58,2))/ 1000)* SAMPLING_INTERVAL; 如果(approximateAngleSummary&GT; = FLIP_RADIANS){
triggerFlipDetected();
clearStack();
返回;
}
}
} 私人无效clearStack(){
stack.clear();
} 私人无效addToStack(float val)将{
lastAdd = System.currentTimeMillis的();
INT的stackSize = stack.size();
如果(的stackSize大于0&放大器;及((stack.get(的stackSize-1)大于0?1:-1)=(VAL大于0 1:α-1)||的stackSize&GT; STACK_MAX_SIZE)){
clearStack();
}
stack.add(VAL);
} 私人无效triggerFlipDetected(){
lastFlip = System.currentTimeMillis的(); receiver.onFlipDetected();
} 公共接口FlipEventReceiver {
公共无效onFlipDetected();
}
}
用法:
公共类FlipTestActivity扩展活动实现FlipEventReceiver {FlipListener flipListener;
布尔flipListenerActive =真;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_flip_test); flipListener =新FlipListener(这一点,这一点);
}公共无效onFlipDetected(){
//翻页当检测怎么办
} @覆盖
保护无效onResume(){
super.onResume();
如果(!flipListenerActive){
flipListener.onResume();
flipListenerActive = TRUE;
}
} @覆盖
保护无效的onPause(){
super.onPause();
如果(flipListenerActive){
flipListener.onPause();
flipListenerActive = FALSE;
}
}}
的SensorManager是一个类,可以让你访问设备的传感器。检查这里
这里是一个很好的教程的传感器
How to detect rotation around Y axis of phone?
I am novice in android. I would like to detect 180 degrees rotation. I would like to detect for example if user flip phone which lies on a table or if user rotate his phone in his pocket.
I have read a lot of articles but I really don't understand how to get phone position and then compute angle between another position.
I have found for example this article but I don't know what to do with array named orientation:
Android - Get device angle by using getOrientation function
Thanks!
// Here is my solution. Not perfectly logical but works quite good:
public class FlipListener implements SensorEventListener {
SensorManager sensorMgr;
FlipEventReceiver receiver;
public FlipListener(Context context, FlipEventReceiver receiver) {
this.receiver = receiver;
sensorMgr = (SensorManager) context.getSystemService(Activity.SENSOR_SERVICE);
sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_UI);
}
public void onResume() {
sensorMgr.registerListener(this, sensorMgr.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_UI);
}
public void onPause() {
sensorMgr.unregisterListener(this);
clearStack();
}
private static final int IGNORE_FLIPS_AFTER_FLIP = 2500;
private static final int SAMPLING_INTERVAL = 60;
private static final int MINIMAL_STACK_SIZE_TO_FLIP = 2; // Shouldn't be lower than 2
private static final float FLIP_RADIANS = (float)Math.toRadians(140);
private static final int STACK_MAX_SIZE = 38;
private List<Float> stack = new ArrayList<Float>();
private long lastAdd = 0;
private long lastFlip = 0;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
rotationRateAroundYChanged((float)event.values[1]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private void rotationRateAroundYChanged(float rotationRateAroundY) {
long currentTime = System.currentTimeMillis();
if (lastFlip != 0 && (currentTime - lastFlip) < IGNORE_FLIPS_AFTER_FLIP) {
return;
}
if( (currentTime - lastAdd) >= SAMPLING_INTERVAL ) {
if( Math.abs(rotationRateAroundY) > 0.3 ) { // Smaller values are unimportant. They can make only mess.
addToStack(rotationRateAroundY);
checkForFlip();
}
}
}
private void checkForFlip() {
int stackSize = stack.size();
if( stackSize < MINIMAL_STACK_SIZE_TO_FLIP ) return;
float approximateAngleSummary = 0;
float val;
for(int i = 0; i < stackSize; i++) {
val = Math.abs(stack.get(i).floatValue());
// "+ Math.pow(val/4.58, 2) )" don't have a sense. Simply it works better with it.
approximateAngleSummary += ( (val + Math.pow(val/4.58, 2) ) / 1000 ) * SAMPLING_INTERVAL;
if( approximateAngleSummary >= FLIP_RADIANS ) {
triggerFlipDetected();
clearStack();
return;
}
}
}
private void clearStack() {
stack.clear();
}
private void addToStack(float val) {
lastAdd = System.currentTimeMillis();
int stackSize = stack.size();
if( stackSize > 0 && ((stack.get(stackSize-1) > 0 ? 1 : -1) != (val>0?1:-1) || stackSize > STACK_MAX_SIZE) ) {
clearStack();
}
stack.add(val);
}
private void triggerFlipDetected() {
lastFlip = System.currentTimeMillis();
receiver.onFlipDetected();
}
public interface FlipEventReceiver {
public void onFlipDetected();
}
}
Usage:
public class FlipTestActivity extends Activity implements FlipEventReceiver {
FlipListener flipListener;
boolean flipListenerActive = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flip_test);
flipListener = new FlipListener(this, this);
}
public void onFlipDetected() {
// What to do when flip detected
}
@Override
protected void onResume() {
super.onResume();
if( !flipListenerActive ) {
flipListener.onResume();
flipListenerActive = true;
}
}
@Override
protected void onPause() {
super.onPause();
if( flipListenerActive ) {
flipListener.onPause();
flipListenerActive = false;
}
}
}
SensorManager is a class that lets you access the device's sensors. Check here
Here is a nice tutorial about sensors
这篇关于检测绕Y轴的Android的Java旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!