修改了速度移动的处理

This commit is contained in:
ZCShou 2020-09-02 11:53:56 +08:00
parent 9900242466
commit 5dc139b0c5
4 changed files with 73 additions and 40 deletions

View File

@ -3,6 +3,7 @@ package com.zcshou.joystick;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.CountDownTimer;
@ -14,6 +15,9 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.Toast;
import androidx.preference.PreferenceManager;
import com.zcshou.gogogo.R;
@ -24,11 +28,7 @@ public class JoyStick extends View {
private WindowManager mWindowManager;
private View mFloatView;
private JoyStickClickListener mListener;
private TimeCount time;
boolean isAuto;
double mAngle;
double mR;
// 控制按键相关
ImageButton btnInput;
boolean isWalk;
ImageButton btnWalk;
@ -37,7 +37,14 @@ public class JoyStick extends View {
boolean isBike;
ImageButton btnBike;
public JoyStick(Service context) {
// 移动
private TimeCount time;
boolean isAuto;
double mAngle;
double mR;
double mSpeed;
public JoyStick(Context context) {
super(context);
this.mContext = context;
@ -97,6 +104,8 @@ public class JoyStick extends View {
return;
}
mSpeed = 1.3;
mFloatView = inflater.inflate(R.layout.joystick, null);
mFloatView.setOnTouchListener(new JoyStickOnTouchListener());
@ -120,7 +129,10 @@ public class JoyStick extends View {
isRun = false;
btnBike.setImageResource(R.drawable.ic_bike);
isBike = false;
mListener.setCurrentSpeed(0.00003);
// mSpeed = sharedPref.getFloat("setting_walk", (float) 0.00003);
mSpeed = 1.3;
//DisplayToast("Speed:" + mSpeed);
mListener.setCurrentSpeed(mR*mSpeed);
}
}
});
@ -137,7 +149,10 @@ public class JoyStick extends View {
isWalk = false;
btnBike.setImageResource(R.drawable.ic_bike);
isBike = false;
mListener.setCurrentSpeed(0.00006);
// mSpeed = sharedPref.getFloat("setting_run", (float) 0.00006);
mSpeed = 4.0;
//DisplayToast("Speed:" + mSpeed);
mListener.setCurrentSpeed(mR*mSpeed);
}
}
});
@ -154,7 +169,10 @@ public class JoyStick extends View {
isWalk = false;
btnRun.setImageResource(R.drawable.ic_run);
isRun = false;
mListener.setCurrentSpeed(0.00009);
// mSpeed = sharedPref.getFloat("setting_bike", (float) 0.00009);
mSpeed = 12.0;
//DisplayToast("Speed:" + mSpeed);
mListener.setCurrentSpeed(mR*mSpeed);
}
}
});
@ -163,6 +181,7 @@ public class JoyStick extends View {
private void initJoyStickView() {
time = new TimeCount(1000, 1000);
isAuto = false;
mR = 0;
ButtonView btnView = mFloatView.findViewById(R.id.joystick_view);
btnView.setListener(new ButtonView.ButtonViewClickListener() {
@ -171,6 +190,7 @@ public class JoyStick extends View {
if (isAuto) {
isAuto = false;
time.cancel();
mR = 0;
} else {
isAuto = true;
}
@ -179,48 +199,48 @@ public class JoyStick extends View {
@Override
public void clickTop() {
time.cancel();
mAngle = 0;
mAngle = 90;
mR = 1;
if (isAuto) {
time.start();
} else {
mListener.clickAngleInfo(mAngle, mR);
mListener.clickAngleInfo(mAngle, mR*mSpeed);
}
}
@Override
public void clickRight() {
time.cancel();
mAngle = 90;
mAngle = 0;
mR = 1;
if (isAuto) {
time.start();
} else {
mListener.clickAngleInfo(mAngle, mR);
mListener.clickAngleInfo(mAngle, mR*mSpeed);
}
}
@Override
public void clickBottom() {
time.cancel();
mAngle = 180;
mAngle = 270;
mR = 1;
if (isAuto) {
time.start();
} else {
mListener.clickAngleInfo(mAngle, mR);
mListener.clickAngleInfo(mAngle, mR*mSpeed);
}
}
@Override
public void clickLeft() {
time.cancel();
mAngle = 270;
mAngle = 180;
mR = 1;
if (isAuto) {
time.start();
} else {
mListener.clickAngleInfo(mAngle, mR);
mListener.clickAngleInfo(mAngle, mR*mSpeed);
}
}
@ -275,7 +295,7 @@ public class JoyStick extends View {
}
public interface JoyStickClickListener {
void clickAngleInfo(double angle, double r);
void clickAngleInfo(double angle, double speed);
void setCurrentSpeed(double speed);
}
@ -287,7 +307,7 @@ public class JoyStick extends View {
@Override
public void onFinish() {//计时完毕时触发
mListener.clickAngleInfo(mAngle, mR);
mListener.clickAngleInfo(mAngle, mR*mSpeed);
time.start();
}
@ -297,9 +317,9 @@ public class JoyStick extends View {
}
}
// public void DisplayToast(String str) {
// Toast toast = Toast.makeText(mContext, str, Toast.LENGTH_LONG);
// toast.setGravity(Gravity.TOP, 0, 220);
// toast.show();
// }
// public void DisplayToast(String str) {
// Toast toast = Toast.makeText(mContext, str, Toast.LENGTH_LONG);
// toast.setGravity(Gravity.TOP, 0, 220);
// toast.show();
// }
}

View File

@ -170,20 +170,31 @@ public class GoGoGoService extends Service {
mJoyStick = new JoyStick(this);
mJoyStick.setListener(new JoyStick.JoyStickClickListener() {
@Override
public void clickAngleInfo(double angle, double r) {
// 注意这里的 x y 圆中角度的对应问题
public void clickAngleInfo(double angle, double speed) {
mSpeed = speed * 3.6; // 转换为 km/h, 1米/(m/s)=3.6千米/(km/h)
// 注意这里的 x y 圆中角度的对应问题 X 轴正向为 0
double x = Math.cos(angle * 2 * Math.PI / 360); // 注意安卓使用的是弧度
double y = Math.sin(angle * 2 * Math.PI / 360); // 注意安卓使用的是弧度
// 根据当前的经纬度和距离计算下一个经纬度
// Latitude: 1 deg = 110.574 km // 纬度的每度的距离大约为 110.574km
// Longitude: 1 deg = 111.320*cos(latitude) km // 经度的每度的距离从0km到111km不等
// 具体见http://wp.mlab.tw/?p=2200
String[] latLngStr = curLatLng.split("&");
double lat = Double.parseDouble(latLngStr[0]) + mSpeed * y;
double lng = Double.parseDouble(latLngStr[1]) + mSpeed * x;
curLatLng = lat + "&" + lng;
double lngDegree = mSpeed * x / (111.320 * Math.cos(Math.abs(Double.parseDouble(latLngStr[1])) * Math.PI / 180));
double latDegree = mSpeed * y / 110.574;
double lng = Double.parseDouble(latLngStr[0]) + lngDegree / 1000; // 为啥 / 1000 ? 按照速度算下来这里偏大
double lat = Double.parseDouble(latLngStr[1]) + latDegree / 1000; // 为啥 / 1000 ? 按照速度算下来这里偏大
curLatLng = lng + "&" + lat;
}
@Override
public void setCurrentSpeed(double speed) {
mSpeed = speed;
mSpeed = speed * 3.6; // 转换为 km/h, 1米/(m/s)=3.6千米/(km/h)
}
});

View File

@ -43,10 +43,12 @@
<!-- 设置界面 -->
<string name="title_activity_settings">设置</string>
<string name="setting_group_move">移动</string>
<string name="setting_move_summary">不建议将速度设置过大</string>
<string name="setting_walk">步行速度</string>
<string name="setting_run">跑步速度</string>
<string name="setting_bike">驾驶速度</string>
<string name="setting_walk">步行速度(米/秒)</string>
<string name="setting_walk_default">1.2</string>
<string name="setting_run">跑步速度(米/秒)</string>
<string name="setting_run_default">3.6</string>
<string name="setting_bike">驾驶速度(米/秒)</string>
<string name="setting_bike_default">10.0</string>
<string name="setting_group_log">日志</string>
<string name="setting_log_off">关闭日志</string>
<string name="setting_group_about">关于</string>

View File

@ -5,20 +5,20 @@
<EditTextPreference
app:key="setting_walk"
app:title="@string/setting_walk"
app:defaultValue="3"
app:summary="@string/setting_move_summary"/>
app:defaultValue="@string/setting_walk_default"
app:summary="@string/setting_walk_default"/>
<EditTextPreference
app:key="setting_run"
app:title="@string/setting_run"
app:defaultValue="6"
app:summary="@string/setting_move_summary"/>
app:defaultValue="@string/setting_run_default"
app:summary="@string/setting_run_default"/>
<EditTextPreference
app:key="setting_bike"
app:title="@string/setting_bike"
app:defaultValue="9"
app:summary="@string/setting_move_summary"/>
app:defaultValue="@string/setting_bike_default"
app:summary="@string/setting_bike_default"/>
</PreferenceCategory>