Explanation of panoramio Implementation of the Ghost Map photo application in the android open-source code (6)

Source: Internet
Author: User

This document introduces viewimage. Java and viewmap. java. The former allows you to browse a single image, and the latter allows you to customize a map to display the image shooting location and the current location of the user. The activity interface implemented by viewimage is shown on the left. When you click the menu key of the mobile phone, the menu option interface is displayed on the right:

The layout file view_image.xml used by the above activity is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> </P> <p> <scrollview xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: fillviewport = "true"> </P> <p> <linearlayout <br/> Android: id = "@ + ID/content" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: gravity = "center_hori Zontal "<br/> </P> <p> <framelayout <br/> Android: layout_width =" fill_parent "<br/> Android: layout_height = "0dip" <br/> Android: layout_weight = "1" <br/> Android: padding = "10dip"> </P> <p> <linearlayout <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: layout_gravity = "center" <br/> Android: gravity = "center_horizontal" <br />></P> <p> <imageview <br/> Android: Id = "@ + ID/image" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: Background = "@ drawable/picture_frame" <br/> Android: scaletype = "fitcenter" <br/> Android: adjustviewbounds = "true" <br/> Android: maxheight = "320dip" <br/> Android: layout_alignparenttop = "true" <br/> Android: layout_centerhorizontal = "true" <br/> </P> <p> <te Xtview <br/> Android: Id = "@ + ID/Title" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: textsize = "20dip" <br/> Android: singleline = "true" <br/> Android: ellipsize = "end" <br/> Android: textcolor = "? Android: ATTR/textcolorprimary "<br/> Android: paddingleft =" 5dip "<br/> Android: paddingright =" 5dip "<br/> Android: layout_alignleft = "@ ID/image" <br/> Android: layout_alignright = "@ ID/image" <br/> Android: layout_below = "@ ID/image" <br/> </P> <p> <textview <br/> Android: id = "@ + ID/owner" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: textsize = "16dip" <br/> Android: singleline = "true" <br/> Android: ellipsize = "end" <br/> Android: textcolor = "? Android: ATTR/textcolorprimary "<br/> Android: paddingleft =" 5dip "<br/> Android: paddingright =" 5dip "<br/> Android: layout_alignleft = "@ ID/Title" <br/> Android: layout_alignright = "@ ID/Title" <br/> Android: layout_below = "@ ID/Title" <br/> </linearlayout> </P> <p> </framelayout> </P> <p> <textview <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" <br/> Android: textsize = "13 Dip "<br/> Android: textcolor = "? Android: ATTR/textcolorsecondary "<br/> Android: text =" @ string/Copyright "<br/> Android: gravity =" center "<br/> Android: paddingleft = "10dip" <br/> Android: paddingright = "10dip" <br/> </P> <p> </linearlayout> </P> <p> </scrollview>

Based on the URL and other information transmitted from imagelist, viewimage enables the background thread to load medium-size image information from the server. The UI components used mainly include menu options and prompt dialog boxes.

To implement menu options, you need to override the two functions of activity: oncreateoptionsmenu and onoptionsitemselected. The program framework is as follows:

Import android. app. activity; <br/> Import android. view. menu; <br/> Import android. view. menuitem; </P> <p> public class mainactivity extends activity {</P> <p> @ override <br/> Public Boolean oncreateoptionsmenu (menu) {<br/> // create menu options in this function <br/> return Super. oncreateoptionsmenu (menu); <br/>}</P> <p> @ override <br/> Public Boolean onoptionsitemselected (menuitem item) {<br/> // define the response event when the menu item is selected <br/> return Super. onoptionsitemselected (item); <br/>}</P> <p>}

To implement the dialog box, you must override the oncreatedialog function of the activity and create a dialog box in the function. Then, call the showdialog function of the activity where you want to display it to display the dialog box created in oncreatedialog. The viewimage implementation code is as follows:

Package COM. google. android. panoramio; </P> <p> Import COM. google. android. maps. geopoint; </P> <p> Import android. app. activity; <br/> Import android. app. alertdialog; <br/> Import android. app. dialog; <br/> Import android. content. activitynotfoundexception; <br/> Import android. content. intent; <br/> Import android. graphics. bitmap; <br/> Import android.net. uri; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. util. log; <br/> Import android. view. menu; <br/> Import android. view. menuitem; <br/> Import android. view. view; <br/> Import android. view. window; <br/> Import android. widget. imageview; <br/> Import android. widget. textview; </P> <p>/** <br/> * activity for displaying a single image <br/> */<br/> public class viewimage extends activity {<br/> private Static final string tag = "panoramio "; </P> <p> Private Static final int menu_radar = menu. first + 1; </P> <p> Private Static final int menu_map = menu. first + 2; </P> <p> Private Static final int menu_author = menu. first + 3; </P> <p> Private Static final int menu_view = menu. first + 4; </P> <p> Private Static final int dialog_no_radar = 1; </P> <p> panoramioitem mitem; </P> <p> private handler mhandler; </P> <p> private imageview mimage; </P> <p> private textview mtitle; </P> <p> private textview mowner; </P> <p> private view mcontent; </P> <p> private int mmapzoom; </P> <p> private int mmaplatitudee6; </P> <p> private int mmaplongitudee6; </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> requestwindowfeature (window. feature_indeterminate_progress); <br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. view_image); </P> <p> // information of the search region passed from imagelist <br/> intent I = getintent (); <br/> mitem = I. getparcelableextra (imagemanager. panoramio_item_extra); <br/> mmapzoom = I. getintextra (imagemanager. zoom_extra, integer. min_value); <br/> mmaplatitudee6 = I. getintextra (imagemanager. latitude_e6_extra, integer. min_value); <br/> mmaplongitudee6 = I. getintextra (imagemanager. longitude_e6_extra, integer. min_value); </P> <p> mhandler = new handler (); </P> <p> mcontent = findviewbyid (R. id. content); <br/> mimage = (imageview) findviewbyid (R. id. image); <br/> mtitle = (textview) findviewbyid (R. id. title); <br/> mowner = (textview) findviewbyid (R. id. owner); </P> <p> // set the content to invisible during initialization, and display the content after the background thread loads data <br/> mcontent. setvisibility (view. gone); <br/> getwindow (). setfeatureint (window. feature_indeterminate_progress, <br/> window. progress_visibility_on); <br/> New loadthread (). start (); <br/>}</P> <p> @ override <br/> Public Boolean oncreateoptionsmenu (menu) {<br/> super. oncreateoptionsmenu (menu); <br/> menu. add (0, menu_radar, 0, R. string. menu_radar) // Add a radar menu item <br/>. seticon (R. drawable. ic_menu_radar) <br/>. setalphabeticshortcut ('R'); <br/> menu. add (0, menu_map, 0, R. string. menu_map) // Add a map menu item <br/>. seticon (R. drawable. ic_menu_map) <br/>. setalphabeticshortcut ('M'); <br/> menu. add (0, menu_author, 0, R. string. menu_author) // Add the author information menu item <br/>. seticon (R. drawable. ic_menu_author) <br/>. setalphabeticshortcut ('A'); <br/> menu. add (0, menu_view, 0, R. string. menu_view) // Add a menu item for browsing images in the browser <br/>. seticon (Android. r. drawable. ic_menu_view) <br/>. setalphabeticshortcut ('V'); <br/> return true; <br/>}</P> <p> @ override <br/> Public Boolean onoptionsitemselected (menuitem item) {<br/> switch (item. getitemid () {<br/> case menu_radar: {<br/> // start the radar application <br/> intent I = new intent ("com. google. android. radar. show_radar "); <br/> geopoint location = mitem. getlocation (); <br/> I. putextra ("latitude", (float) (location. getlatitudee6 ()/000000f); <br/> I. putextra ("longpolling", (float) (location. getlongitudee6 ()/000000f); <br/> try {<br/> startactivity (I); <br/>} catch (activitynotfoundexception ex) {<br/> showdialog (dialog_no_radar); // The radar application does not exist. The prompt dialog box is displayed. <br/>}< br/> return true; <br/>}< br/> case menu_map: {<br/> // start the custom map viewmap <br/> intent I = new intent (this, viewmap. class); <br/> I. putextra (imagemanager. panoramio_item_extra, mitem); <br/> I. putextra (imagemanager. zoom_extra, mmapzoom); <br/> I. putextra (imagemanager. latitude_e6_extra, mmaplatitudee6); <br/> I. putextra (imagemanager. longitude_e6_extra, mmaplongitudee6); </P> <p> startactivity (I); </P> <p> return true; <br/>}< br/> case menu_author: {<br/> // display the author information in the browser <br/> intent I = new intent (intent. action_view); <br/> I. setdata (URI. parse (mitem. getownerurl (); <br/> startactivity (I); <br/> return true; <br/>}< br/> case menu_view: {<br/> // display the image information in the browser <br/> intent I = new intent (intent. action_view); <br/> I. setdata (URI. parse (mitem. getphotourl (); <br/> startactivity (I); <br/> return true; <br/>}</P> <p> return Super. onoptionsitemselected (item); <br/>}</P> <p> @ override <br/> protected dialog oncreatedialog (int id) {<br/> switch (ID) {<br/> case dialog_no_radar: <br/> // create a dialog box <br/> alertdialog. builder = new alertdialog. builder (this); <br/> return builder. settitle (R. string. no_radar_title) <br/>. setmessage (R. string. no_radar) <br/>. seticon (Android. r. drawable. ic_dialog_alert) <br/>. setpositivebutton (Android. r. string. OK, null ). create (); <br/>}< br/> return NULL; <br/>}</P> <p>/** <br/> * The thread used to load the medium size bitmap <br/> */<br/> private class loadthread extends thread {</P> <p> Public loadthread () {<br/>}</P> <p> @ override <br/> Public void run () {<br/> try {<br/> string uri = mitem. getthumburl (); <br/> uri = Uri. replace ("thumbnail", "medium"); // modify the image URL <br/> final bitmap B = bitmaputils. loadbitmap (URI); // loads a medium-sized bitmap </P> <p> // notifies the UI thread to change the interface <br/> mhandler. post (New runnable () {<br/> Public void run () {<br/> mimage. setimagebitmap (B); <br/> mtitle. settext (mitem. gettitle (); <br/> mowner. settext (mitem. getowner (); <br/> mcontent. setvisibility (view. visible); <br/> getwindow (). setfeatureint (window. feature_indeterminate_progress, <br/> window. progress_visibility_off); <br/>}< br/>}); <br/>}catch (exception e) {<br/> log. E (TAG, E. tostring (); <br/>}</P> <p>}

Viewmap inherits from mapactivity and is used to display the current user location and photo shooting location on a custom map. The following figure shows the implementation interface:

To use Google's map service in Android, You need to implement the mapview component (or implement mapactivity). When mapview is used in the program, you need to use it in androidmanifest. instructions for adding class libraries to the application tag of the XML file:

<Uses-library Android: Name = "com. Google. Android. Maps"/>

The following describes how to add a permission license at the same time:

<Uses-Permission Android: Name = "android. Permission. Internet"/>

The Code is as follows:

Package COM. google. android. panoramio; </P> <p> Import COM. google. android. maps. geopoint; <br/> Import COM. google. android. maps. mapactivity; <br/> Import COM. google. android. maps. mapcontroller; <br/> Import COM. google. android. maps. mapview; <br/> Import COM. google. android. maps. mylocationoverlay; <br/> Import COM. google. android. maps. overlay; <br/> Import COM. google. android. maps. projection; </P> <p> Import android. CO Ntent. intent; <br/> Import android. graphics. canvas; <br/> Import android. graphics. point; <br/> Import android. graphics. drawable. drawable; <br/> Import android. OS. bundle; <br/> Import android. view. gravity; <br/> Import android. view. view; <br/> Import android. view. viewgroup. layoutparams; <br/> Import android. widget. framelayout; </P> <p> Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p>/** <br/> * Custom map, showing the location of the photo and the current location of the user <br/> */<br/> public class viewmap extends mapactivity {<br/> private mapview mmapview; </P> <p> private mylocationoverlay mmylocationoverlay; </P> <p> arraylist <panoramioitem> mitems = NULL; </P> <p> private panoramioitem mitem; </P> <p> private drawable mmarker; // The Position of the photo. </P> <p> private int mmarkerxoffset; // X coordinate of the identified point </P> <p> private int mmarkeryoffset; // y coordinate of the identified point </P> <p> @ overrid E <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); </P> <p> // code implementation layout <br/> framelayout frame = new framelayout (this ); <br/> mmapview = new mapview (this, "mapviewcompassdemo_dummyapikey"); <br/> frame. addview (mmapview, <br/> New framelayout. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); <br/> setcontentview (FRAME); </P> <p> mmylocatio Noverlay = new mylocationoverlay (this, mmapview); </P> <p> // load the resource file <br/> mmarker = getresources (). getdrawable (R. drawable. map_pin); </P> <p> // set the boundary for mmarker to be drawn on overlay. <br/> final int intrinsicwidth = mmarker. getintrinsicwidth (); <br/> final int intrinsicheight = mmarker. getintrinsicheight (); <br/> mmarker. setbounds (0, 0, intrinsicwidth, intrinsicheight); </P> <p> mmarkerxoffset =-(intrinsicwidth/2); <Br/> mmarkeryoffset =-intrinsicheight; </P> <p> // obtain the data transmitted from viewimage. <br/> intent I = getintent (); <br/> mitem = I. getparcelableextra (imagemanager. panoramio_item_extra); <br/> int mapzoom = I. getintextra (imagemanager. zoom_extra, integer. min_value); <br/> int maplatitudee6 = I. getintextra (imagemanager. latitude_e6_extra, integer. min_value); <br/> int maplongitudee6 = I. getintextra (imagemanager. l Ongitude_e6_extra, integer. min_value); </P> <p> // Add two layers of overlay to mapview <br/> final list <overlay> overlays = mmapview. getoverlays (); <br/> overlays. add (mmylocationoverlay); <br/> overlays. add (New panoramiooverlay (); </P> <p> // sets the zoom level and center point of the mapview. <br/> final mapcontroller = mmapview. getcontroller (); <br/> If (mapzoom! = Integer. min_value & maplatitudee6! = Integer. min_value <br/> & maplongitudee6! = Integer. min_value) {<br/> controller. setzoom (mapzoom); <br/> controller. setcenter (New geopoint (maplatitudee6, maplongitudee6); <br/>}else {<br/> // locaiton acquisition failed, set the default zoom level to 15, add a runnable object to the Message Queue <br/> // when the location is fixed, the map will be moved to the specified center in the new thread <br/> controller. setzoom (15); <br/> mmylocationoverlay. runonfirstfix (New runnable () {<br/> Public void run () {<br/> controller. animateto (mmylocationoverlay. ge Tmylocation (); <br/>}< br/>}); <br/>}</P> <p> mmapview. setclickable (true); <br/> mmapview. setenabled (true); <br/> mmapview. setsatellite (true); <br/> addzoomcontrols (FRAME ); // Add the zoom control component to the Map <br/>}</P> <p> @ override <br/> protected void onresume () {<br/> super. onresume (); <br/> // try to enable location service. This function is registered as locationmanager. gps_provider and <br/> // locationmanager. network_provider observer <br/> mmylocationoverla Y. enablemylocation (); <br/>}</P> <p> @ override <br/> protected void onstop () {<br/> // stop location update <br/> mmylocationoverlay. disablemylocation (); <br/> super. onstop (); <br/>}</P> <p>/** <br/> * Get the zoom control component, and add it to the bottom of the mapview <br/> */<br/> @ suppresswarnings ("deprecation") <br/> private void addzoomcontrols (framelayout frame) {<br/> View zoomcontrols = mmapview. getzoomcontrols (); </P> <p> framelayout. lay Outparams P = <br/> New framelayout. layoutparams (layoutparams. wrap_content, <br/> layoutparams. wrap_content, gravity. bottom + gravity. center_horizontal); <br/> // place the map zoom component below the map mapview <br/> frame. addview (zoomcontrols, P); <br/>}</P> <p> @ override <br/> protected Boolean isroutedisplayed () {<br/> return false; // route information display is not supported <br/>}</P> <p>/** <br/> * display the overlay of the location where the photo is taken, and the resource of the dingtalk. <br/> * /<br/> public class Panoramiooverlay extends overlay {<br/> @ override <br/> Public void draw (canvas, mapview, Boolean shadow) {<br/> If (! Shadow) {<br/> point = new point (); <br/> // obtain the projection class, converts between pixel points on the screen and coordinate points <br/> projection P = mapview. getprojection (); <br/> // converts the latitude and longitude Point Information geopoint object to the coordinate point object in the mapview. <br/> P. topixels (mitem. getlocation (), point); <br/> super. draw (canvas, mapview, shadow); <br/> // draw the drawable object mmarker on the specified X and Y coordinates <br/> drawat (canvas, mmarker, point. X + mmarkerxoffset, point. Y + mmarkeryoffset, shadow); <br/>}</P> <p>}

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.