示例#1
0
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   FrameLayout mainView = null;
   mainView = new FrameLayout(this);
   setContentView(mainView);
   View view = null;
   view = new MyView(this);
   view.setLongClickable(true);
   view.setOnTouchListener(this);
   mainView.addView(view);
   textView = new TextView(this);
   mainView.addView(textView);
 }
  private void createFloatingWindow() {
    SharedPreferences shared = getSharedPreferences("float_flag", Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = shared.edit();
    editor.putInt("float", 1);
    editor.commit();
    windowManager = (WindowManager) getApplicationContext().getSystemService("window");
    wmParams = ((ProfilerApplication) getApplication()).getMywmParams();
    wmParams.type = 2002;
    wmParams.flags |= 8;
    wmParams.gravity = Gravity.LEFT | Gravity.TOP;
    wmParams.x = 0;
    wmParams.y = 0;
    wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.format = 1;
    windowManager.addView(floatingView, wmParams);
    floatingView.setOnTouchListener(
        new View.OnTouchListener() {
          public boolean onTouch(View v, MotionEvent event) {
            x = event.getRawX();
            y = event.getRawY() - 25;
            switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                mTouchStartX = event.getX();
                mTouchStartY = event.getY();
                break;
              case MotionEvent.ACTION_MOVE:
                updateViewPosition();
                break;

              case MotionEvent.ACTION_UP:
                updateViewPosition();
                mTouchStartX = mTouchStartY = 0;
                break;
            }
            return true;
          }
        });
  }