【Android】第3章(25)示例24--OpenGL绘制功能
分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04 一、简介 百度地图SDK为广大开发者开放了OpenGL绘制接口,帮助开发者在地图上实现更灵活的样式绘制,丰富地图使用效果体验。 二、运行截图 简介:介绍如何使用OpenGL在地图上实现自定义绘制
分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04
一、简介
百度地图SDK为广大开发者开放了OpenGL绘制接口,帮助开发者在地图上实现更灵活的样式绘制,丰富地图使用效果体验。
二、运行截图
简介:介绍如何使用OpenGL在地图上实现自定义绘制。
详述:
(1)利用OpenGL绘制基本折线;
(2)利用OpenGL在地图上进行纹理绘制;
本示例运行截图如下:
三、设计步骤
1、添加demo24_opengl.xml文件
在layout文件夹下添加该文件,然后将代码改为下面的内容:
<span style="color: #0000ff;"></span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span> <span style="color: #0000ff;"><span style="color: #800000;">RelativeLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;"> android:orientation</span><span style="color: #0000ff;">="vertical"</span> <span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">com.baidu.mapapi.map.TextureMapView </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/bmapView"</span><span style="color: #ff0000;"> android:layout_width</span><span style="color: #0000ff;">="match_parent"</span><span style="color: #ff0000;"> android:layout_height</span><span style="color: #0000ff;">="fill_parent"</span> <span style="color: #0000ff;">/></span> <span style="color: #0000ff;"></span><span style="color: #800000;">RelativeLayout</span><span style="color: #0000ff;">></span></span></span>
2、添加Demo24OpenGL.cs文件
在SrcSdkDemos文件夹下添加该文件,然后将代码改为下面的内容:
<span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.App; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.OS; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Com.Baidu.Mapapi.Map; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Com.Baidu.Mapapi.Model; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Graphics; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Util; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Collections.Generic; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> javax.Microedition.Khronos.Opengles; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Java.Nio; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Opengl; </span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> BdMapV371Demos.SrcSdkDemos { </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 此demo用来展示如何在地图绘制的每帧中再额外绘制一些用户自己的内容 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></span> [Activity(Label = <span style="color: #800000;">"</span><span style="color: #800000;">@string/demo_name_opengl</span><span style="color: #800000;">"</span><span style="color: #000000;">)] </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Demo24OpenGL : Activity, BaiduMap.IOnMapDrawFrameCallback { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 地图相关</span> <span style="color: #0000ff;">PRivate</span><span style="color: #000000;"> TextureMapView mMapView; </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> BaiduMap mBaiduMap; </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> Bitmap bitmap; </span><span style="color: #0000ff;">private</span> LatLng latlng1 = <span style="color: #0000ff;">new</span> LatLng(<span style="color: #800080;">39.97923</span>, <span style="color: #800080;">116.357428</span><span style="color: #000000;">); </span><span style="color: #0000ff;">private</span> LatLng latlng2 = <span style="color: #0000ff;">new</span> LatLng(<span style="color: #800080;">39.94923</span>, <span style="color: #800080;">116.397428</span><span style="color: #000000;">); </span><span style="color: #0000ff;">private</span> LatLng latlng3 = <span style="color: #0000ff;">new</span> LatLng(<span style="color: #800080;">39.96923</span>, <span style="color: #800080;">116.437428</span><span style="color: #000000;">); </span><span style="color: #0000ff;">private</span> IList<latlng><span style="color: #000000;"> latLngPolygon; </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">float</span><span style="color: #000000;">[] vertexs; </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> FloatBuffer vertexBuffer; </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span> textureId = -<span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">readonly</span> <span style="color: #0000ff;">string</span> LTAG = <span style="color: #800000;">"</span><span style="color: #800000;">Demo24OpenGL</span><span style="color: #800000;">"</span><span style="color: #000000;">; </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnCreate(Bundle savedInstanceState) { </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnCreate(savedInstanceState); SetContentView(Resource.Layout.demo24_opengl); mMapView </span>= FindViewById<texturemapview><span style="color: #000000;">(Resource.Id.bmapView); mBaiduMap </span>=<span style="color: #000000;"> mMapView.Map; latLngPolygon </span>= <span style="color: #0000ff;">new</span> List<latlng><span style="color: #000000;">() { latlng1,latlng2,latlng3 }; mBaiduMap.SetOnMapDrawFrameCallback(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">); bitmap </span>=<span style="color: #000000;"> BitmapFactory.DecodeResource(Resources, Resource.Drawable.ground_overlay); } </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnPause() { mMapView.OnPause(); </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnPause(); } </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnResume() { mMapView.OnResume(); textureId </span>= -<span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnResume(); } </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnDestroy() { mMapView.OnDestroy(); </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnDestroy(); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnMapDrawFrame(IGL10 gl, MapStatus drawingMapStatus) { </span><span style="color: #0000ff;">if</span> (mBaiduMap.Projection != <span style="color: #0000ff;">null</span><span style="color: #000000;">) { calPolylinePoint(drawingMapStatus); drawPolyline(gl, Color.Argb(</span><span style="color: #800080;">255</span>, <span style="color: #800080;">255</span>, <span style="color: #800080;">0</span>, <span style="color: #800080;">0</span>), vertexBuffer, <span style="color: #800080;">10</span>, <span style="color: #800080;">3</span><span style="color: #000000;">, drawingMapStatus); drawTexture(gl, bitmap, drawingMapStatus); } } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> calPolylinePoint(MapStatus mspStatus) { PointF[] polyPoints </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> PointF[latLngPolygon.Count]; vertexs </span>= <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">float</span>[<span style="color: #800080;">3</span> *<span style="color: #000000;"> latLngPolygon.Count]; </span><span style="color: #0000ff;">int</span> i = <span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">foreach</span> (LatLng xy <span style="color: #0000ff;">in</span><span style="color: #000000;"> latLngPolygon) { polyPoints[i] </span>=<span style="color: #000000;"> mBaiduMap.Projection.ToOpenGLLocation(xy, mspStatus); vertexs[i </span>* <span style="color: #800080;">3</span>] =<span style="color: #000000;"> polyPoints[i].X; vertexs[i </span>* <span style="color: #800080;">3</span> + <span style="color: #800080;">1</span>] =<span style="color: #000000;"> polyPoints[i].Y; vertexs[i </span>* <span style="color: #800080;">3</span> + <span style="color: #800080;">2</span>] = <span style="color: #800080;">0.0f</span><span style="color: #000000;">; i</span>++<span style="color: #000000;">; } </span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> j = <span style="color: #800080;">0</span>; j ) { Log.Debug(LTAG, <span style="color: #800000;">"</span><span style="color: #800000;">vertexs[</span><span style="color: #800000;">"</span> + j + <span style="color: #800000;">"</span><span style="color: #800000;">]: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> vertexs[j]); } vertexBuffer </span>=<span style="color: #000000;"> makeFloatBuffer(vertexs); } </span><span style="color: #0000ff;">private</span> FloatBuffer makeFloatBuffer(<span style="color: #0000ff;">float</span><span style="color: #000000;">[] fs) { ByteBuffer bb </span>= ByteBuffer.AllocateDirect(fs.Length * <span style="color: #800080;">4</span><span style="color: #000000;">); bb.Order(ByteOrder.NativeOrder()); FloatBuffer fb </span>=<span style="color: #000000;"> bb.AsFloatBuffer(); fb.Put(fs); fb.Position(</span><span style="color: #800080;">0</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> fb; } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">void</span> drawPolyline(IGL10 gl, <span style="color: #0000ff;">int</span><span style="color: #000000;"> color, FloatBuffer lineVertexBuffer, </span><span style="color: #0000ff;">float</span> lineWidth, <span style="color: #0000ff;">int</span><span style="color: #000000;"> pointSize, MapStatus drawingMapStatus) { gl.GlEnable(GL10.GlBlend); gl.GlEnableClientState(GL10.GlVertexArray); gl.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha); </span><span style="color: #0000ff;">float</span> colorA = Color.GetAlphaComponent(color) /<span style="color: #000000;"> 255f; </span><span style="color: #0000ff;">float</span> colorR = Color.GetRedComponent(color) /<span style="color: #000000;"> 255f; </span><span style="color: #0000ff;">float</span> colorG = Color.GetGreenComponent(color) /<span style="color: #000000;"> 255f; </span><span style="color: #0000ff;">float</span> colorB = Color.GetBlueComponent(color) /<span style="color: #000000;"> 255f; gl.GlVertexPointer(</span><span style="color: #800080;">3</span>, GL10.GlFloat, <span style="color: #800080;">0</span><span style="color: #000000;">, lineVertexBuffer); gl.GlColor4f(colorR, colorG, colorB, colorA); gl.GlLineWidth(lineWidth); gl.GlDrawArrays(GL10.GlLineStrip, </span><span style="color: #800080;">0</span><span style="color: #000000;">, pointSize); gl.GlDisable(GL10.GlBlend); gl.GlDisableClientState(GL10.GlVertexArray); } </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 使用opengl坐标绘制 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="gl"></span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="bitmap"></span> <span style="color: #808080;">///</span> <span style="color: #808080;"><param name="drawingMapStatus"></span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> drawTexture(IGL10 gl, Bitmap bitmap, MapStatus drawingMapStatus) { PointF p1 </span>=<span style="color: #000000;"> mBaiduMap.Projection.ToOpenGLLocation(latlng2, drawingMapStatus); PointF p2 </span>=<span style="color: #000000;"> mBaiduMap.Projection.ToOpenGLLocation(latlng3, drawingMapStatus); ByteBuffer byteBuffer </span>= ByteBuffer.AllocateDirect(<span style="color: #800080;">4</span> * <span style="color: #800080;">3</span> * <span style="color: #800080;">4</span><span style="color: #000000;">); byteBuffer.Order(ByteOrder.NativeOrder()); FloatBuffer vertices </span>=<span style="color: #000000;"> byteBuffer.AsFloatBuffer(); vertices.Put(</span><span style="color: #0000ff;">new</span> <span style="color: #0000ff;">float</span>[] { p1.X, p1.Y, <span style="color: #800080;">0.0f</span>, p2.X, p1.Y, <span style="color: #800080;">0.0f</span><span style="color: #000000;">, p1.X, p2.Y, </span><span style="color: #800080;">0.0f</span>, p2.X, p2.Y, <span style="color: #800080;">0.0f</span><span style="color: #000000;"> }); ByteBuffer indicesBuffer </span>= ByteBuffer.AllocateDirect(<span style="color: #800080;">6</span> * <span style="color: #800080;">2</span><span style="color: #000000;">); indicesBuffer.Order(ByteOrder.NativeOrder()); ShortBuffer indices </span>=<span style="color: #000000;"> indicesBuffer.AsShortBuffer(); indices.Put(</span><span style="color: #0000ff;">new</span> <span style="color: #0000ff;">short</span>[] { <span style="color: #800080;">0</span>, <span style="color: #800080;">1</span>, <span style="color: #800080;">2</span>, <span style="color: #800080;">1</span>, <span style="color: #800080;">2</span>, <span style="color: #800080;">3</span><span style="color: #000000;"> }); ByteBuffer textureBuffer </span>= ByteBuffer.AllocateDirect(<span style="color: #800080;">4</span> * <span style="color: #800080;">2</span> * <span style="color: #800080;">4</span><span style="color: #000000;">); textureBuffer.Order(ByteOrder.NativeOrder()); FloatBuffer texture </span>=<span style="color: #000000;"> textureBuffer.AsFloatBuffer(); texture.Put(</span><span style="color: #0000ff;">new</span> <span style="color: #0000ff;">float</span>[] { <span style="color: #800080;">0</span><span style="color: #000000;">, 1f, 1f, 1f, 0f, 0f, 1f, 0f }); indices.Position(</span><span style="color: #800080;">0</span><span style="color: #000000;">); vertices.Position(</span><span style="color: #800080;">0</span><span style="color: #000000;">); texture.Position(</span><span style="color: #800080;">0</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 生成纹理</span> <span style="color: #0000ff;">if</span> (textureId == -<span style="color: #800080;">1</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">int</span>[] textureIds = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">int</span>[<span style="color: #800080;">1</span><span style="color: #000000;">]; gl.GlGenTextures(</span><span style="color: #800080;">1</span>, textureIds, <span style="color: #800080;">0</span><span style="color: #000000;">); textureId </span>= textureIds[<span style="color: #800080;">0</span><span style="color: #000000;">]; Log.Debug(LTAG, </span><span style="color: #800000;">"</span><span style="color: #800000;">textureId: </span><span style="color: #800000;">"</span> +<span style="color: #000000;"> textureId); gl.GlBindTexture(GL10.GlTexture2d, textureId); GLUtils.TexImage2D(GL10.GlTexture2d, </span><span style="color: #800080;">0</span>, bitmap, <span style="color: #800080;">0</span><span style="color: #000000;">); gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureMinFilter, GL10.GlNearest); gl.GlTexParameterf(GL10.GlTexture2d, GL10.GlTextureMagFilter, GL10.GlNearest); gl.GlBindTexture(GL10.GlTexture2d, </span><span style="color: #800080;">0</span><span style="color: #000000;">); } gl.GlEnable(GL10.GlTexture2d); gl.GlEnableClientState(GL10.GlVertexArray); gl.GlEnableClientState(GL10.GlTextureCoordArray); gl.GlEnable(GL10.GlBlend); gl.GlBlendFunc(GL10.GlSrcAlpha, GL10.GlOneMinusSrcAlpha); gl.GlColor4f(</span><span style="color: #800080;">1.0f</span>, <span style="color: #800080;">1.0f</span>, <span style="color: #800080;">1.0f</span>, <span style="color: #800080;">1.0f</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 绑定纹理ID</span> <span style="color: #000000;"> gl.GlBindTexture(GL10.GlTexture2d, textureId); gl.GlVertexPointer(</span><span style="color: #800080;">3</span>, GL10.GlFloat, <span style="color: #800080;">0</span><span style="color: #000000;">, vertices); gl.GlTexCoordPointer(</span><span style="color: #800080;">2</span>, GL10.GlFloat, <span style="color: #800080;">0</span><span style="color: #000000;">, texture); gl.GlDrawElements(GL10.GlTriangleStrip, </span><span style="color: #800080;">6</span><span style="color: #000000;">, GL10.GlUnsignedShort, indices); gl.GlDisable(GL10.GlTexture2d); gl.GlDisableClientState(GL10.GlVertexArray); gl.GlDisableClientState(GL10.GlTextureCoordArray); gl.GlDisable(GL10.GlBlend); } } }</span></latlng></texturemapview></latlng>
3、修改MainActivity.cs文件
在MainActivity.cs文件的demos字段定义中,去掉【示例24】下面的注释。
运行观察效果。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

OnLeaks 现在与 Android Headlines 合作,首次展示了 Galaxy S25 Ultra,几天前,他试图从他的 X(以前的 Twitter)粉丝那里筹集到 4,000 美元以上的资金,但失败了。对于上下文,嵌入在 h 下面的渲染图像

除了发布两款新智能手机外,TCL 还发布了一款名为 NXTPAPER 14 的新 Android 平板电脑,其大屏幕尺寸是其卖点之一。 NXTPAPER 14 采用 TCL 标志性品牌哑光液晶面板 3.0 版本

最近几天,Ice Universe 不断披露有关 Galaxy S25 Ultra 的详细信息,人们普遍认为这款手机将是三星的下一款旗舰智能手机。除此之外,泄密者声称三星只计划升级一款相机

Vivo Y300 Pro刚刚全面亮相,它是最薄的中端Android手机之一,配备大电池。准确来说,这款智能手机的厚度仅为 7.69 毫米,但配备了 6,500 mAh 的电池。这与最近推出的容量相同

Redmi Note 14 Pro Plus 现已正式成为去年 Redmi Note 13 Pro Plus 的直接后继产品(亚马逊售价 375 美元)。正如预期的那样,Redmi Note 14 Pro Plus与Redmi Note 14和Redmi Note 14 Pro一起成为Redmi Note 14系列的主角。李

摩托罗拉今年发布了无数设备,尽管其中只有两款是可折叠的。就上下文而言,虽然世界上大多数地区都收到了 Razr 50 和 Razr 50 Ultra,但摩托罗拉在北美提供了 Razr 2024 和 Razr 2

三星尚未就何时更新其 Fan Edition (FE) 智能手机系列提供任何提示。目前来看,Galaxy S23 FE 仍然是该公司的最新版本,于 2023 年 10 月年初推出。
