我这里的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);
}
}