package com.demo.navtogether; import androidx.appcompat.app.AppCompatActivity; import android.app.ProgressDialog; import android.graphics.Point; import android.os.Bundle; import android.view.View; import android.view.ViewTreeObserver; import android.widget.Button; import android.widget.Toast; import com.demo.navtogether.utils.DijkstraUtils; import com.demo.navtogether.utils.PathPlan; import com.demo.navtogether.utils.TriangleUtils; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity { PlanPathView planPathView; Button btnRefresh,btnDrawD,btnDrawC; boolean animD,animC; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); planPathView = findViewById(R.id.planpath); btnRefresh = findViewById(R.id.btn_refresh); btnDrawD = findViewById(R.id.btn_draw_dijkstra); btnDrawC = findViewById(R.id.btn_draw_custom); ViewTreeObserver vto = planPathView.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { int height = planPathView.getMeasuredHeight(); int width = planPathView.getMeasuredWidth(); planPathView.setPoints(randomList(width,height)); planPathView.getViewTreeObserver().removeOnPreDrawListener(this); return true; } }); } PlanPathView.AnimatorListener listener = new PlanPathView.AnimatorListener() { @Override public void onAnimation(int value) { btnRefresh.setEnabled(false); btnDrawD.setEnabled(false); btnDrawC.setEnabled(false); } @Override public void onFinish() { btnRefresh.setEnabled(true); btnDrawD.setEnabled(true); btnDrawC.setEnabled(true); } }; /** * 创建随机位置 * @param width 范围宽度 * @param height 范围高度 * @return */ public ArrayList randomList(int width,int height){ ArrayList places = new ArrayList<>(); places.add(new PathPlan.Place("甲",random(width,height),0)); places.add(new PathPlan.Place("乙",random(width,height),0)); places.add(new PathPlan.Place("丙",random(width,height),0)); places.add(new PathPlan.Place("丁",random(width,height),0)); places.add(new PathPlan.Place("戊",random(width,height),0)); places.add(new PathPlan.Place("己",random(width,height),0)); places.add(new PathPlan.Place("庚",random(width,height),0)); places.add(new PathPlan.Place("辛",random(width,height),0)); places.add(new PathPlan.Place("壬",random(width,height),0)); places.add(new PathPlan.Place("癸",random(width,height),0)); return places; } /** * 创建随机位置 * @param maxX * @param maxY * @return */ public Point random(int maxX,int maxY){ int marginX = (int) (getResources().getDisplayMetrics() .density*15)*2; Point point = new Point(); point.x = (int) (Math.random()*(maxX-marginX))+(marginX/2); point.y = (int) (Math.random()*(maxY-marginX))+(marginX/2); return point; } /** * 迪杰斯特拉算法画线 * @param view */ public void drawLine(View view){ planPathView.clearLines(); //执行算法,返回位置路线集合 ArrayList excute = new DijkstraUtils(PathPlan.intPoints(planPathView.getPoints())).excute(planPathView.getPoints().get(2)); ArrayList lines = new ArrayList<>(); for(int i = 0 ; i < excute.size() -1; i ++){ //生成路线 lines.add(new PlanPathView.Line(excute.get(i).point,excute.get(i+1).point,excute.get(i+1).distance)); System.out.println(excute.get(i)+" 距离:"+(i == excute.size() -1?"": TriangleUtils.getLengthOfSide(excute.get(i).point,excute.get(i+1).point)) +" "+excute.get(i).distance); } if(animD){ planPathView.drawLine(lines); }else { animD = true; planPathView.drawAnimLine(lines, listener); } } int index = 0 ; ArrayList> customLists; /** * 自定义算法画线 * @param view */ public void drawLineCustom(View view){ planPathView.clearLines(); System.out.println("drawLineCustom " ); if(customLists == null || customLists.size() == 0) { ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setMessage("正在規劃路綫"); progressDialog.show(); new Thread(new Runnable() { @Override public void run() { HashMap distanceHashMap = PathPlan.intPoints(planPathView.getPoints()); customLists = new DijkstraUtils(distanceHashMap).initCustomPathPlan(planPathView.getPoints().get(2), null); runOnUiThread(new Runnable() { @Override public void run() { System.out.println("drawLineCustom " ); progressDialog.dismiss(); drawLineCustome(); } }); } }).start(); }else{ drawLineCustome(); } } public void drawLineCustome(){ if(customLists.size()>1){ Toast.makeText(MainActivity.this,"当前有"+customLists.size()+"路线规划",Toast.LENGTH_SHORT).show(); } ArrayList lines = new ArrayList<>(); ArrayList arrayList = customLists.get(index % customLists.size()); for(int i = 0 ; i < arrayList.size() -1; i ++){ lines.add(new PlanPathView.Line(arrayList.get(i).point,arrayList.get(i+1).point,arrayList.get(i+1).distance)); } index++; if(animC){ planPathView.drawLine(lines); }else { animC = true; planPathView.drawAnimLine(lines, listener); } } public void refresh(View view) { planPathView.clear(); customLists.clear(); int width = planPathView.getWidth(); int height = planPathView.getHeight(); ArrayList places = new ArrayList<>(); places.add(new PathPlan.Place("A",random(width,height),0)); places.add(new PathPlan.Place("B",random(width,height),0)); places.add(new PathPlan.Place("C",random(width,height),0)); places.add(new PathPlan.Place("D",random(width,height),0)); places.add(new PathPlan.Place("E",random(width,height),0)); places.add(new PathPlan.Place("F",random(width,height),0)); places.add(new PathPlan.Place("G",random(width,height),0)); places.add(new PathPlan.Place("H",random(width,height),0)); animD = false; animC = false; planPathView.setAnimPoints(randomList(width,height),listener); } }