项目需求:
android中只有单击和其他事件,其实都是由OnTouch事件演变而来;最近有项目要求双击全屏,所以就试着实现了下
具体实现如下:
1.MainActivity.java实现:
public class MainActivity extends Activity implements OnTouchListener { private long firstClick; private long lastClick; // 计算点击的次数 private int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.ontourch).setOnTouchListener(this); } @Override public boolean onTouch(View arg0, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 如果第二次点击 距离第一次点击时间过长 那么将第二次点击看为第一次点击 if (firstClick != 0 && System.currentTimeMillis() - firstClick > 300) { count = 0; } count++; if (count == 1) { firstClick = System.currentTimeMillis(); } else if (count == 2) { lastClick = System.currentTimeMillis(); // 两次点击小于300ms 也就是连续点击 if (lastClick - firstClick < 300) {// 判断是否是执行了双击事件 System.out.println(">>>>>>>>执行了双击事件"); } } break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return true; } }
2.main_activity.xml实现:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/ontourch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
好了实现就是这么简单;有问题可以@我
我总结出一句话:
如果target sdk>=13,必须使用如下方式声明activity:android:configChanges="orientation|screenSize" 否则不会调用onConfigurationChanged方法!!!
引用地址:
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
原文:
Caution: Beginning
with Android 3.2 (API level 13), the "screen size" also changes when
the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersionattributes),
you must include the "screenSize" value
in addition to the "orientation" value.
That is, you must decalare android:configChanges="orientation|screenSize".
However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
设置中心点和缩放比例:
/** * 监听amap地图加载成功事件回调 */ @Override public void onMapLoaded() { LatLng marker1 = new LatLng(39.90403, 116.407525); //设置中心点和缩放比例 aMap.moveCamera(CameraUpdateFactory.changeLatLng(marker1)); aMap.moveCamera(CameraUpdateFactory.zoomTo(12)); }
获取两点之前距离:
LatLng start = new LatLng(39.95676, 116.401394); LatLng end = new LatLng(36.63014,114.499574); AMapUtils.calculateLineDistance(start, end)
刚入手高德地图,在此做个标记。