whycxzp
2024-07-19 339df2d21fc3c2784300db90eeb2299e0f89dc84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.whyc.handcontrol;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
 
import com.whyc.R;
import com.whyc.handcontrol.sensors.Orientation;
 
/**
 * @Author: sunchao
 * @CreateDate: 2020/7/18 2:52 PM
 * imu头控实现示例
 */
public class HandControlActivity extends AppCompatActivity implements Orientation.Listener{
 
    private Orientation mOrientation;
    private View view;
    FrameLayout.LayoutParams lp;
    private float xCurrent = 0;//当前横向角度
    private float yCurrent = 0;//当前纵向角度
    public static final int SPEED_MAINANGLE = 10;
    /**
     * 每步滑动的距离
     */
    public static final float DISTANCE_ONESTEP = 20f;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handcontrol);
        view = findViewById(R.id.view);
 
        initImu();
        initViews();
        initData();
    }
 
    private void initImu() {
 
        //TODO add to onResume ?
 
        mOrientation = Orientation.getInstance();
        mOrientation.initialize(this);
        mOrientation.addListener(this);
        mOrientation.startListening();
    }
 
    private void initViews() {
        view = findViewById(R.id.view);
    }
 
    private void initData() {
 
    }
 
    @Override
    public void onOrientationChanged(float pitch, float roll, float yaw) {
        scrollScreen(yaw, pitch);
    }
 
    @Override
    public void onStart() {
        super.onStart();
        if (mOrientation != null)
            mOrientation.addListener(this);
 
    }
 
    @Override
    public void onStop() {
        super.onStop();
        if (mOrientation != null)
            mOrientation.removeListener(this);
 
    }
 
    private void scrollScreen(float x, float y) {
        if (xCurrent == 0 && yCurrent == 0) {
            xCurrent = x;
            yCurrent = y;
            return;
        }
        float xDeviation = getDeviation(x, xCurrent);//x偏移
        float yDeviation = getDeviation(y, yCurrent);//y偏移
//        Logger.d("mainact  xDeviation =  " + xDeviation + " yDeviation = " + yDeviation);
 
        Boolean errorImu = xDeviation < -1 * SPEED_MAINANGLE || xDeviation > SPEED_MAINANGLE || yDeviation < -1 * SPEED_MAINANGLE || yDeviation > SPEED_MAINANGLE;
        if (errorImu) {
            xCurrent = x;
            yCurrent = y;
            return;
        }
        if ((xDeviation < -0.05 || xDeviation > 0.05) || (yDeviation < -0.02 || yDeviation > 0.02)) {
            int xDeviationInt = Math.round(xDeviation * DISTANCE_ONESTEP);
            int yDeviationInt = Math.round(yDeviation * DISTANCE_ONESTEP * 2);
            if (Math.abs(xDeviationInt) < 1 && Math.abs(yDeviationInt) < 1)
                return;
 
            if (lp == null)
                lp = (FrameLayout.LayoutParams) view.getLayoutParams();
            int leftMargin = lp.leftMargin + xDeviationInt;
            int topMargin = lp.topMargin + yDeviationInt;
            lp.leftMargin = leftMargin;
            lp.topMargin = topMargin;
            view.setLayoutParams(lp);
 
 
//            ll_parent.setTranslationX(xDeviation);
//            ll_parent.setTranslationY(yDeviation);
            yCurrent = y;
            xCurrent = x;
 
        }
 
 
    }
 
    /**
     * 计算偏移值
     *
     * @return
     */
    public static float getDeviation(float value, float base) {
        if (base > 0) {
            if (value > 0) {
                return value - base;
            } else {
                if (Math.abs(value - base) < 180) {
                    return value - base;
                } else {
                    return 180 + value + 180 - base;
                }
 
            }
        } else {
            if (value < 0) {
                return value - base;
            } else {
                if (Math.abs(value - base) < 180) {
                    return value - base;
                } else {
                    return -180 + value - 180 - base;
                }
 
            }
        }
    }
}