继续用svg来实现中国地图,先要找svg格式的中国地图,然后按找Javaweb的形式传入需要的对象集,实现点击地图某处显示高亮并弹出相应的数据具体代码如下:
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.PathParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import db.dao.quanbandao;
public class MapView extends View {
private static final String TestApp="TestApp";
private Paint paint;
private Context mContext;
private List<PathItem> pathItemList=new ArrayList<>();
private int[] colors = new int[]{Color.RED,Color.YELLOW,Color.GRAY, Color.GREEN};
private PathItem selectPathItem;
public MapView(Context context) {
super(context);
}
public MapView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
this.mContext=context;
this.paint=new Paint();
thread.start();
}
private Thread thread=new Thread(new Runnable() {//在子线程
@Override
public void run() {
InputStream inputStream = mContext.getResources().openRawResource(R.raw.china);//获取svg文件
DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();//对svg进行解析
try {
DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(inputStream);//获取到了svg
Element documentElement = document.getDocumentElement();//获取根节点
NodeList nodeList = document.getElementsByTagName("path");//获取子节点
float left=-1,top=-1,right=-1,bottom=-1;
for(int i=0;i<nodeList.getLength();i++){
Element element= (Element) nodeList.item(i);
String pathdata = element.getAttribute("android:pathData");
String shen=element.getAttribute("android:provice");
@SuppressLint("RestrictedApi") Path path= PathParser.createPathFromPathData(pathdata);
PathItem pathItem=new PathItem(path);
pathItem.setName(shen);
quanbandao dao= quanbandao.getInstance(getContext());
// Log.d(TestApp,shen);
int num=0;
num=dao.findren(shen);
Log.d(TestApp,""+num);
if(num>=12){
pathItem.setColor(colors[0]);
}
else if(num<12&&num>=5){
pathItem.setColor(colors[1]);
}
else if(num<5&&num>0){
pathItem.setColor(colors[2]);
}
else if(num==0){
pathItem.setColor(colors[3]);
}
pathItemList.add(pathItem);
// RectF rectF=new RectF();
// path.computeBounds(rectF,true);
//
// left=left==-1?rectF.left:Math.min(rectF.left,left);
// right=right==-1?rectF.right:Math.min(rectF.right,right);
// bottom=bottom==-1?rectF.bottom:Math.min(rectF.bottom,bottom);
// top=top==-1?rectF.top:Math.min(rectF.top,top);
}
handler.sendEmptyMessage(1);//数据处理完成可以进行画
} catch (Exception e) {//此处要抛出两处异常,这样写抛出所有异常
e.printStackTrace();
}
}
});
private Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
invalidate();//刷新
}
};
@Override
protected void onDraw(Canvas canvas) {
quanbandao dao= quanbandao.getInstance(getContext());
super.onDraw(canvas);
for(PathItem pathItem:pathItemList){
pathItem.drawItem(canvas,paint,false);
}
if(selectPathItem!=null){
selectPathItem.drawItem(canvas,paint,true);
Toast.makeText(getContext(), selectPathItem.getName()+"省上报体温正常"+dao.findren(selectPathItem.getName())+"人,未上报人数0人,体温异常人数0人", Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
onTouch(event.getX(),event.getY());
return super.onTouchEvent(event);
}
private void onTouch(float x, float y) {
for(PathItem pathItem:pathItemList){
if( pathItem.isTouch(x,y)){
selectPathItem=pathItem;
}
}
if(selectPathItem!=null){
invalidate();
}
}
}此操作相当于自己定义一个view控件,在此控件文件中进行设定及数据操作,然后将此view控件写在activity中进行显示。