package com.demo.navtogether; import androidx.appcompat.app.AppCompatActivity; 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(); 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)); planPathView.setPoints(places); 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 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 ; /** * 自定义算法画线 * @param view */ public void drawLineCustom(View view){ planPathView.clearLines(); System.out.println("drawLineCustom " ); HashMap distanceHashMap = PathPlan.intPoints(planPathView.getPoints()); ArrayList> arrayLists = new DijkstraUtils(distanceHashMap).initCustomPathPlan(planPathView.getPoints().get(2), null); if(arrayLists.size()>1){ Toast.makeText(this,"当前有"+arrayLists.size()+"路线规划",Toast.LENGTH_SHORT).show(); } ArrayList lines = new ArrayList<>(); DecimalFormat df = new DecimalFormat("#.00"); double totalLength = 0 ; ArrayList arrayList = arrayLists.get(index % arrayLists.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)); totalLength +=Double.parseDouble(df.format(arrayList.get(i).distance)); System.out.println(arrayList.get(i)+" 距离:"+(i == arrayList.size() -1?"": TriangleUtils.getLengthOfSide(arrayList.get(i).point,arrayList.get(i+1).point)) +" "+arrayList.get(i).distance); } index++; if(animC){ planPathView.drawLine(lines); }else { animC = true; planPathView.drawAnimLine(lines, listener); } } public void refresh(View view) { planPathView.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(places,listener); } }