【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 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











최근 아이스 유니버스는 삼성의 차기 플래그십 스마트폰으로 널리 알려진 갤럭시 S25 울트라에 대한 세부 정보를 꾸준히 공개해 왔습니다. 무엇보다도 유출자는 삼성이 카메라 업그레이드를 하나만 가져올 계획이라고 주장했습니다.

OnLeaks는 이제 Android Headlines와 제휴하여 X(이전 Twitter) 팔로어로부터 4,000달러 이상의 수익을 창출하려는 시도가 실패한 지 며칠 후 Galaxy S25 Ultra에 대한 첫 번째 모습을 제공합니다. 맥락에 따라 h 아래에 포함된 렌더링 이미지

TCL은 두 가지 새로운 스마트폰을 발표하는 것과 함께 NXTPAPER 14라는 새로운 Android 태블릿도 발표했는데, TCL의 거대한 화면 크기는 판매 포인트 중 하나입니다. NXTPAPER 14는 TCL의 시그니처 브랜드인 무광택 LCD 패널 버전 3.0을 갖추고 있습니다.

최근 아이스 유니버스는 삼성의 차기 플래그십 스마트폰으로 널리 알려진 갤럭시 S25 울트라에 대한 세부 정보를 꾸준히 공개해 왔습니다. 무엇보다도 유출자는 삼성이 카메라 업그레이드를 하나만 가져올 계획이라고 주장했습니다.

Vivo Y300 Pro는 방금 완전히 공개되었으며 대용량 배터리를 갖춘 가장 얇은 중급 Android 휴대폰 중 하나입니다. 정확히 말하면 스마트폰의 두께는 7.69mm에 불과하지만 배터리 용량은 6,500mAh입니다. 최근 출시된 것과 동일한 용량이다.

Redmi Note 14 Pro Plus는 이제 작년 Redmi Note 13 Pro Plus(Amazon에서 현재 $375)의 직접적인 후속 제품으로 공식화되었습니다. 예상대로 Redmi Note 14 Pro Plus는 Redmi Note 14 및 Redmi Note 14 Pro와 함께 Redmi Note 14 시리즈를 주도합니다. 리

모토로라는 올해 셀 수 없이 많은 기기를 출시했지만 그 중 폴더블 기기는 2개뿐이었습니다. 맥락에 따라 전 세계 대부분이 Razr 50 및 Razr 50 Ultra로 쌍을 받았지만 Motorola는 북미에서 Razr 2024 및 Razr 2로 제공합니다.

삼성전자는 팬에디션(FE) 스마트폰 시리즈를 언제 업데이트할지 아직 힌트를 주지 않았다. 현재 상태로 Galaxy S23 FE는 2023년 10월 초에 출시된 회사의 최신 버전으로 남아 있습니다.
