在Android比较复杂的界面就要使用SVG比较简单
使用步骤
1. 下载台湾地图的SVG
2. 用这个网站 将svg资源转换成相应的 Android代码
3. 利用Xml解析SVG的代码 封装成javaBean 最重要的得到Path
4. 重写OnDraw方法 利用Path绘制台湾地图
5. 重写OnTouchEvent方法,记录手指触摸位置,判断这个位置是否坐落在某个省份上
Path的分析
- M = moveto(M X,Y) :将画笔移动到指定的坐标位置,相当于 android Path 里的moveTo()
- L = lineto(L X,Y) :画直线到指定的坐标位置,相当于 android Path 里的lineTo()
- H = horizontal lineto(H X):画水平线到指定的X坐标位置
- V = vertical lineto(V Y):画垂直线到指定的Y坐标位置
- C = curveto(C X1,Y1,X2,Y2,ENDX,ENDY):三次贝赛曲线
- S = smooth curveto(S X2,Y2,ENDX,ENDY) 同样三次贝塞尔曲线,更平滑
- Q = quadratic Belzier curve(Q X,Y,ENDX,ENDY):二次贝赛曲线
- T = smooth quadratic Belzier curveto(T ENDX,ENDY):映射 同样二次贝塞尔曲线,更平滑
- A = elliptical Arc(A RX,RY,XROTATION,FLAG1,FLAG2,X,Y):弧线 ,相当于arcTo()
- Z = closepath():关闭路径(会自动绘制链接起点和终点)
android:pathData="M541.02,336.29L541.71,336.09
L542.54,337.38L543.77,338.27
L543.53,338.58L545.92,338.99.8,350.12L561.12,349.61L562.97,349.6L563.89,349.94
L563.48,350.21L563.6,351.15L562.98,351.84L562.99,353.94L562.28,353.68L562.06,3
53.97L561.87,355.49L561.13,355.88L561.38,356.41L560.77,357.72L561.33,357.73
。。。。。
L562.06,359L563.49,358.5L563.75,357.85L564.17,358.09L564.64
,361.19L565.52,361.68L564.51,362.21L564.67,363.38L565.17,363.21L565.35,364.41
L566.19,364.53L566.23,365.29L567.26,365L568.99,365.25L569.63,364.91
L539.3,337.63L539.84,336.78L540.31,336.88z" />
[颜色16进制地址] (http://www.cnblogs.com/zhxiaomiao/archive/2010/07/29/1787604.html)
**颜色要标准16进制**
**下面就是画台湾地图**
package com.math.activity.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceActivity;
import android.provider.DocumentsContract;
import android.support.annotation.Nullable;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import com.math.activity.R;
import com.math.activity.utils.PathParser;
import com.math.activity.utils.ProvinceItem;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
/**
* 作者 : songli
* 时间 : 2017/6/1 15:21
* QQ : 2734030745
* 邮箱 :15850774503@163.com
*/
public class MapView extends View {
private static final String TAG = “MapView”;
private Context context;
private List itemList = new ArrayList();
private int[] colorArray = new int[]{0xFF000000, 0xFFFF0000, 0x80800000, 0x80000000};
private Paint paint;
/**
* 每一个省份
*/
private ProvinceItem selectItem;
//放大陪数
private float scale = 1.3f;
private GestureDetectorCompat gestureDetectorCompat;
public MapView(Context context) {
this(context, null);
}
public MapView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
this.context = context;
paint = new Paint();
paint.setAntiAlias(true);
//加载SVG数据
loadDateHhread.start();
gestureDetectorCompat = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onDown(MotionEvent e) {
handlerTouch(e.getX(), e.getY());
return super.onDown(e);
}
});
}
/**
* 处理触摸事件的方法
* @param x
* @param y
*/
private void handlerTouch(float x, float y) {
if (itemList != null){
ProvinceItem temp = null;
for (ProvinceItem item :itemList){
//放大系数
if (item.isTouch((int)(x / scale),(int)(y / scale))){
temp = item;
break;
}
}
if (temp != null){
selectItem = temp;
postInvalidate();
}
}
}
Thread loadDateHhread = new Thread() {
@Override
public void run() {
//拿到svg图片流
InputStream inputStream = context.getResources().openRawResource(R.raw.taiwan);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
Document doc = builder.parse(inputStream);
//xml的path解析
Element rootElement = doc.getDocumentElement();
NodeList items = rootElement.getElementsByTagName("path");
for (int i = 0; i < items.getLength(); i++) {
Element element = (Element) items.item(i);
String pathData = element.getAttribute("android:pathData");
Log.d("tuch", pathData);
//拿到path 封装ProvinceItem中
Path path = PathParser.createPathFromPathData(pathData);
ProvinceItem provinceItem = new ProvinceItem(path);
itemList.add(provinceItem);
handler.sendEmptyMessage(1);
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
};
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (itemList != null) {
int totalNumber = itemList.size();
// Log.d(TAG, "次数:" + totalNumber);
for (int i = 0; i < totalNumber; i++) {
/*int color = Color.WHITE;*/
int color = Color.YELLOW;
int flag = i;
Log.d(TAG, "次数:" + i);
//分配颜色
switch (flag) {
case 1:
case 5:
case 9:
case 13:
case 17:
case 20:
Log.d(TAG, "颜色:" + flag);
color = colorArray[0];
break;
case 2:
case 6:
case 10:
case 14:
case 18:
case 21:
Log.d(TAG, "颜色:" + flag);
color = colorArray[1];
break;
case 3:
case 7:
case 11:
case 15:
case 19:
case 22:
color = colorArray[2];
Log.d(TAG, "颜色:" + flag);
break;
case 4:
case 8:
case 12:
case 16:
case 0:
case 23:
color = colorArray[3];
Log.d(TAG, "颜色:" + flag);
break;
default:
color = Color.BLACK;
break;
}
itemList.get(i).setDrawColor(color);
}
postInvalidate();
}
}
};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (itemList != null) {
canvas.save();
canvas.scale(scale, scale);
for (ProvinceItem provinceItem : itemList) {
//绘制未被选择
if (provinceItem != selectItem) {
provinceItem.draw(canvas, paint, false);
}
}
//绘制选择的
if (selectItem != null) {
selectItem.draw(canvas, paint, true);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//将事件转交给
return gestureDetectorCompat.onTouchEvent(event);
}
}
**用一个工具类**
package com.math.activity.utils;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
/**
* 一个区
* 作者 : songli
* 时间 : 2017/6/1 15:31
* QQ : 2734030745
* 邮箱 :15850774503@163.com
*/
public class ProvinceItem {
// 路径
private Path path;
//绘制颜色
private int drawColor;
public ProvinceItem(Path path) {
this.path = path;
}
/**
* @param canvas
* @param paint
* @param isSelect
*/
public void draw(Canvas canvas, Paint paint, boolean isSelect) {
if (isSelect) {
//绘制背景
paint.setStrokeWidth(2);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL);
paint.setShadowLayer(8, 0, 0, 0xfff);
//path
canvas.drawPath(path, paint);
//绘制省份
paint.clearShadowLayer();
paint.setColor(drawColor);
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(2);
canvas.drawPath(path, paint);
} else {
//没有被选择
paint.clearShadowLayer();
;
//绘制内容
paint.setStrokeWidth(1);
paint.setStyle(Paint.Style.FILL);
paint.setColor(drawColor);
canvas.drawPath(path, paint);
//绘制边境线
paint.setStyle(Paint.Style.STROKE);
paint.setColor(0xff00e8f4);
canvas.drawPath(path, paint);
}
}
/**
* 是否在区上
* @param x
* @param y
* @return
*/
public boolean isTouch(int x, int y){
RectF rectF = new RectF();
//
path.computeBounds(rectF, true);
Region region = new Region();
region.setPath(path, new Region((int)rectF.left, (int)rectF.right, (int)rectF.top, (int)rectF.bottom));
return region.contains(x,y);
}
public Path getPath() {
return path;
}
public void setPath(Path path) {
this.path = path;
}
public int getDrawColor() {
return drawColor;
}
public void setDrawColor(int drawColor) {
this.drawColor = drawColor;
}
}
“`