手势监听GestureDetector

我这里的activity是NoDislay类型的,布局通过windowManager添加的,只监听了滑动,处理了左右滑动的距离


    <style name="AccountActivityTheme" parent="android:Theme.NoDisplay">
    <item name="android:windowNoTitle">true</item>

    </style>

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CurrentPictureActivity extends Activity  {

	private String Tag = "CurrentPictureMainActivity";
	private LockScreenReceiver mLockScreenReceiver;
	private WindowManager mWindowManager ;
	private WindowManager.LayoutParams wmParams;
	private RelativeLayout layout;
	private Vibrator vibrator;
	private	GestureDetector detector;
	private TextView TV_sleep;
	private TextView TV_unlock;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 隐藏标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		Log.i(Tag, "onCreate:----------");
		initReceiver();
		inItView();
	}

	private void inItView(){
		//获取LayoutParams对象  
		wmParams = new WindowManager.LayoutParams();  

		//获取的是LocalWindowManager对象  
		mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		//设置window type  
		//wmParams.type = LayoutParams.TYPE_PHONE;  
		wmParams.type =WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
		//设置悬浮窗口长宽数据    
		wmParams.width = WindowManager.LayoutParams.MATCH_PARENT;  
		wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		LayoutInflater inflater = getLayoutInflater();
		layout = (RelativeLayout)inflater.inflate(R.layout.activity_main, null);
		TV_sleep = (TextView)layout.findViewById(R.id.TV_sleep);
		TV_unlock = (TextView)layout.findViewById(R.id.TV_unlock);

		TV_unlock.setVisibility(View.INVISIBLE);
		//调整悬浮窗显示的停靠位置为左侧置顶  
		wmParams.gravity = Gravity.BOTTOM;
		mWindowManager.addView(layout, wmParams);
		//监听手势,这里主要监听左右滑动
		final GestureDetector  mGestureDetector = new GestureDetector(this, new MyOnGestureListener());
		layout.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				mGestureDetector.onTouchEvent(event);
				return true;//这里一定要返回true
			}
		});

	}


	public class MyOnGestureListener extends SimpleOnGestureListener{

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// TODO Auto-generated method stub

			Log.i(Tag, "onScroll:  e1.getX()"+e1.getX());
			Log.i(Tag, "onScroll:  e2.getX()"+e2.getX());
			Log.i(Tag, "onScroll:  e1.getY()"+e1.getY());
			Log.i(Tag, "onScroll:  e2.getY()"+e2.getY());
			Log.i(Tag, "onScroll:  distanceX"+distanceX);
			if ((distanceX >50 || distanceX < -50) ) {
				//移除view 
				if (mWindowManager!= null) {
					mWindowManager.removeView(layout);
					mWindowManager = null;
				}
				finish();
			}
			return super.onScroll(e1, e2, distanceX, distanceY);
		}

	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值