【Android】3.18 示例18--自定义绘制功能
分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04
简介:介绍自定义绘制点、线、多边形、圆等几何图形和文字
详述:
(1)支持绘制凸多边形,如要绘制凹多边形请用三角形进行拼接;
(2)支持绘制文字;
一、运行截图
本示例运行截图如下:

二、设计步骤
1、添加Demo18_geometry.xml文件
在layout文件夹下添加该文件,然后将代码改为下面的内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:checked="false"
android:text="虚线绘制"
android:id="@+id/dottedline" /> <Button
android:id="@+id/btnReset"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="10dip"
android:layout_weight="1.0"
android:text="绘制" /> <Button
android:id="@+id/btnClear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="10dip"
android:layout_weight="1.0"
android:text="清除" />
</LinearLayout> <com.baidu.mapapi.map.TextureMapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" /> </LinearLayout>
2、添加Demo18Geometry.cs文件
在SrcSdkDemos文件夹下添加该文件,然后将其内容改为下面的代码:
using System.Collections.Generic;
using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Graphics;
using Android.Widget;
using Com.Baidu.Mapapi.Map;
using Com.Baidu.Mapapi.Model;
using Java.Lang;
namespace BdMapV371Demos.SrcSdkDemos
{
/// <summary>
/// 此demo用来展示如何在地图上用GraphicsOverlay添加点、线、多边形、圆,同时展示如何在地图上用TextOverlay添加文字
/// </summary>
[Activity(Label = "@string/demo_name_geometry",
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden,
ScreenOrientation = ScreenOrientation.Sensor)]
public class Demo18Geometry : Activity
{
TextureMapView mMapView;
BaiduMap mBaiduMap;
Polyline mPolyline;
Polyline mColorfulPolyline;
Polyline mTexturePolyline;
BitmapDescriptor mRedTexture = BitmapDescriptorFactory.FromAsset("icon1_road_red_arrow.png");
BitmapDescriptor mBlueTexture = BitmapDescriptorFactory.FromAsset("icon1_road_blue_arrow.png");
BitmapDescriptor mGreenTexture = BitmapDescriptorFactory.FromAsset("icon1_road_green_arrow.png"); protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.demo18_geometry); // 初始化地图
mMapView = FindViewById<TextureMapView>(Resource.Id.bmapView);
mBaiduMap = mMapView.Map; // UI初始化
Button resetBtn = FindViewById<Button>(Resource.Id.btnReset);
resetBtn.Click += delegate
{
// 添加绘制元素
AddCustomElementsDemo();
};
Button clearBtn = FindViewById<Button>(Resource.Id.btnClear);
clearBtn.Click += delegate
{
// 清除所有图层
mMapView.Map.Clear();
};
CheckBox dottedline = FindViewById<CheckBox>(Resource.Id.dottedline);
dottedline.CheckedChange += delegate
{
if (mPolyline == null) return;
if (dottedline.Checked) mPolyline.DottedLine = true;
else mPolyline.DottedLine = false;
};
} /// <summary>
/// 添加点、线、多边形、圆、文字
/// </summary>
public void AddCustomElementsDemo()
{
// 添加普通折线绘制
LatLng p1 = new LatLng(39.97923, 116.357428);
LatLng p2 = new LatLng(39.94923, 116.397428);
LatLng p3 = new LatLng(39.97923, 116.437428);
IList<LatLng> points = new List<LatLng>();
points.Add(p1);
points.Add(p2);
points.Add(p3);
OverlayOptions ooPolyline = new PolylineOptions()
.InvokeWidth()
.InvokeColor(Color.ParseColor("#AAFF0000").ToArgb())
.InvokePoints(points);
mPolyline = (Polyline)mBaiduMap.AddOverlay(ooPolyline); // 添加多颜色分段的折线绘制
LatLng p11 = new LatLng(39.965, 116.444);
LatLng p21 = new LatLng(39.925, 116.494);
LatLng p31 = new LatLng(39.955, 116.534);
LatLng p41 = new LatLng(39.905, 116.594);
LatLng p51 = new LatLng(39.965, 116.644);
IList<LatLng> points1 = new List<LatLng>();
points1.Add(p11);
points1.Add(p21);
points1.Add(p31);
points1.Add(p41);
points1.Add(p51);
IList<Integer> colorValue = new List<Integer>();
colorValue.Add(Integer.ValueOf(Color.ParseColor("#AAFF0000").ToArgb()));
colorValue.Add(Integer.ValueOf(Color.ParseColor("#AA00FF00").ToArgb()));
colorValue.Add(Integer.ValueOf(Color.ParseColor("#AA0000FF").ToArgb()));
OverlayOptions ooPolyline1 = new PolylineOptions()
.InvokeWidth()
.InvokeColor(Color.ParseColor("#AAFF0000").ToArgb())
.InvokePoints(points1)
.ColorsValues(colorValue);
mColorfulPolyline = (Polyline)mBaiduMap.AddOverlay(ooPolyline1); // 添加多纹理分段的折线绘制
LatLng p111 = new LatLng(39.865, 116.444);
LatLng p211 = new LatLng(39.825, 116.494);
LatLng p311 = new LatLng(39.855, 116.534);
LatLng p411 = new LatLng(39.805, 116.594);
IList<LatLng> points11 = new List<LatLng>();
points11.Add(p111);
points11.Add(p211);
points11.Add(p311);
points11.Add(p411);
IList<BitmapDescriptor> textureList = new List<BitmapDescriptor>();
textureList.Add(mRedTexture);
textureList.Add(mBlueTexture);
textureList.Add(mGreenTexture);
IList<Integer> textureIndexs = new List<Integer>();
textureIndexs.Add(Integer.ValueOf());
textureIndexs.Add(Integer.ValueOf());
textureIndexs.Add(Integer.ValueOf());
OverlayOptions ooPolyline11 = new PolylineOptions()
.InvokeWidth()
.InvokePoints(points11)
.DottedLine(true)
.InvokeCustomTextureList(textureList)
.TextureIndex(textureIndexs);
mTexturePolyline = (Polyline)mBaiduMap.AddOverlay(ooPolyline11); // 添加弧线
OverlayOptions ooArc = new ArcOptions()
.InvokeColor(Color.ParseColor("#AA00FF00").ToArgb())
.InvokeWidth()
.Points(p1, p2, p3);
mBaiduMap.AddOverlay(ooArc); // 添加圆
LatLng llCircle = new LatLng(39.90923, 116.447428);
OverlayOptions overlayCircle = new CircleOptions()
.InvokeFillColor(0x000000FF)
.InvokeCenter(llCircle)
.InvokeStroke(new Stroke(, Color.ParseColor("#AA000000").ToArgb()))
.InvokeRadius();
mBaiduMap.AddOverlay(overlayCircle); LatLng llDot = new LatLng(39.98923, 116.397428);
OverlayOptions ooDot = new DotOptions()
.InvokeCenter(llDot)
.InvokeRadius()
.InvokeColor(Color.ParseColor("#FF0000FF").ToArgb());
mBaiduMap.AddOverlay(ooDot); // 添加多边形
LatLng pt1 = new LatLng(39.93923, 116.357428);
LatLng pt2 = new LatLng(39.91923, 116.327428);
LatLng pt3 = new LatLng(39.89923, 116.347428);
LatLng pt4 = new LatLng(39.89923, 116.367428);
LatLng pt5 = new LatLng(39.91923, 116.387428);
IList<LatLng> pts = new List<LatLng>();
pts.Add(pt1);
pts.Add(pt2);
pts.Add(pt3);
pts.Add(pt4);
pts.Add(pt5);
OverlayOptions polygon = new PolygonOptions()
.InvokePoints(pts)
.InvokeStroke(new Stroke(, Color.ParseColor("#AA00FF00").ToArgb()))
.InvokeFillColor(Color.ParseColor("#AAFFFF00").ToArgb());
mBaiduMap.AddOverlay(polygon); // 添加文字
LatLng t = new LatLng(39.86923, 116.397428);
OverlayOptions t1 = new TextOptions()
.InvokeBgColor(Color.ParseColor("#AAFFFF00").ToArgb())
.InvokeFontSize()
.InvokeFontColor(Color.ParseColor("#FFFF00FF").ToArgb())
.InvokeText("百度地图SDK")
.InvokeRotate(-)
.InvokePosition(t);
mBaiduMap.AddOverlay(t1);
} protected override void OnPause()
{
mMapView.OnPause();
base.OnPause();
} protected override void OnResume()
{
mMapView.OnResume();
base.OnResume();
} protected override void OnDestroy()
{
mMapView.OnDestroy();
mRedTexture.Recycle();
mBlueTexture.Recycle();
mGreenTexture.Recycle();
base.OnDestroy();
} }
}
3、修改MainActivity.cs
在MainActivity.cs文件的demos字段定义中,去掉【示例18】下面的注释。
运行观察结果。
【Android】3.18 示例18--自定义绘制功能的更多相关文章
- 【Android】3.24 示例24--OpenGL绘制功能
分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 百度地图SDK为广大开发者开放了OpenGL绘制接口,帮助开发者在地图上实现更灵活的样式绘制,丰富地 ...
- Android图片加载框架最全解析(六),探究Glide的自定义模块功能
不知不觉中,我们的Glide系列教程已经到了第六篇了,距离第一篇Glide的基本用法发布已经过去了半年的时间.在这半年中,我们通过用法讲解和源码分析配合学习的方式,将Glide的方方面面都研究了个遍, ...
- 【转】Android编码规范建议18条
转自:http://www.chinaz.com/design/2015/0908/443732.shtml Android编码规范建议18条 适合手机app设计师和android 工程师阅读. 1. ...
- 18 UI美化自定义主题样式代码
自定义主题 假设我们我们对现有的样式不大满意 那么可在工程目录res/values下的styles.xml自定义 方法: 1. res/values下的styles.xml文件中自定义一个标签 < ...
- 【Android Training UI】创建自定义Views(Lesson 2 - 自定义Drawing)
发布在我的网站:http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-2 ...
- Android使用Fragment来实现ViewPager的功能(解决切换Fragment状态不保存)以及各个Fragment之间的通信
以下内容为原创,转载请注明:http://www.cnblogs.com/tiantianbyconan/p/3364728.html 我前两天写过一篇博客<Android使用Fragment来 ...
- 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)
发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...
- Android端IM应用中的@人功能实现:仿微博、QQ、微信,零入侵、高可扩展
本文由“猫爸iYao”原创分享,感谢作者. 1.引言 最近有个需求:评论@人(没错,就是IM聊天或者微博APP里的@人功能),就像下图这样: ▲ 微信群聊界面里的@人功能 ▲ QQ群聊界面里 ...
- Android下添加新的自定义键值和按键处理流程【转】
本文转载自: Android下添加新的自定义键值和按键处理流程 说出来不怕大家笑话,我写这篇博客的原因在于前几天去一个小公司面试Android系统工程师,然后在面试的时候对方的技术总监问了我一 ...
随机推荐
- STL - 容器 - MultiSet
MultiSet根据特定排序准则,自动将元素排序.MultiSet允许元素重复.一些常规操作:MultiSetTest.cpp #include <iostream> #include & ...
- Electron 入门案例1
1:package.json 通过npm init生成package.json文件,内容如下: { "name": "t02", "version&q ...
- 建立与读取.ini文件
一般读写ini文件被读写Registry所取代,但我们还是可以透过 win31的传统方式读写ini文件,以存程式目前的相关设定,而於下一次程式执行时再 读回来.目前建议使用GetSetting Sav ...
- vue 源码 断点调试
1.添加sourceMap sourceMap: true 2.npm run dev 会生成vue.js.map 3.断点调试 <!DOCTYPE html> <html> ...
- vue 数据劫持 响应式原理 Observer Dep Watcher
1.vue响应式原理流程图概览 2.具体流程 (1)vue示例初始化(源码位于instance/index.js) import { initMixin } from './init' import ...
- Linux系统Domino704升级为901 64位的步骤及注意事项
[背景] 随便系统业务量的不断增大,应用数据库越来越多.与第三方接口的需求越来越多.文档量越来越多,32位的domino对server的利用率已无法满足系统需求的日益增长,低版本号的domino ...
- jquery获取td所在的行和列
今天在做项目时.遇到一个须要获取第几行第几列的问题. 后来.网上找了找资料,整理了此文.(使用jquery的preAll()获取列) 代码例如以下: <!DOCTYPE html PUBLIC ...
- 〖Android〗OK6410a的Android HAL层代码编写笔记
一.编写LED灯的Linux驱动程序代码 之所以使用存在HAL层,是为了保护对硬件驱动过程的逻辑与原理: 所以,残留在Linux驱动层的代码,只保留了基本的读写操作,而不含有关键的逻辑思维: 1. l ...
- CentOS 6.5上使用gdb调试时出现Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686 .
在CentOS6.5上用gdb调试时提示Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.i686先修改 ...
- HDUOJ------1058 Humble Numbers
Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...