編輯:關於 Android 程式設計
WhirlyGlobe-Maply 是一套以 OpenGL ES 為底層、專為行動裝置設計的開源地圖工具包,同時支援 iOS 與 Android。本文以 Android 為示範平台,帶你從零建立一個能顯示球狀地球與平面地圖的專案。
示範環境:Windows 7 64 位、Android Studio 1.2.2,以及 WhirlyGlobe-Maply 官方壓縮包(見官網下載頁)。
開啟 Android Studio,新建名為 HelloEarth 的專案。
選擇要使用的 SDK 版本。
模板選擇 Empty Activity。
Activity 名稱保留預設的 MainActivity(也可自行更名),點擊 Finish 完成建立。
從下載的壓縮包中取出 app/libs/WhirlyGlobeMaply.aar,複製到專案的 app/libs 目錄下。
開啟 build.gradle(Project: HelloEarth),在 allprojects 中加入 flatDir 目錄宣告,讓 Gradle 能找到本地 aar:
再開啟 build.gradle(Module: app),在 dependencies 中補上對 aar 的依賴:
右鍵點擊 MainActivity,選擇新增一個空白 Fragment,命名為 HelloGlobeFragment,並取消勾選三個自動產生的選項。
修改 activity_main.xml,把預設的 Hello World 文字框註解掉,改為掛載 Fragment:
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:name="com.example.administrator.helloearth.HelloGlobeFragment" />
</RelativeLayout>
接著改寫 HelloGlobeFragment.java。關鍵在於覆寫 chooseDisplayType() 回傳 Globe,並在 controlHasStarted() 裡掛上瓦片圖層:
package com.example.administrator.helloearth;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mousebird.maply.GlobeMapFragment;
import com.mousebird.maply.QuadImageTileLayer;
import com.mousebird.maply.RemoteTileInfo;
import com.mousebird.maply.RemoteTileSource;
import com.mousebird.maply.SphericalMercatorCoordSystem;
import java.io.File;
public class HelloGlobeFragment extends GlobeMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle inState) {
super.onCreateView(inflater, container, inState);
return baseControl.getContentView();
}
@Override
protected MapDisplayType chooseDisplayType() {
return MapDisplayType.Globe;
}
@Override
protected void controlHasStarted() {
// 設定瓦片地圖來源,並指定本地快取目錄
String cacheDirName = "stamen_watercolor";
File cacheDir = new File(getActivity().getCacheDir(), cacheDirName);
cacheDir.mkdir();
RemoteTileSource remoteTileSource = new RemoteTileSource(
new RemoteTileInfo("https://tile.stamen.com/watercolor/", "png", 0, 18));
remoteTileSource.setCacheDir(cacheDir);
SphericalMercatorCoordSystem coordSystem = new SphericalMercatorCoordSystem();
// 地球模式下使用 globeControl;平面地圖模式下則改用 mapControl
QuadImageTileLayer baseLayer = new QuadImageTileLayer(globeControl, coordSystem, remoteTileSource);
baseLayer.setImageDepth(1);
baseLayer.setSingleLevelLoading(false);
baseLayer.setUseTargetZoomLevel(false);
baseLayer.setCoverPoles(true);
baseLayer.setHandleEdges(true);
globeControl.addLayer(baseLayer);
// 定位到指定經緯度並設定初始高度
globeControl.animatePositionGeo(-3.6704803, 40.5023056, 5, 1.0);
}
}
運行後即可看到可雙擊放大的三維地球。
流程與地球類似:新增一個名為 HelloMapFragment 的空白 Fragment,並把 activity_main.xml 中的 fragment name 換掉(同時可把上一段地球版 Fragment 的宣告註解保留)。
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:name="com.example.administrator.helloearth.HelloMapFragment" />
</RelativeLayout>
HelloMapFragment.java 的差異只有兩處:chooseDisplayType() 回傳 Map,以及改用 mapControl 來加圖層與定位,並可開放旋轉手勢。
package com.example.administrator.helloearth;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mousebird.maply.GlobeMapFragment;
import com.mousebird.maply.QuadImageTileLayer;
import com.mousebird.maply.RemoteTileInfo;
import com.mousebird.maply.RemoteTileSource;
import com.mousebird.maply.SphericalMercatorCoordSystem;
import java.io.File;
public class HelloMapFragment extends GlobeMapFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle inState) {
super.onCreateView(inflater, container, inState);
return baseControl.getContentView();
}
@Override
protected MapDisplayType chooseDisplayType() {
return MapDisplayType.Map;
}
@Override
protected void controlHasStarted() {
String cacheDirName = "stamen_watercolor";
File cacheDir = new File(getActivity().getCacheDir(), cacheDirName);
cacheDir.mkdir();
RemoteTileSource remoteTileSource = new RemoteTileSource(
new RemoteTileInfo("https://tile.stamen.com/watercolor/", "png", 0, 18));
remoteTileSource.setCacheDir(cacheDir);
SphericalMercatorCoordSystem coordSystem = new SphericalMercatorCoordSystem();
QuadImageTileLayer baseLayer = new QuadImageTileLayer(mapControl, coordSystem, remoteTileSource);
baseLayer.setImageDepth(1);
baseLayer.setSingleLevelLoading(false);
baseLayer.setUseTargetZoomLevel(false);
baseLayer.setCoverPoles(true);
baseLayer.setHandleEdges(true);
mapControl.addLayer(baseLayer);
mapControl.animatePositionGeo(103.994067, 30.770697, 5, 1.0);
mapControl.setAllowRotateGesture(true);
}
}
成果如下,同樣支援雙擊放大。
以上兩種顯示模式共用同一套圖層邏輯,差別只在於 control 的選擇與 displayType 的設定,因此後續要擴充其他投影方式或自訂圖層時,沿用同樣的結構即可。
Android 沉浸式狀態欄與懸浮 Tab 效果實作解析
許多電商或內容型應用的詳情頁,都會使用類似效果:頁面上方是圖片或內容區,當使用者向上滑動時,Tab 會懸浮在頂部,方便隨時切換。這種設計能提升操作效率,也讓畫面更有層次感
Android 自定義 ViewPager 滑動行為:控制左右切換與頁面顯示
為何要自定義 ViewPager 滑動 標準 ViewPager 預設允許左右滑動切換頁面,但實際專案常有特殊需求。例如某個頁面需要禁止向右滑動,或第三個頁面只能透過按鈕
Android 圓形圖片控件:RoundImageView 自訂實作
圓形頭像是很常見的需求。雖然有現成的圖形處理函式庫可用,但自己刻一個自訂 View 更能掌握細節——比方說還能加上雙層圓框。 RoundImageView.java
AndroidManifest 配置文件詳解
本質上,AndroidManifest.xml 是整個應用的主配置清單文件,包含應用的包名、版本號、組件、權限等信息;作用則是記錄該應用的相關配置。 一、全局配置