Define your own Android Baidu map zoom icon, you need to define a zoom control, to achieve the effect such as the following:
The zoom effect here enables the Click button to zoom in and out of the map, and also controls the available state of the zoom icon by gesture magnification and zoom out. Detailed implementations such as the following:
The first is the two XML configuration files under the Drawable folder:
Zoom_selector_in.xml
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" true "android:drawable=" @drawable/zoomin_press "/> < Item android:state_enabled= "false" android:drawable= "@drawable/zoomin_disable"/> <item android:drawable = "@drawable/zoomin_normal"/></selector>
Zoom_selector_out.xml
<?XML version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:state_pressed= "true" android:drawable= "@drawable/zoomout_press"/> <item Android:state_enabled= "false" android:drawable= "@drawable/zoomout_disable"/> <item android:drawable= "@ Drawable/zoomout_normal "/></selector>
Layout files in Zoom_controls_in_out.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 "> <button Android:id= "@+id/btn_zoom_in" android:layout_width= "32DP" android:layout_height= "32DP" android: background= "@drawable/zoom_selector_in"/> <button android:id= "@+id/btn_zoom_out" android: Layout_width= "32DP" android:layout_height= "32DP" android:background= "@drawable/zoom_selector_out"/ ></LinearLayout>
Master configuration file Main_activity.xml
<?XML version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <com.baidu.mapapi.map.mapview android:id= "@+id/mv_map" android:layout _width= "Match_parent" android:layout_height= "match_parent" android:clickable= "true"/> < Com.example.map.view.ZoomControlsView android:id= "@+id/zcv_zoom" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignparentright= "true" android:layout_ Alignparentbottom= "true" android:layout_marginbottom= "10DP" android:layout_marginright= "10DP"/> </RelativeLayout>
The relevant XML files are here. The following is the detailed implementation code:
Define the zoom control class yourself Zoomcontrolsview.java
Package Com.example.map.view;import Com.baidu.mapapi.map.baidumap;import Com.baidu.mapapi.map.mapstatus;import Com.baidu.mapapi.map.mapstatusupdatefactory;import Com.baidu.mapapi.map.mapview;import Com.example.map.activity.r;import Android.content.context;import Android.util.attributeset;import Android.widget.button;import Android.widget.linearlayout;import Android.view.layoutinflater;import Android.view.view;import Android.view.View.OnClickListener; public class Zoomcontrolsview extends LinearLayout implements onclicklistener{private Button inbtn;//Zoom buttonprivate Button outbtn;//Zoom Out buttonprivate baidumap baidumap;//Baidu Map Object Controller Private Mapstatus mapstatus;//Baidu map State private float minzoomlevel;//Map minimum level private float maxzoomlevel;//map maximum level public Zoomcontrolsview (context context, AttributeSet attrs) {Super (context, attrs, 0); init ();} Public Zoomcontrolsview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} /** * Initialize */private void init () {//Get layout view LinearLayout view= (linearlayout) layoutinflater.from (GetContext ()). Inflate (r.layout.zoom_controls_in_out, NULL);//Get Amplification BUTTONINBTN = (Button) View.findviewbyid (r.id.btn_zoom_in);//Get Zoom Out buttonoutbtn= (button) View.findviewbyid (r.id.btn_zoom_out); /Set Click event Inbtn.setonclicklistener (This), Outbtn.setonclicklistener (this);//Join Viewaddview (view);} @Overridepublic void OnClick (View v) {this.mapstatus=this.baidumap.getmapstatus ();//Get Map State switch (V.getid ()) {case r.id.btn_zoom_in://Change Map State this.baiduMap.setMapStatus (Mapstatusupdatefactory.zoomto (mapstatus.zoom+1)); Controlzoomshow ();//Change the zoom buttonbreak;case r.id.btn_zoom_out://Change the map state this.baiduMap.setMapStatus ( Mapstatusupdatefactory.zoomto (mapstatus.zoom-1)); Controlzoomshow ();//change the zoom Buttonbreak;default:break;} Get the status Mapstatus=this.baidumap.getmapstatus again ();} /** * Set map view * @param mapview */public void SetMapView (Mapview mapview) {//Get Baidu Map Controller this.baidumap=mapview.getmap ();// Set map gesture Event This.baiduMap.setOnMapStatusChangeListener (Onmapstatuschangelistener);//Get Baidu Map maximum minimum level maxzoomlevel=baIdumap.getmaxzoomlevel (); Minzoomlevel=baidumap.getminzoomlevel (); Controlzoomshow ();//Change Zoom button}/** * Control Zoom icon Display */ private void Controlzoomshow () {//Gets the current map state float zoom=this.baidumap.getmapstatus (). zoom;//assumes that the current state is greater than or equal to the maximum state of the map, Then the zoom button fails if (zoom>=maxzoomlevel) {inbtn.setbackgroundresource (r.drawable.zoomin_press); Inbtn.setenabled ( FALSE);} Else{inbtn.setbackgroundresource (r.drawable.zoom_selector_in); inbtn.setenabled (true);} If the current state is less than or equal to the minimum state of the map, the button invalidation if (zoom<=minzoomlevel) {Outbtn.setbackgroundresource (r.drawable.zoomout_press) is reduced. ); outbtn.setenabled (false);} Else{outbtn.setbackgroundresource (r.drawable.zoom_selector_out); outbtn.setenabled (True);}} /** * Map state Change related interface implementation */baidumap.onmapstatuschangelistener Onmapstatuschangelistener=new Baidumap.onmapstatuschangelistener () {/** * gesture operation map, setting map state and other actions cause the map state to change. * @param the status of status changes at the beginning of the map state * * @Overridepublic void Onmapstatuschangestart (Mapstatus arg0) {}/** * Map status Change end * @param St ATUs map State at the end of the map status change */@Overridepublic void Onmapstatuschangefinish (MaPStatus arg0) {}/** * Map status change * @param status Current map status */@Overridepublic void Onmapstatuschange (Mapstatus arg0) {Controlzoom Show ();}};}
Mainactivity.java:
Package Com.example.map.activity;import Com.baidu.mapapi.sdkinitializer;import Com.baidu.mapapi.map.BaiduMap; Import Com.baidu.mapapi.map.mapview;import Com.example.map.view.zoomcontrolsview;import android.app.Activity; Import Android.os.bundle;public class Mainactivity extends activity{private mapview mvmap;//Baidu map Control Private Baidumap baidumap;//Map Object Controller private zoomcontrolsview zcvzomm;//zoom control @overrideprotected void OnCreate (Bundle savedinstancestate {super.oncreate (savedinstancestate);//Must be sdkinitializer.initialize before Setcontentview (Getapplicationcontext ()); Setcontentview (r.layout.main_activity); Initmap ();//Initialize}/** * Initialize map */private void Initmap () {//Get map Control mvmap= (Mapview) Findviewbyid (R.id.mv_map); Mvmap.showzoomcontrols (false);//Hide zoom control//Get Map Object Controller baidumap=mvmap.getmap (); Baidumap.setbuildingsenabled (TRUE);//settings Display floor contour baidumap.setmapstatus (Mapstatusupdatefactory.zoomto (19f));//Set map status/ /Get Zoom control zcvzomm= (Zoomcontrolsview) Findviewbyid (r.id.zcv_zoom); Zcvzomm.setmapview (mvmap);//Set Baidu map control}@ overrideprotectedvoid OnPause () {super.onpause (); Mvmap.onpause ();} @Overrideprotected void Onresume () {super.onresume (); Mvmap.onresume ();} @Overrideprotected void OnDestroy () {Super.ondestroy (); Mvmap.ondestroy ();//Destroy Map}}
This allows you to implement your own defined zoom icon.
Android own definition Baidu map zoom icon