【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系统工程师,然后在面试的时候对方的技术总监问了我一 ...
随机推荐
- Appium Python 四:怎样获取APP的Package以及Activity
看到一篇很好的博客:[Android测试][随笔]获得App的包名和启动页Activity 除了博客上的方法,我还找到两种方法: 方法一:aapt 前提需要使用SDK Manager.exe 下载 A ...
- Java从零开始学十五(继承)
一.继承作用 继承使用复用以前的代码非常容易,能够大大的缩短开发周期,降低开发成本,同时增加程序的易维护性 继承使重一个类A能够直接使用另外一个类B的属性和方法的一种途径 类A可以有自己的属性和方法 ...
- Using QuickExec
Fiddler's QuickExec box allows you to launch script-commands quickly. Keyboard Shortcuts Hit ALT+Q t ...
- NGINX原理 之 SLAB分配机制(转)
1 引言 众所周知,操作系统使用伙伴系统管理内存,不仅会造成大量的内存碎片,同时处理效率也较低下.SLAB是一种内存管理机制,其拥有较高的处理效率,同时也有效的避免内存碎片的产生,其核心思想是预分配. ...
- &和&&的一点区别
&两边的语句都会执行,&&有可能只执行左边的语句. &做位运算与的时候可以更高效的判断一个数字是不是奇数. 1&1 = 1 0&1 = 0 所以和1与结 ...
- 头文件dirent.h
<dirent.h>是POSIX.1标准定义的unix类目录操作的头文件,包含了许多UNIX系统服务的函数原型,例如opendir函数.readdir函数. opendir函数: DI ...
- 关于LoadRunner的迭代
通过用lr做负载压力测试过程发现,如果设定不同的action迭代次数,每次得出的结果是不同的,曲线的表现形式也是不同的.这点就使我们会感觉困惑,为什么要设置action的迭代次数?以及对于不同的应用系 ...
- php替换str_replace的使用方法,支持多个替换
废话不多说,直接上代码: str_replace(['a','b','c'],'a',$str);//a或b或c都替换成a str_replace(['a','b','c'],['d','e','f' ...
- 将数据库dbcp连接池改为c3p0连接池(草稿,别点)
D:\Develop\apache-tomcat-7.0.52\bin\catalina.bat run[2017-12-26 02:31:29,174] Artifact pers:war expl ...
- HDUOJ ----1709
The Balance Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...