继续用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 MapViewContext context) {
supercontext);
}
public MapViewContext context, @Nullable AttributeSet attrs) {
supercontext, attrs);
initcontext);
}
private void initContext context) {
this.mContext=context;
this.paint=new Paint);
thread.start);
}
private Thread thread=new Threadnew Runnable) {//在子线程
@Override
public void run) {
InputStream inputStream = mContext.getResources).openRawResourceR.raw.china);//获取svg文件
DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance);//对svg进行解析
try {
DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder);
Document document = documentBuilder.parseinputStream);//获取到了svg
Element documentElement = document.getDocumentElement);//获取根节点
NodeList nodeList = document.getElementsByTagName"path");//获取子节点
float left=-1,top=-1,right=-1,bottom=-1;
forint i=0;i<nodeList.getLength);i++){
Element element= Element) nodeList.itemi);
String pathdata = element.getAttribute"android:pathData");
String shen=element.getAttribute"android:provice");
@SuppressLint"RestrictedApi") Path path= PathParser.createPathFromPathDatapathdata);
PathItem pathItem=new PathItempath);
pathItem.setNameshen);
quanbandao dao= quanbandao.getInstancegetContext));
// Log.dTestApp,shen);
int num=0;
num=dao.findrenshen);
Log.dTestApp,""+num);
ifnum>=12){
pathItem.setColorcolors[0]);
}
else ifnum<12&&num>=5){
pathItem.setColorcolors[1]);
}
else ifnum<5&&num>0){
pathItem.setColorcolors[2]);
}
else ifnum==0){
pathItem.setColorcolors[3]);
}
pathItemList.addpathItem);
// RectF rectF=new RectF);
// path.computeBoundsrectF,true);
//
// left=left==-1?rectF.left:Math.minrectF.left,left);
// right=right==-1?rectF.right:Math.minrectF.right,right);
// bottom=bottom==-1?rectF.bottom:Math.minrectF.bottom,bottom);
// top=top==-1?rectF.top:Math.minrectF.top,top);
}
handler.sendEmptyMessage1);//数据处理完成可以进行画
} catch Exception e) {//此处要抛出两处异常,这样写抛出所有异常
e.printStackTrace);
}
}
});
private Handler handler=new Handler){
@Override
public void handleMessage@NonNull Message msg) {
super.handleMessagemsg);
invalidate);//刷新
}
};
@Override
protected void onDrawCanvas canvas) {
quanbandao dao= quanbandao.getInstancegetContext));
super.onDrawcanvas);
forPathItem pathItem:pathItemList){
pathItem.drawItemcanvas,paint,false);
}
ifselectPathItem!=null){
selectPathItem.drawItemcanvas,paint,true);
Toast.makeTextgetContext), selectPathItem.getName)+"省上报体温正常"+dao.findrenselectPathItem.getName))+"人,未上报人数0人,体温异常人数0人", Toast.LENGTH_SHORT).show);
}
}
@Override
public boolean onTouchEventMotionEvent event) {
onTouchevent.getX),event.getY));
return super.onTouchEventevent);
}
private void onTouchfloat x, float y) {
forPathItem pathItem:pathItemList){
if pathItem.isTouchx,y)){
selectPathItem=pathItem;
}
}
ifselectPathItem!=null){
invalidate);
}
}
}此操作相当于自己定义一个view控件,在此控件文件中进行设定及数据操作,然后将此view控件写在activity中进行显示。