3月17日
繼續用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中進行顯示。
總結
- 上一篇: 学生管理系统(SSM简易版)总结
- 下一篇: SAP Fiori Elements 应