From 60df583cc43c99f3ce366d04b15b92964da21c26 Mon Sep 17 00:00:00 2001 From: Runt <qingingrunt2010@qq.com> Date: Sat, 26 Dec 2020 11:27:06 +0000 Subject: [PATCH] 加載框 --- app/src/main/java/com/demo/navtogether/MainActivity.java | 77 ++++++++++++++++++++++++++++---------- 1 files changed, 57 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/demo/navtogether/MainActivity.java b/app/src/main/java/com/demo/navtogether/MainActivity.java index 3475c61..9b2d2f4 100644 --- a/app/src/main/java/com/demo/navtogether/MainActivity.java +++ b/app/src/main/java/com/demo/navtogether/MainActivity.java @@ -2,6 +2,7 @@ import androidx.appcompat.app.AppCompatActivity; +import android.app.ProgressDialog; import android.graphics.Point; import android.os.Bundle; import android.view.View; @@ -37,14 +38,7 @@ int height = planPathView.getMeasuredHeight(); int width = planPathView.getMeasuredWidth(); - ArrayList<PathPlan.Place> 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.setPoints(randomList(width,height)); planPathView.getViewTreeObserver().removeOnPreDrawListener(this); return true; @@ -66,6 +60,28 @@ btnDrawC.setEnabled(true); } }; + + /** + * 创建随机位置 + * @param width 范围宽度 + * @param height 范围高度 + * @return + */ + public ArrayList<PathPlan.Place> randomList(int width,int height){ + + ArrayList<PathPlan.Place> 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; + } /** * 创建随机位置 @@ -106,7 +122,7 @@ } int index = 0 ; - + ArrayList<ArrayList<PathPlan.Place>> customLists; /** * 自定义算法画线 * @param view @@ -114,19 +130,40 @@ public void drawLineCustom(View view){ planPathView.clearLines(); System.out.println("drawLineCustom " ); - HashMap<PathPlan.Place, PathPlan.Distance> distanceHashMap = PathPlan.intPoints(planPathView.getPoints()); - ArrayList<ArrayList<PathPlan.Place>> arrayLists = new DijkstraUtils(distanceHashMap).initCustomPathPlan(planPathView.getPoints().get(2), null); - if(arrayLists.size()>1){ - Toast.makeText(this,"当前有"+arrayLists.size()+"路线规划",Toast.LENGTH_SHORT).show(); + if(customLists == null || customLists.size() == 0) { + ProgressDialog progressDialog = new ProgressDialog(this); + progressDialog.setMessage("正在規劃路綫"); + progressDialog.show(); + new Thread(new Runnable() { + @Override + public void run() { + + HashMap<PathPlan.Place, PathPlan.Distance> 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<PlanPathView.Line> lines = new ArrayList<>(); - DecimalFormat df = new DecimalFormat("#.00"); - double totalLength = 0 ; - ArrayList<PathPlan.Place> arrayList = arrayLists.get(index % arrayLists.size()); + ArrayList<PathPlan.Place> 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)); - 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){ @@ -137,9 +174,9 @@ } } - public void refresh(View view) { planPathView.clear(); + customLists.clear(); int width = planPathView.getWidth(); int height = planPathView.getHeight(); ArrayList<PathPlan.Place> places = new ArrayList<>(); @@ -153,6 +190,6 @@ places.add(new PathPlan.Place("H",random(width,height),0)); animD = false; animC = false; - planPathView.setAnimPoints(places,listener); + planPathView.setAnimPoints(randomList(width,height),listener); } } \ No newline at end of file -- Gitblit v1.9.1