(三)高德地圖之自定義縮放及縮放動畫效果
阿新 • • 發佈:2018-11-26
這一節主要實現的功能是地圖的自定義縮放及縮放的動畫效果,還是直接放上程式碼更直觀些,主要部位裡面基本有註解
還是老樣子,首先是新建activity_zoom_animate.xml佈局檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <CheckBox android:id="@+id/animate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:textOff="動畫" android:textOn="動畫" /> </LinearLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:padding="5dp" android:orientation="horizontal" > <Button android:id="@+id/zoom_in" android:layout_width="40dip" android:layout_height="40dip" android:layout_alignParentLeft="true" android:textSize="20sp" android:gravity="center" android:text="+" /> <Button android:id="@+id/zoom_out" android:layout_width="40dip" android:layout_height="40dip" android:layout_toRightOf="@id/zoom_in" android:layout_marginLeft="5dp" android:textSize="20sp" android:gravity="center" android:text="-" /> </RelativeLayout> </RelativeLayout> <com.amap.api.maps.MapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
然後是建立類檔案Zoom_Animate.java
package com.junto.gdmaptest.activity; import android.app.Activity; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import com.amap.api.maps.AMap; import com.amap.api.maps.CameraUpdate; import com.amap.api.maps.CameraUpdateFactory; import com.amap.api.maps.MapView; import com.junto.gdmaptest.R; /** * Created by WangJinyong on 2018/10/22. * 地圖自定義縮放及縮放動畫效果 */ public class Zoom_AnimateActivity extends Activity implements View.OnClickListener { MapView mapView = null; AMap aMap; CheckBox animate;//是否新增動畫 Button zoomIn,zoomOut;//+ - @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zoom_animate); //獲取地圖控制元件引用 mapView = findViewById(R.id.map); //在activity執行onCreat時執行mapView.onCreate(savedInstanceState),建立地圖,必需要寫的 mapView.onCreate(savedInstanceState); initView(); } @Override protected void onResume() { super.onResume(); //在activity執行onResume時執行mMapView.onResume (),重新繪製載入地圖 mapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity執行onPause時執行mMapView.onPause (),暫停地圖的繪製 mapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); //在activity執行onDestroy時執行mMapView.onDestroy(),銷燬地圖 mapView.onDestroy(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //在activity執行onSaveInstanceState時執行mMapView.onSaveInstanceState (outState),儲存地圖當前的狀態 mapView.onSaveInstanceState(outState); } private void initView() { //初始化AMap物件 if (aMap == null) { aMap = mapView.getMap(); } animate = findViewById(R.id.animate);//是否新增動畫 animate.setOnClickListener(this); zoomIn = findViewById(R.id.zoom_in);//+ zoomIn.setOnClickListener(this); zoomOut = findViewById(R.id.zoom_out);//- zoomOut.setOnClickListener(this); } /** * 根據動畫按鈕狀態,呼叫函式animateCamera或moveCamera來改變可視區域 */ private void changeCamera(CameraUpdate update, AMap.CancelableCallback callback) { boolean animated = animate.isChecked(); if (animated) { aMap.animateCamera(update, 1000, callback);//1000是動畫時間 } else { aMap.moveCamera(update); } } @Override public void onClick(View view) { switch (view.getId()){ case R.id.zoom_in://點選地圖放大按鈕(+)響應事件 changeCamera(CameraUpdateFactory.zoomIn(), null); break; case R.id.zoom_out://點選地圖縮小按鈕(-)響應事件 changeCamera(CameraUpdateFactory.zoomOut(), null); break; } } }
通過上面的程式碼就能實現地圖的自定義縮放和縮放的動畫效果了,這些都是可控的。
下一節再接著介紹地圖定位的幾種模式