- package com.example.windowmnagerexcercise;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.PixelFormat;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.WindowManager;
- import android.webkit.WebView.FindListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Button button=(Button) findViewById(R.id.button1);
- final LayoutInflater inflater=getLayoutInflater();
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- WindowManager windowManager=(WindowManager) getSystemService(Context.WINDOW_SERVICE);
- WindowManager.LayoutParams wmParams=new WindowManager.LayoutParams();
- wmParams.type =WindowManager.LayoutParams.TYPE_PHONE; // 设置window type
- wmParams.format =PixelFormat.RGBA_8888;// 设置图片格式,效果为背景透明
- /*
- * 下面的flags属性的效果形同“锁定”。 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。
- */
- wmParams.flags=WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
- WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
- wmParams.gravity = Gravity.RIGHT| Gravity. CENTER_VERTICAL; // 调整悬浮窗口至右侧中间
- // 以屏幕左上角为原点,设置x、y初始值
- wmParams.x = 0;
- wmParams.y = 0;
- // 设置悬浮窗口长宽数据
- wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
- wmParams.height =WindowManager.LayoutParams.WRAP_CONTENT;
- //注意此处的p_w_picpathview必须不属于某个父控件,否则报错。
- ImageView p_w_picpathView=new ImageView(getApplicationContext());
- p_w_picpathView.setImageResource(R.drawable.e);
- windowManager.addView(p_w_picpathView, wmParams);
- }
- });
- }
- }