悬浮窗口

package com.example.multiwin;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {
        WindowManager mWindowManager = null;
        WindowManager.LayoutParams mLayoutParams;
        Button mButton;
        private float touchX;
        private float touchY;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = new Button(getApplicationContext());
        mButton.setHeight(100);
        mButton.setWidth(100);
        mButton.setText(“这是一个悬浮框”);
        mButton.setGravity(Gravity.CENTER);
        mWindowManager = (WindowManager)getApplicationContext().getSystemService(“window”);
        mLayoutParams = new WindowManager.LayoutParams();
        mLayoutParams.format = 1;
        //mLayoutParams.format = PixelFormat.RGBA_8888;
        //mLayoutParams.type = 2002;
        //mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        mLayoutParams.type =2003;
        //mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        mLayoutParams.flags =40;
        //mLayoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //以屏幕的左上角为初始值,设置x y的初值
        //也就是设置LayoutParams的基准!可以改变x y的数值测试效果
        //x:X position for this window.摘自官方文档
        //When using LEFT or START or RIGHT or END it provides an offset from the given edge.

        mLayoutParams.x = 0;
        mLayoutParams.y = 0;
        mLayoutParams.width = 150;
        mLayoutParams.height = 150;

       //mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
       //mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
       //mLayoutParams.gravity = Gravity.CENTER;

       mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
       mLayoutParams.alpha = 0.7f;
       mWindowManager.addView(mButton, mLayoutParams);
       //为Button设置触控事件

       mButton.setOnTouchListener(new OnTouchListener() {
             @Override
              public boolean onTouch(View v, MotionEvent event) {
                      // TODO Auto-generated method stub
                     //在这里使用getX、getY的效果是不正常的!要注意
                         touchX = event.getRawX();

                touchY = event.getRawY();
                switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:         
                    updateViewPosition();
                    break;

                case MotionEvent.ACTION_MOVE:
                    updateViewPosition();
                     break;

                case MotionEvent.ACTION_UP:
                    updateViewPosition();
                     break;              

              }

                //return true;
                return false;
                     }
               });

        //为Button设置点击事件
        mButton.setOnClickListener(new OnClickListener() {
                @Override
                       public void onClick(View v) {
                     // TODO Auto-generated method stub
                     //      mWindowManager.removeView(mButton);
                     }
               });
   }
    public void updateViewPosition() {
            //int tempX = (int) (touchX – mButton.getWidth() / 2);
            //mLayoutParams.x = (tempX >= 0) tempX : 0;
            //int tempY = (int) (touchY – mButton.getHeight() / 2);
            //mLayoutParams.y = (tempY >= 0) tempY : 0;
            mLayoutParams.x = (int) (touchX – mButton.getWidth() / 2);
            mLayoutParams.y = (int) (touchY – mButton.getHeight() / 2);
            mWindowManager.updateViewLayout(mButton, mLayoutParams);         
    }
}

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2013年7月1日
下一篇 2013年7月1日

相关推荐