最近做了很多客制化的Touch需求,很多情况都可以直接通过GestureDetector来完成,这里挑选三指截屏简述原理。先来说说主要思路,在view的onInterceptTouchEvent方法中,处理pointerCount==3时的ACTION_MOVE事件,在这里可以判断手指滑动的速度,距离等,当满足我们需要的条件时,便可进行操作(这里的截屏是通过sendBroadcast(intent)后在PhoneWindowManager中接受广播进行截屏操作)。
在重写相关view的Touch操作时,先获取到当前的屏幕参数:
DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();//获取手机屏幕参数
mHeightPixels = metrics.heightPixels;//获取手机屏幕高度
重写onInterceptTouchEvent,利用Point存储手指移动前后X、Y轴的位置,可以在event.getPointerCount()满足需求时进行判断(不同屏幕支持多点触控的count也不相同,自己测试得到的最大值为5),同时定义了一个mMode变量,可以在case MotionEvent.ACTION_POINTER_DOWN中为其赋值,设置当前的mode类型,具体代码如下:
public boolean onInterceptTouchEvent(MotionEvent event) {
if(true) {
int pointerCount = event.getPointerCount();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mEnterTime = System.currentTimeMillis();
mMode = MODE_NONE;
CURRENT_SOUND_STEP = 15;//mAM.getStreamVolume(AudioManager.STREAM_SYSTEM);
initP0.x = preP0.x = (int)event.getX(0);
initP0.y = preP0.y = (int)event.getY(0);
break;
case MotionEvent.ACTION_MOVE:
//for when pointerCount ==3 but not go into ACTION_POINTER_DOWN
if(mMode == MODE_NONE || mMode == MODE_INTERUPT) {
break;
} else if((mMode == MODE_2) && (pointerCount == 2)) {
Point current0 = new Point((int)event.getX(0), (int)event.getY(0));
Point current1 = new Point((int)event.getX(1), (int)event.getY(1));
int step = (current0.y - preP0.y) / SOUND_STEP;
if(step != 0) {
int current = CURRENT_SOUND_STEP + (initP0.y - current0.y) / SOUND_STEP;
current = current < 0 ? 0 : current;
current = current > MAX_SOUND_STEP ? MAX_SOUND_STEP : current;
Log.i(TAG, "current:" + current);
//mAM.setMode(AudioManager.MODE_NORMAL);
mAM.setStreamVolume(AudioManager.STREAM_SYSTEM, current, AudioManager.FLAG_PLAY_SOUND);
preP0 = current0;
preP1 = current1;
} else if((mMode == MODE_3) && (pointerCount == 3)) {
Point current0 = new Point((int)event.getX(0), (int)event.getY(0));
Point current1 = new Point((int)event.getX(1), (int)event.getY(1));
Point current2 = new Point((int)event.getX(2), (int)event.getY(2));
if(Math.abs(current0.y - initP0.y) > (mHeightPixels / 10) &&
Math.abs(current1.y - initP1.y) > (mHeightPixels / 10) &&
Math.abs(current2.y - initP2.y) > (mHeightPixels / 10)) {
Intent intent = new Intent("intent.action.SendKey");
intent.putExtra("KeyCode", KeyEvent.KEYCODE_SYSRQ);
getContext().sendBroadcast(intent);
Log.e(TAG, "takeScreenshot!");
mMode = MODE_INTERUPT;
}
return true;//该touch事件终止向子view传递
}
break;
//有一个非主要的手指按下了
case MotionEvent.ACTION_POINTER_DOWN:
if(mMode == MODE_INTERUPT) {
break;
}
if(pointerCount == 2) {
mMode = MODE_2;
initP1.x = preP1.x = (int)event.getX(1);
initP1.y = preP1.y = (int)event.getY(1);
if(Math.abs(initP0.y - initP1.y) > (SOUND_STEP * 2)) {
mMode = MODE_INTERUPT;
Log.i(TAG, "ACTION_POINTER_DOWN p0 and p1 are too far.");
break;
}
Log.i(TAG, "ACTION_POINTER_DOWN MODE_2.");
} else if(pointerCount == 3) {
mMode = MODE_3;
initP1.x = preP1.x = (int)event.getX(1);
initP1.y = preP1.y = (int)event.getY(1);
initP2.x = preP2.x = (int)event.getX(2);
initP2.y = preP2.y = (int)event.getY(2);
Log.i(TAG, "ACTION_POINTER_DOWN MODE_3.");
}
if((System.currentTimeMillis() - mEnterTime) > 500) {
mMode = MODE_INTERUPT;
Log.i(TAG, "ACTION_POINTER_DOWN too slowly.");
}
break;
//有一个非主要的手指抬起了
case MotionEvent.ACTION_POINTER_UP:
mMode = MODE_INTERUPT;
break;
case MotionEvent.ACTION_UP:
mMode = MODE_NONE;
break;
}
}
}
具体的滑动距离,速度,方向等都可已自己根据需求修改。
接收广播时,registerReceiver与unregisterReceiver即可,注意在进行截屏前判断下当前的开关值
BroadcastReceiver mKeyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
boolean m3PTouchScreenshot;
if ("intent.action.SendKey".equals(action)) {
Bundle bundle = intent.getExtras();
int keycode = bundle.getInt("KeyCode");
Log.i(TAG, "mKeyReceiver:" + keycode);
switch(keycode) {
case KeyEvent.KEYCODE_SYSRQ:
boolean touchScreenshot = SystemProperties.getBoolean("3p_screenshot_off", false);
m3PTouchScreenshot = Settings.Global.getInt(context.getContentResolver(), "3p_touch_screenshot", (!touchScreenshot ? 1 : 0)) == 1;
Log.i(TAG, "m3PTouchScreenshot:" + m3PTouchScreenshot);
if(m3PTouchScreenshot)
takeScreenshot();
break;
}
}
}
};
takeScreenshot()就是执行的截屏操作啦~