In android, overlay pops up bubbles with tails.

Source: Internet
Author: User


It looks like above.
In this process, I encountered two problems:
1: How to Make a bubble View with a tail
2: how to add this View to MapView.
1: How to Make a bubble View with a tail
I implemented it using the background image. Of course, normal PNG will be distorted when the View is scaled, especially the sharp tail.
Later, you can use the SDK \ Tools \ draw9patch to complete a Png file in 9.png format. bat to process, as long as the side of the ordinary png can be marked, the specific draw9patch. I won't talk about how to use bat here. There are a lot of documents on the Internet and you will find them by yourself.
The 9.png we generated is like the following. Pay attention to the black lines around it. It is the identifier of 9png during stretching.

With this png, you can directly put it in the res/drawable directory of your project,
Then, create the xml file of your view in the res/layout directory, for example, overlay_pop.xml. Mine is like this:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: background = "@ drawable/bubble_background" <! --Here the 9.png -->
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: paddingLeft = "5px"
Android: paddingTop = "5px"
Android: paddingRight = "5px"
Android: paddingBottom = "20px" <! -- Add padding. Otherwise, the content in the view will be painted on the border. -->
>
<TextView android: id = "@ + id/map_bubbleTitle"
Android: ellipsize = "marquee"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: gravity = "center_horizontal"
Android: singleLine = "true"
/> <! -- Style can be absent. Here, the first TextView represents the title and uses a large font. -->
<TextView android: id = "@ + id/map_bubbleText"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: singleLine = "false"
/> <! -- Style may not be available. Here, the second TextView indicates the description, which uses a large font -->
</LinearLayout>
In this way, popView is created.
2: how to add this View to MapView.
Click a position in the mapView to bring up the popView.
Or click an Overlay to bring up the popView. Here, click Overlay to describe it,
Overlay has the onTap () method. You can implement your own overlay and overideonTap () methods. popView is displayed,
You can also use setOnFocusChangeListener () to bring up popView in listener ,.
I use listener, because setOnFocusChangeListener is triggered when the focus is lost. I can hide the popView when the focus is lost.
MapView is inherited from ViewGroup. Therefore, MapView has the addView () method and MapView. LayoutParams.
MapView. LayoutParams can be located based on GeoPoint. I used this feature to locate the pop-up popView.
PointItemizedOverlay overlay = new PointItemizedOverlay (drawable); <! -- This is the overlay that I inherited from ItemizedOverlay. It is mainly used to draw an image and write a name. It is very simple. No specific code is posted here. -->
Public class BaseMapActivity extends MapActivity {
/**
* Map View
*/
Protected MapView mapView;

/**
* Pop-up bubble View
*/
Private View popView;
/**
Listener
*/
Private final ItemizedOverlay. OnFocusChangeListener onFocusChangeListener = new ItemizedOverlay. OnFocusChangeListener (){

@ Override
Public void onFocusChanged (ItemizedOverlay overlay, OverlayItem newFocus ){
// Create a bubble window
 

If (popView! = Null ){
PopView. setVisibility (View. GONE );
}

If (newFocus! = Null ){

MapView. LayoutParams geoLP = (MapView. LayoutParams) popView. getLayoutParams ();
GeoLP. point = newFocus. getPoint (); // This line is used for popView locating.
TextView title = (TextView) popView. findViewById (R. id. map_bubbleTitle );
Title. setText (newFocus. getTitle ());

TextView desc = (TextView) popView. findViewById (R. id. map_bubbleText );
If (newFocus. getSnippet () = null | newFocus. getSnippet (). length () = 0 ){
Desc. setVisibility (View. GONE );
} Else {
Desc. setVisibility (View. VISIBLE );
Desc. setText (newFocus. getSnippet ());
}
MapView. updateViewLayout (popView, geoLP );
PopView. setVisibility (View. VISIBLE );
}
}
};

Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
/**
Omit other code
**/
// Initialize the bubble and set it to invisible
PopView = inflater. inflate (R. layout. overlay_popup, null );
MapView. addView (popView,
New MapView. LayoutParams (MapView. LayoutParams. WRAP_CONTENT, MapView. LayoutParams. WRAP_CONTENT,
Null, MapView. LayoutParams. BOTTOM_CENTER ));
// Because the tail of my bubble is centered below, set it to MapView. LayoutParams. BOTTOM_CENTER.
// GeoPoint is not set here in onFocusChangeListener
Views. add (popView );
PopView. setVisibility (View. GONE );
Add overlay
PointItemizedOverlay overlay = new PointItemizedOverlay (drawable );
// Set the listener for displaying and hiding bubbles
Overlay. setOnFocusChangeListener (onFocusChangeListener );
Overlay. addOverlay (/* your own overlayItem */);
Overlay. addOverlay (/* your own overlayItem */);
Overlay. addOverlay (/* your own overlayItem */);
}
}
This is basically done.

Http://www.apkbus.com/android-635-1-1.html

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.