package com.example.map_js;
import com.example.utils.LogToastSysUtils;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
public class WebViewTouch implements OnTouchListener {
RunJavaScript rjs;
float eventDown;
float evnetUp;
boolean isPress = false;
Handler handler = new Handler();
public WebViewTouch(RunJavaScript rjs) {
super();
this.rjs = rjs;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
printLog("action-->"+action);
switch (action) {
case MotionEvent.ACTION_DOWN:
printLog("ACTION_DOWN");
isPress = true;
//模擬長按事件
handler.postDelayed(longClick, 500);
break;
case MotionEvent.ACTION_UP:
printLog("ACTION_UP");
isPress = false;
break;
}
//多手勢時返回的 第二個按下是261 彈起的時候是262 運算後
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
printLog("ACTION_POINTER_DOWN");
eventDown = dis(event);
break;
case MotionEvent.ACTION_POINTER_UP:
printLog("ACTION_POINTER_UP");
evnetUp = dis(event);
int t = calEvent(eventDown, evnetUp);
if (t == 1) {
rjs.mapBig();
return true;
} else if (t == -1) {
rjs.mapSmall();
return true;
}
break;
default:
break;
}
return false;
}
private void printLog(String str) {
LogToastSysUtils.printLog("WebViewTouch", str);
}
//根據按下和彈起兩個點之間的距離差的差
private int calEvent(float eventDown, float eventUp) {
float t = eventDown - eventUp;
printLog("(eventDown-eventUp)-->" + (eventDown - eventUp));
if (t > 20f) {
return -1;
} else if (t < -20f) {
return 1;
}
return 0;
}
//計算兩個手勢間的距離
private float dis(MotionEvent event) {
return Math.abs(event.getX(0) - event.getX(1));
}
private Runnable longClick = new Runnable() {
@Override
public void run() {
if (isPress) {
// rjs.mapChangeType();
}
}
};
}