【问题标题】:Android - Google Maps - How do I change the image of a marker and then back to the original image?Android - Google Maps - 如何更改标记的图像,然后返回原始图像?
【发布时间】:2015-07-28 04:31:20
【问题描述】:

我无法弄清楚如何在用户点击它时更改标记的可绘制对象,然后在用户将手指离开屏幕时返回到原始可绘制对象。

我已经知道如何使用这个来更改标记的可绘制对象:

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

问题是,该代码到底应该放在哪里?如果我将它放在onMarkerClick 回调中,那么为时已晚,因为该回调在用户将手指离开屏幕后触发(就像任何视图中的正常点击事件仅在手指离开屏幕时触发)。

我尝试将可绘制对象设置为选择器(如下所示),但它不起作用(代码引发异常):

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" />

【问题讨论】:

  • 我也面临这个问题。你解决了吗?
  • @CodeHunter 不,我终于能够说服设计团队稍微改变他们的设计。
  • 我明白了。好吧,当我单击标记时,我可以放置一个按下的图标,但是当我单击另一个标记时,不能返回前一个默认值。感谢您的快速回复。

标签: android google-maps selector drawable marker


【解决方案1】:

我建议你用地图试试OnTouchListener

MotionEvent.ACTION_DOWN 可以设置一个背景,MotionEvent.ACTION_UP 可以设置另一个背景。

请参考链接herehere

【讨论】:

  • 感谢您的回复,但我无法在标记或地图上应用 OnTouchListener。它们不是常见的观点。
猜你喜欢
  • 2013-08-31
  • 2017-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 2016-12-25
相关资源
最近更新 更多