Android - Google地图 - 如何更改标记的图像,然后返回原始图像?

时间:2015-07-28 04:31:20

标签: android google-maps selector drawable marker

我无法弄清楚如何更改标记的drawable,以便当用户点击它时,然后当用户将手指从屏幕上移开时返回到原始drawable。

我已经知道如何使用以下方法更改标记的drawable:

marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.my_drawable));

问题是,我在哪里准确地放置代码?如果我把它放在onMarkerClick回调中,那么为时已晚,因为用户将手指离开屏幕后会触发回调(就像任何视图中的普通点击事件仅在手指松开时才会触发)屏幕)。

我试图将drawable设置为选择器(如下所示),但它不起作用(代码抛出异常):

final BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.my_selector);

final MarkerOptions markerOptions = new MarkerOptions().icon(bitmapDescriptor)
                                                       .anchor(0.5f, 1.0f)
                                                       .position(latLon);

这是my_selector.xml(上面使用过):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/drawable_A" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/drawable_B" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="@drawable/drawable_B" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="@android:color/white" />

1 个答案:

答案 0 :(得分:0)

我建议您尝试使用地图OnTouchListener

MotionEvent.ACTION_DOWN上,您可以设置一个背景,在MotionEvent.ACTION_UP上可以设置其他背景。

请参阅链接herehere

相关问题