Android Custom Baidu Map zoom icon

Source: Internet
Author: User

Custom implementation of the Android Baidu Map zoom icon, you need to customize a zoom control, the effect is as follows:

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. The specific implementation is as follows:

The first is the two XML configuration files under the drawable directory:

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:o rientation= "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 implementation code:

Custom Zoom Control class 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 button Private button outbtn;//Zoom button Private Baidumap baidumap;//Baidu Map Object Controller Private Mapstatus mapstatus;//Baidu map State private float minzoomlevel;// Map minimum level private float maxzoomlevel;//map max 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 Magnification button inbtn= (button) View.findviewbyid (r.id.btn_zoom_in);//Get Zoom Out button outbtn= (button) View.findviewbyid (r.id.btn_zoom_out);// Set Click event Inbtn.setonclicklistener (This), Outbtn.setonclicklistener (this);//Add 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 button break;case r.id.btn_zoom_out://change the map state this.baiduMap.setMapStatus ( Mapstatusupdatefactory.zoomto (mapstatus.zoom-1)); Controlzoomshow ();//Change the zoom button break;default:break;} Regain status Mapstatus=this.baidumap.getmapstatus ();} /** * 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 of float zoom=this.baidumap.getmapstatus (). zoom;//If the current state is greater than or equal to the maximum state of the map, then the Magnify 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 Zoom Out button fails if (zoom<=minzoomlevel) {outbtn.setbackgroundresource (r.drawable.zoomout_press); o Utbtn.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 start changing. * @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 the 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) {controlzoomshow ();}};} 

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 a custom zoom icon.



Android Custom Baidu Map zoom icon

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.