修改ui逻辑,mainactivity逻辑,整合代码

chiyanzhen_branch
CYZ 2 years ago
parent db545bbb14
commit d99f8f6612

@ -22,7 +22,7 @@
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.Sixaunyi" android:theme="@style/Theme.Sixaunyi.NoActionBar"
tools:targetApi="31"> tools:targetApi="31">
<activity <activity
android:name=".SettingActivity" android:name=".SettingActivity"

@ -3,10 +3,10 @@ package com.example.sixaunyi;
import static androidx.constraintlayout.motion.utils.Oscillator.TAG; import static androidx.constraintlayout.motion.utils.Oscillator.TAG;
import static java.lang.Math.PI; import static java.lang.Math.PI;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
@ -21,7 +21,8 @@ import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import android.animation.Animator;
import android.animation.ValueAnimator;
import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationClientOption;
@ -34,6 +35,8 @@ import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng; import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker; import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions; import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.animation.Animation;
import com.amap.api.maps.model.animation.TranslateAnimation;
import com.amap.api.services.core.AMapException; import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint; import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem; import com.amap.api.services.core.PoiItem;
@ -45,7 +48,7 @@ import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.poisearch.PoiResult; import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch; import com.amap.api.services.poisearch.PoiSearch;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
import android.view.animation.LinearInterpolator;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
@ -82,10 +85,6 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
private GeocodeSearch geocodeSearch; private GeocodeSearch geocodeSearch;
//解析成功标识码 //解析成功标识码
private static final int PARSE_SUCCESS_CODE = 1000; private static final int PARSE_SUCCESS_CODE = 1000;
//POI查询对象
private PoiSearch.Query query;
//POI搜索对象
private PoiSearch poiSearch;
//城市码 //城市码
private String cityCode = null; private String cityCode = null;
//浮动按钮 //浮动按钮
@ -100,19 +99,20 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
private MarkerOptions markerOption = new MarkerOptions(); private MarkerOptions markerOption = new MarkerOptions();
// //
private DatagramSocket reveSocket; private DatagramSocket reveSocket;
private final static String Recieve_IP = "192.168.240.46"; private ExecutorService mThreadPool = Executors.newCachedThreadPool();
private final static int RECEIVE_PORT = 9999; ////////////////////////////可修改部分/////////////////////////////
private boolean listenStatus = true; private final LatLng Monitorlatlng = new LatLng(28.258348849051746,113.04552182879037);
private final static String Recieve_IP = "192.168.39.46";
private final static int RECEIVE_PORT = 9987;
private final static String SEND_to_IP = "192.168.39.47";
private final static int SEND_PORT = 8888;
/////////////////////////////////////////////////////////////////
private InetAddress serverAddr; private InetAddress serverAddr;
//private MainActivity.ReceiveHandler receiveHandler = new ReceiveHandler();
private String Angle; private String Angle;
private String MonitorAngle; private String MonitorAngle;
private float distance; private float distance;
private float MonitorDistance; private float MonitorDistance;
private UdpReceiveThread udpReceiveThread; private UdpReceiveThread udpReceiveThread;
private ExecutorService mThreadPool = Executors.newCachedThreadPool();
private final static String SEND_to_IP = "192.168.39.47";
private final static int SEND_PORT = 8888;
@SuppressLint("MissingInflatedId") @SuppressLint("MissingInflatedId")
@Override @Override
@ -123,7 +123,6 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
ClearMarker_btn = findViewById(R.id.clearMarker_btn); ClearMarker_btn = findViewById(R.id.clearMarker_btn);
ChangeAct_btn = findViewById(R.id.change_btn); ChangeAct_btn = findViewById(R.id.change_btn);
ChangeAct_btn.show(); ChangeAct_btn.show();
//tvContent = findViewById(R.id.tv_content); //tvContent = findViewById(R.id.tv_content);
/*, /*,
* *
@ -136,7 +135,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
udpReceiveThread = new UdpReceiveThread(); udpReceiveThread = new UdpReceiveThread();
udpReceiveThread.startListening(); udpReceiveThread.startListening();
addSensorMarker(Monitorlatlng);
} }
/** /**
* Android * Android
@ -289,6 +288,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
city = aMapLocation.getCity(); city = aMapLocation.getCity();
//当前位置经纬度坐标 //当前位置经纬度坐标
Current_latlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude()); Current_latlng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
MonitorDistance = AMapUtils.calculateLineDistance(Current_latlng,Monitorlatlng);
//获取纬度 //获取纬度
double latitude = aMapLocation.getLatitude(); double latitude = aMapLocation.getLatitude();
//获取经度 //获取经度
@ -351,19 +351,16 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
@Override @Override
public void onMapLongClick(LatLng latLng) { public void onMapLongClick(LatLng latLng) {
distance = AMapUtils.calculateLineDistance(Current_latlng,latLng); distance = AMapUtils.calculateLineDistance(Current_latlng,latLng);
LatLng Monitorlatlng = new LatLng(113.05370390725032,28.256794281117525);
MonitorDistance = AMapUtils.calculateLineDistance(Current_latlng,Monitorlatlng);
Angle = calcAngle(Current_latlng.latitude, Current_latlng.longitude, latLng.latitude,latLng.longitude); Angle = calcAngle(Current_latlng.latitude, Current_latlng.longitude, latLng.latitude,latLng.longitude);
MonitorAngle = calcAngle(Current_latlng.latitude, Current_latlng.longitude, 113.05370390725032,28.256794281117525); MonitorAngle = calcAngle(Current_latlng.latitude, Current_latlng.longitude, 113.05370390725032,28.256794281117525);
//showMsg("长按了地图,经度:"+latLng.longitude+",纬度:"+latLng.latitude); //showMsg("长按了地图,经度:"+latLng.longitude+",纬度:"+latLng.latitude);
Log.i(TAG,String.valueOf(latLng.longitude)); Log.i(TAG, String.valueOf(latLng.longitude));
Log.i(TAG,String.valueOf(latLng.latitude)); Log.i(TAG, String.valueOf(latLng.latitude));
showMsg("直线距离:"+distance+"米"+"角度"+Angle); showMsg("直线距离:"+distance+"米"+"角度"+Angle);
//坐标转地址113.05370390725032 28.256794281117525 //坐标转地址113.05370390725032 28.256794281117525
latlonToAddress(latLng); latlonToAddress(latLng);
//添加标点 //添加标点
addMarker(latLng); addMarker(latLng);
} }
/** /**
@ -388,6 +385,126 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
marker.showInfoWindow(); marker.showInfoWindow();
markerList.add(marker); markerList.add(marker);
} }
// 添加传感器标点
private Marker sensorMarker = null;
private void addSensorMarker(LatLng latLng) {
// 显示浮动按钮
// 添加传感器标点
MarkerOptions sensorMarkerOption = new MarkerOptions();
sensorMarker = aMap.addMarker(sensorMarkerOption
.position(latLng)
.draggable(false)
// 标点图标
.icon(BitmapDescriptorFactory.
fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.sensor)))
// 备注
.title("传感器")
.snippet("距离:")
);
sensorMarker.showInfoWindow();
}
// 添加无人机标点
private Marker droneMarker = null;
private void addDroneMarker() {
// 添加无人机标点
MarkerOptions droneMarkerOption = new MarkerOptions();
droneMarker = aMap.addMarker(droneMarkerOption
.position(Current_latlng)
.draggable(false)
// 标点图标
.icon(BitmapDescriptorFactory.
fromBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.drone)))
// 备注
.title("无人机")
.snippet("距离:")
);
droneMarker.showInfoWindow();
}
// 移动无人机标点到传感器标点并显示平移动画
public static class SmoothMoveUtil {
private Marker droneMarker;
private LatLng startLatLng;
private LatLng endLatLng;
private long duration;
public SmoothMoveUtil(Marker marker) {
this.droneMarker = marker;
}
public void moveDroneMarker(LatLng sensorLatLng, long duration) {
if (droneMarker != null) {
startLatLng = droneMarker.getPosition();
endLatLng = sensorLatLng;
this.duration = duration;
// 开始动画
startSmoothMove();
}
}
private void startSmoothMove() {
LatLngInterpolator latLngInterpolator = new LatLngInterpolator.LinearFixed();
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.setDuration(duration);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float fraction = animation.getAnimatedFraction();
LatLng newPosition = latLngInterpolator.interpolate(fraction, startLatLng, endLatLng);
droneMarker.setPosition(newPosition);
}
});
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
// 动画结束
droneMarker.setPosition(endLatLng);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
valueAnimator.start();
}
// LatLng 插值器接口——算经纬度过渡用的
public interface LatLngInterpolator {
LatLng interpolate(float fraction, LatLng a, LatLng b);
class LinearFixed implements LatLngInterpolator {
@Override
public LatLng interpolate(float fraction, LatLng a, LatLng b) {
double lat = (b.latitude - a.latitude) * fraction + a.latitude;
double lngDelta = b.longitude - a.longitude;
// 如果跨越了 180 度经度,则进行调整
if (Math.abs(lngDelta) > 180) {
lngDelta -= Math.signum(lngDelta) * 360;
}
double lng = lngDelta * fraction + a.longitude;
return new LatLng(lat, lng);
}
}
}
}
/** /**
* Marker * Marker
* @param view * @param view
@ -401,6 +518,12 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
ClearMarker_btn.hide(); ClearMarker_btn.hide();
} }
public void moveDrone(){
SmoothMoveUtil moveUtil = new SmoothMoveUtil(droneMarker);
long time = (long) MonitorDistance/2*1000;
moveUtil.moveDroneMarker(Monitorlatlng,time);
}
public void changeAct(View view) { public void changeAct(View view) {
udpReceiveThread.stopListening(); udpReceiveThread.stopListening();
Intent intent = new Intent(MainActivity.this, VideoActivity.class); Intent intent = new Intent(MainActivity.this, VideoActivity.class);
@ -496,7 +619,7 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
public class UdpReceiveThread extends Thread { public class UdpReceiveThread extends Thread {
private static final String TAG = "UdpReceiveThread"; private static final String TAG = "UdpReceiveThread";
private static final int RECEIVE_PORT = 9999; private static final int mRECEIVE_PORT = RECEIVE_PORT;
private boolean mRunning; private boolean mRunning;
private String mReceiveIp = Recieve_IP; private String mReceiveIp = Recieve_IP;
private String mExpectedData = "Motion detected!"; private String mExpectedData = "Motion detected!";
@ -506,17 +629,25 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
@Override @Override
public void run() { public void run() {
try { try {
DatagramSocket socket = new DatagramSocket(RECEIVE_PORT); DatagramSocket socket = new DatagramSocket(mRECEIVE_PORT);
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length); DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (mRunning) { while (mRunning) {
Log.i(TAG, "run: 111111111111111111");
socket.receive(packet); socket.receive(packet);
byte[] data = packet.getData(); byte[] data = packet.getData();
String receivedString = new String(data, 0, packet.getLength()); String receivedString = new String(data, 0, packet.getLength());
Log.d(TAG, "Received data: " + receivedString); Log.d(TAG, "Received data: " + receivedString);
if (receivedString.equals(mExpectedData)) { if (receivedString.equals(mExpectedData)) {
Log.i(TAG, "run: 222222222222222222222222222");
sendCommand("WARNING"); sendCommand("WARNING");
Context context = MainActivity.this;
showAlert(context,"传感器报警");
addDroneMarker();
moveDrone();
mRunning = false; mRunning = false;
} }
} }
socket.close(); socket.close();
@ -527,6 +658,16 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
e.printStackTrace(); e.printStackTrace();
} }
} }
public void showAlert(Context context, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("警告");
builder.setMessage(message);
builder.setIcon(R.drawable.warning);
builder.setPositiveButton("确定", null);
AlertDialog dialog = builder.create();
dialog.show();
}
public void startListening() { public void startListening() {
mRunning = true; mRunning = true;
@ -595,7 +736,9 @@ public class MainActivity extends AppCompatActivity implements AMapLocationListe
} }
return String.valueOf(angle); return String.valueOf(angle);
} }
//UDP发送线程 /*
*UDP线
*/
class SendRunnable implements Runnable { class SendRunnable implements Runnable {
byte[] mData; byte[] mData;
InetAddress mAddress; InetAddress mAddress;

@ -1,14 +1,181 @@
package com.example.sixaunyi; package com.example.sixaunyi;
import static androidx.constraintlayout.motion.utils.Oscillator.TAG;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class SettingActivity extends AppCompatActivity { public class SettingActivity extends AppCompatActivity {
private boolean if_jiankong;
private final static String SEND_to_IP = "192.168.39.47";
private SeekBar Speed_btn;
private Switch Battery_btn;
private ImageButton return_btn;
private final static int SEND_PORT = 8888;
private ExecutorService mThreadPool = Executors.newCachedThreadPool();
private Switch photo_btn;
private Context context;
private SharedPreferences sharedPreferences;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting); setContentView(R.layout.activity_setting);
Speed_btn= findViewById(R.id.speed);
return_btn=findViewById(R.id.returns);
photo_btn = findViewById(R.id.photo);
Battery_btn = findViewById(R.id.Battery_warning);
TextView tvProgress = (TextView) findViewById(R.id.tvProgress);
sharedPreferences = getSharedPreferences("control_state", MODE_PRIVATE);
boolean photo_btn_state= sharedPreferences.getBoolean("photo_btn_state", false);
photo_btn.setChecked(photo_btn_state);
// 恢复SeekBar状态
int speedProgress = sharedPreferences.getInt("speed_progress", 0);
Speed_btn.setProgress(speedProgress);
tvProgress.setText(String.valueOf(speedProgress+10));
photo_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 保存Switch状态
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("photo_btn_state", isChecked);
editor.apply();
}
});
Speed_btn.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tvProgress.setText(String.valueOf(progress+10));
int speed=progress+10;
String s = "SPEED " + Integer.toString(speed);
try {
sendCommand(s);
} catch (UnknownHostException e) {
e.printStackTrace();
}
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("speed_progress", progress);
editor.apply();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
Battery_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Battery_btn.isChecked()) {
try {
sendCommand("battery_warning");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}else
{
try {
sendCommand("warning_cancel");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
} }
});
return_btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(SettingActivity.this, VideoActivity.class);
// 启动目标 Activity
finish();
}
});
photo_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (photo_btn.isChecked()) {
try {
sendCommand("photo_high");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}else
{
try {
sendCommand("photo_low");
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
});
}
/*
* UDP线
* */
class SendRunnable implements Runnable {
byte[] mData;
InetAddress mAddress;
int mPort;
public SendRunnable(byte[] data, InetAddress address, int port) {
mData = data;
mAddress = address;
mPort = port;
}
@Override
public void run() {
try {
// 创建发送数据报文
DatagramPacket packet = new DatagramPacket(mData, mData.length, mAddress, mPort);
// 创建 DatagramSocket 对象并发送数据报文
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
// 关闭 DatagramSocket 对象
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*封装函数*/
private void sendCommand(String str) throws UnknownHostException {
byte[] sendData = str.getBytes();
InetAddress address = InetAddress.getByName(SEND_to_IP);
SendRunnable sendRunnable1 = new SendRunnable(sendData, address, SEND_PORT);
mThreadPool.execute(sendRunnable1);
}
} }

@ -26,6 +26,8 @@ import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button; import android.widget.Button;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.ImageButton; import android.widget.ImageButton;
@ -78,6 +80,8 @@ public class VideoActivity extends AppCompatActivity {
private float mdistance; private float mdistance;
// 创建一个可缓存的线程池 // 创建一个可缓存的线程池
private ExecutorService mThreadPool = Executors.newCachedThreadPool(); private ExecutorService mThreadPool = Executors.newCachedThreadPool();
private UdpDataThread warning_thread;
private TextView battery_warning;
//ReceiveData()显示到imageView //ReceiveData()显示到imageView
//manager部分打开线程接收 //manager部分打开线程接收
//model部分发送视频流 //model部分发送视频流
@ -99,9 +103,19 @@ public class VideoActivity extends AppCompatActivity {
TakeOff_btn = findViewById(R.id.takeoff_button); TakeOff_btn = findViewById(R.id.takeoff_button);
Mode_btn = findViewById(R.id.toggleButton2); Mode_btn = findViewById(R.id.toggleButton2);
Setting_btn = findViewById(R.id.setting_button); Setting_btn = findViewById(R.id.setting_button);
battery_warning =findViewById(R.id.warning);
battery_warning.setTextColor(Color.RED);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(500); // 闪烁时间间隔
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
battery_warning.startAnimation(anim);
Intent i = getIntent(); Intent i = getIntent();
mdistance = i.getFloatExtra("Distance",0); mdistance = i.getFloatExtra("Distance",0);
mangle = i.getStringExtra("Angle"); mangle = i.getStringExtra("Angle");
warning_thread = new UdpDataThread();
warning_thread.start();
try { try {
Condition_Init(); Condition_Init();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
@ -267,7 +281,7 @@ public class VideoActivity extends AppCompatActivity {
} }
private void changetoManual() throws UnknownHostException { private void changetoManual() throws UnknownHostException {
sendCommand("MAN"); sendCommand("STOP");
} }
class ReceiveHandler extends Handler{ class ReceiveHandler extends Handler{
@ -378,6 +392,49 @@ public class VideoActivity extends AppCompatActivity {
mThreadPool.execute(sendRunnable1); mThreadPool.execute(sendRunnable1);
} }
/*
*UDP线
*/
public class UdpDataThread extends Thread {
private DatagramSocket socket;
private boolean running;
private byte[] buffer;
private static final int BUFFER_SIZE = 1024; // 设置接收缓冲区大小
public UdpDataThread() {
buffer = new byte[BUFFER_SIZE];
}
@Override
public void run() {
running = true;
try {
socket = new DatagramSocket(6666); // 设置接收端口号
while (running) {
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String receivedData = new String(packet.getData(), 0, packet.getLength());
if (receivedData.equals("电量低于10%")) {
battery_warning.setText(receivedData);}else{
battery_warning.setText("");
}
System.out.println("接收到数据:" + receivedData);
// 在这里处理收到的数据,可以根据实际需求进行解析或其他操作
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
socket.close();
}
}
}
public void stopThread() {
running = false;
}
}
//初始化UDP发送线程 //初始化UDP发送线程
// private static void sendCommand(String Sendstr) throws UnknownHostException { // private static void sendCommand(String Sendstr) throws UnknownHostException {
// InetAddress sendIP = InetAddress.getByName(SEND_to_IP); // 目标 IP 地址 // InetAddress sendIP = InetAddress.getByName(SEND_to_IP); // 目标 IP 地址

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 992 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置透明背景色 -->
<solid android:color="#80000000" />
<!-- 设置一个黑色边框 -->
<stroke
android:width="2px"
android:color="#000000" />
<!-- 设置四个圆角的半径 -->
<corners
android:bottomLeftRadius="10px"
android:bottomRightRadius="10px"
android:topLeftRadius="10px"
android:topRightRadius="10px" />
<!-- 设置一下边距,让空间大一点 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
</shape>

@ -61,7 +61,7 @@
android:background="@null" android:background="@null"
android:contentDescription="@string/turn_left_button" android:contentDescription="@string/turn_left_button"
android:src="@drawable/turnleft" android:src="@drawable/turnleft"
android:scaleType="centerInside" android:scaleType="centerCrop"
/> />
<!-- 右旋转按钮 --> <!-- 右旋转按钮 -->
@ -199,11 +199,10 @@
android:id="@+id/setting_button" android:id="@+id/setting_button"
android:layout_width="50dp" android:layout_width="50dp"
android:layout_height="50dp" android:layout_height="50dp"
android:src="@drawable/setting"
android:background="@null" android:background="@null"
android:contentDescription="@string/setting_button" android:contentDescription="@string/setting_button"
android:scaleType="centerInside" android:scaleType="centerInside"
/> android:src="@drawable/setting" />
<ImageButton <ImageButton
android:id="@+id/start_record_button" android:id="@+id/start_record_button"
@ -249,6 +248,13 @@
android:textOn="" /> android:textOn="" />
</LinearLayout> </LinearLayout>
<TextView
android:id="@+id/warning"
android:layout_width="131dp"
android:layout_height="40dp"
android:layout_marginTop="70dp"
android:text=""
android:textSize="22dp"/>
</FrameLayout> </FrameLayout>

@ -22,7 +22,7 @@
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_margin="20dp" android:layout_margin="20dp"
android:clickable="true" android:clickable="true"
android:onClick="moveDrone"
android:src="@drawable/icon_favorite_red" android:src="@drawable/icon_favorite_red"
android:visibility="gone" android:visibility="gone"
app:backgroundTint="#FFF" app:backgroundTint="#FFF"

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
@ -16,34 +17,67 @@
android:background="#80FFFFFF" android:background="#80FFFFFF"
android:orientation="vertical" android:orientation="vertical"
android:padding="16dp"> android:padding="16dp">
<ImageButton
android:id="@+id/returns"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="bottom"
android:background="#00000000"
android:scaleType="centerInside"
app:srcCompat="@drawable/returns" />
<Switch <Switch
android:id="@+id/switch1" android:id="@+id/Battery_warning"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
android:text="飞行速度(高)" android:text="电量预警"
android:textSize="28dp" /> android:textColor="@color/white"
android:textSize="28dp"
android:background="@drawable/txt_radiuborder"/>
<Switch <Switch
android:id="@+id/switch2" android:id="@+id/photo"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
android:layout_marginTop="20dp" android:layout_marginTop="10dp"
android:text="图像高画质" android:text="图像高画质"
android:textSize="28dp" /> android:textSize="28dp"
android:textColor="@color/white"
android:background="@drawable/txt_radiuborder"/>
<TextView <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="50dp"
android:layout_marginTop="20dp" android:layout_marginTop="10dp"
android:text="低电量预警" android:orientation="horizontal"
android:background="@drawable/txt_radiuborder">
<TextView
android:layout_width="150dp"
android:layout_height="50dp"
android:text="飞行速度"
android:textColor="@color/white"
android:textSize="28dp" /> android:textSize="28dp" />
<SeekBar <SeekBar
android:id="@+id/seekBar" android:id="@+id/speed"
android:layout_width="match_parent" android:layout_width="180dp"
android:layout_height="50dp" android:layout_height="50dp"
android:layout_marginTop="16dp" android:max="90"
android:progress="0"
android:thumbTint='@color/teal_200' /> android:thumbTint='@color/teal_200' />
<TextView
android:id="@+id/tvProgress"
android:layout_width="77dp"
android:layout_height="30dp"
android:text="10"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout> </LinearLayout>
</FrameLayout> </FrameLayout>

Loading…
Cancel
Save