前言
近期由于涉及到导航相关的内容,需要涉及到最短寻址,就浅浅的研究了一下相关的算法,比较出名的就是A*算法和D算法,本篇文章我就自己写的一个demo的例子,来分享一下个人对这个算法的实践。
什么是A*算法
A* (A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法,也是解决许多搜索问题的有效算法。 上面当然是百度百科的解释,但我个人理解而言,就是一种寻找最短路径的算法,但效率上不是最快的。
package com.mio.testhndemo.ui.view;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.mio.testhndemo.R;
public class GridItemView extends FrameLayout {
private TextView topLeftTextView;
private TextView bottomLeftTextView;
private TextView bottomRightTextView;
private ConstraintLayout clRoot;
public GridItemView(Context context) {
this(context, null);
}
public GridItemView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public GridItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.grid_item_layout, this, true);
topLeftTextView = view.findViewById(R.id.top_left_text);
bottomLeftTextView = view.findViewById(R.id.bottom_left_text);
bottomRightTextView = view.findViewById(R.id.bottom_right_text);
clRoot = view.findViewById(R.id.cl_root);
}
@Override
public void setBackgroundColor(int color) {
clRoot.setBackgroundColor(color);
}
public void setTopLeftText(String text) {
topLeftTextView.setText(text);
}
public void setBottomLeftText(String text) {
bottomLeftTextView.setText(text);
}
public void setBottomRightText(String text) {
bottomRightTextView.setText(text);
}
}
自定义viewgroup:
效果
缺陷
代码中没考虑没路的情况,如果走不到就没了...后续优化吧...