当我的项目中需要捕获google map的touch事件时,才发现google没有提供OnTouchListener,在其提供的一些listener中看了一遍也没发现有什么可以替代的,一室查了一番。还好有人实现了该功能,原文链接如下:
How to detect a user pan/touch/drag on Android Map v2
作者捕获的是按屏幕200毫秒以上的事件,有点像LongClick,逻辑改改就可以捕获自己想要的事件了,对我来说,其实想捕获“用户移动了地图”的事件,代码如下:
public class MySupportMapFragment extends SupportMapFragment {
public View mOriginalContentView;
public TouchableWrapper mTouchView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
mOriginalContentView = super.onCreateView(inflater, parent, savedInstanceState);
mTouchView = new TouchableWrapper(getActivity());
mTouchView.addView(mOriginalContentView);
return mTouchView;
}
@Override
public View getView() {
return mOriginalContentView;
}
}
public class TouchableWrapper extends FrameLayout {
private float downX = 0;
private float downY = 0;
private float upX = 0;
private float upY = 0;
private static final float MOVE_DISTANCE = 40;
private UpdateMapAfterUserInterection updateMapAfterUserInterection;
public TouchableWrapper(Context context) {
super(context);
// Force the host activity to implement the UpdateMapAfterUserInterection Interface
try {
updateMapAfterUserInterection = (MapActivity) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement UpdateMapAfterUserInterection");
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
// lastTouched = SystemClock.uptimeMillis();
downX = ev.getX();
downY = ev.getY();
break;
case MotionEvent.ACTION_UP:
upX = ev.getX();
upY = ev.getY();
float intervalX = Math.abs(upX - downX);
float intervalY = Math.abs(upY - downY);
if (intervalX > MOVE_DISTANCE || intervalY > MOVE_DISTANCE) {
// Update the map
updateMapAfterUserInterection.onUpdateMapAfterUserInterection();
}
break;
}
return super.dispatchTouchEvent(ev);
}
// Map Activity must implement this interface
public interface UpdateMapAfterUserInterection {
public void onUpdateMapAfterUserInterection();
}
}
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="net.jackie.view.MySupportMapFragment"/>
public class MapActivity extends FragmentActivity implements UpdateMapAfterUserInterection {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Implement the interface method
public void onUpdateMapAfterUserInterection() {
// TODO Update the map now
}
}
分享到:
相关推荐
重写onTouchEvent事件,实现上下左右滑动的小例子。
onInterceptTouchEvent和onTouchEvent调用关系详解
重写onTouchEvent实现滑屏显示菜单,内有缓存,数据库存储
NULL 博文链接:https://yxwww.iteye.com/blog/1409461
关于onInterceptTouchEvent和onTouchEvent使用demo,很不错的一个demo,可以帮助Android的触摸事件。
onInterceptTouchEvent与OntouchEvent之间的事件传递机制的具体内涵关系...
我们也许会遇到,自定义控件的触屏事件处理,先来了解一下View类中的,onTouch事件和onTouchEvent事件。 1、boolean onTouch(View v, MotionVent event) 触摸事件发送到视图时调用(v:视图,event:触摸事件) 返回...
NULL 博文链接:https://2528.iteye.com/blog/1056731
9.6 移动版GoogleMap——Geocoder反查Address对象 9.7 规划导航路径——DirectionsRoute 9.8 移动设备上的Picasa相册——GooglePicasaAPI 9.9 随身翻译机——GoogleTranslateAPI 第10章 创意Android程序设计 10.1 ...
最难理解的其实是onTouchEvent方法。 一、 概述 1.只有view,ViewGroup,Activity 具有事件分发和消费的功能。 2.Activity因为上最先接触到触摸事件,因此Activity没有事件拦截方法。即没有dispatchTouchEvent方法。...
本篇文章小编为大家介绍,Android onTouchEvent 与 onInterceptTouchEvent的区别详解。需要的朋友参考下
NULL 博文链接:https://username2.iteye.com/blog/2193153
下面小编就为大家带来一篇Android onTouchEvent事件中onTouch方法返回值(介绍)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
下面小编就为大家带来一篇老生常谈onTouch和onTouchEvent(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
继承View类,实现触摸事件OnTouchEvent,使用菜单选项。
详细请参考我的博客: http://blog.csdn.net/allen315410/article/details/41628301
onTouchEvent、TouchDelegate、Transform手势的对应物,通知开始、结束、主指针、指针和选项.zip