How to change the marker for the overlay on tap for android?

前端 未结 4 1452
北海茫月
北海茫月 2021-02-07 20:06

i have made an application in which i need to change the drawable of an overlayitem when a user clicks on it. i am using the following code to achieve this effect:



        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-07 20:47

    I see a lot of answers here doing this the hard way. If you have 2 images and you want to flip them based on focus, do it the easy way:

    Step 1: Copy both images into a drawables folder:

    Example: mycon_focused.png, mycon.png

    Step 2: Create a selector xml file in drawables: Example "marker.xml"

    
    
         
         
    
    

    Step 3: When you create your ItemOverlay and add the OverlayItems drawable, use

    getResources().getDrawable(R.drawable.marker);
    

    instead of

    getResources().getDrawable(R.drawable.mycon);
    

    and then programically changing it in the on tap method. After reading through many answers and not seeing this anywhere I just tried it myself, and it worked perfectly.

    Much thanks to previous contributors, without your help I wouldn't have had a starting point.

    Another note: If you're using Sherif elKhatib suggested code and your marker position is off try:

    int width = marker.getIntrinsicWidth();
    int height = marker.getIntrinsicHeight();
    marker.setBounds(-(width/2),-height,(width/2),0);
    

    instead of

    marker.setBounds(0,0,marker.getIntrinsicWidth(),marker.getIntrinsicHeight());
    

    This should center it perfectly.

提交回复
热议问题