zoukankan      html  css  js  c++  java
  • 悬浮窗

     1 package com.bi.dialogactivity_xuanfuchuang.app;
     2 
     3 import android.graphics.Color;
     4 import android.graphics.PixelFormat;
     5 import android.support.v7.app.ActionBarActivity;
     6 import android.os.Bundle;
     7 import android.view.*;
     8 import android.widget.LinearLayout;
     9 import android.widget.TextView;
    10 
    11 
    12 public class MainActivity extends ActionBarActivity {
    13 
    14     private LinearLayout ll;
    15 
    16     @Override
    17     protected void onCreate(Bundle savedInstanceState) {
    18         super.onCreate(savedInstanceState);
    19         setContentView(R.layout.activity_main);
    20 
    21 
    22         ll=new LinearLayout(this);
    23        ll.setBackgroundColor(Color.BLUE);
    24         Window window=getWindow();
    25         WindowManager.LayoutParams layoutParams = window.getAttributes();
    26 
    27         layoutParams.gravity= Gravity.CENTER;
    28 
    29         //x只有gravity设置了Left right之后才起作用
    30         //y只有gravity设置了top bottom之后才起作用
    31         layoutParams.x=10;
    32         layoutParams.y=10;
    33         layoutParams.width=WindowManager.LayoutParams.MATCH_PARENT;
    34         layoutParams.height=300;
    35 
    36         window.setAttributes(layoutParams);
    37 
    38         WindowManager manager=getWindowManager();
    39         WindowManager.LayoutParams lp=new WindowManager.LayoutParams();
    40         lp.gravity=Gravity.TOP|Gravity.LEFT;
    41         lp.x=100;
    42         lp.y=10;
    43         lp.width=200;
    44         lp.height=30;
    45         lp.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
    46                 |WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
    47         lp.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
    48                 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;//用户点击悬浮窗之外区域能传递事件
    49         //悬浮窗通过addView后如果悬浮窗可以获取焦点那么下层的activity将不能交互
    50         manager.addView(ll,lp);
    51 
    52     }
    53     //不重写此方法会导致回退键不能用
    54     @Override
    55     protected void onDestroy() {
    56         //释放悬浮窗
    57         WindowManager manager=getWindowManager();
    58         manager.removeViewImmediate(ll);
    59         super.onDestroy();
    60     }
    61 
    62 
    63 }
    Main

    上面是activity级别的悬浮窗  activity销毁的话  悬浮窗也会销毁

    下面的是系统级别的不会销毁

     1 package com.bi.xuanfuchuangxitong.app;
     2 
     3 import android.graphics.PixelFormat;
     4 import android.support.v7.app.ActionBarActivity;
     5 import android.os.Bundle;
     6 import android.util.Log;
     7 import android.view.*;
     8 import android.widget.ImageView;
     9 
    10 
    11 public class MainActivity extends ActionBarActivity {
    12 
    13 
    14     private WindowManager.LayoutParams lp;
    15     private WindowManager manager;
    16     private static ImageView imageView;
    17 
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_main);
    22 
    23         manager = (WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE);
    24         //利用Application来创建ImageView避免activity退出导致的Context问题
    25         if(imageView!=null){
    26             manager.removeViewImmediate(imageView);
    27         }
    28 
    29         imageView = new ImageView(getApplicationContext());
    30         imageView.setImageResource(R.mipmap.ic_launcher);
    31         //设置触摸监听
    32         //建议使用匿名内部类来设置触摸的监听器
    33         imageView.setOnTouchListener(new View.OnTouchListener() {
    34             private float lastX, lastY;
    35 
    36             @Override
    37             public boolean onTouch(View v, MotionEvent event) {
    38                 boolean ret = false;
    39                 int action = event.getAction();
    40                 switch (action) {
    41                     case MotionEvent.ACTION_DOWN:
    42                         lastX = event.getRawX();
    43                         lastY = event.getRawY();
    44                         ret = true;
    45                         Log.d("ff", "-->" + action);
    46                         break;
    47                     case MotionEvent.ACTION_MOVE:
    48                         float cx = event.getRawX();
    49                         float cy = event.getRawY();
    50                         float ccx = cx - lastX;
    51                         float ccy = cy - lastY;
    52 
    53                         lp.x += ccx;
    54                         lp.y += ccy;
    55                         //更新悬浮窗
    56                         manager.updateViewLayout(imageView, lp);
    57                         lastY=cy;
    58                         lastX=cx;
    59 
    60                         Log.d("ff", "-->" + action);
    61                         break;
    62                     case MotionEvent.ACTION_UP:
    63                         Log.d("ff", "-->" + action);
    64                         break;
    65                 }
    66 
    67                 return ret;
    68             }
    69         });
    70 
    71 
    72         lp = new WindowManager.LayoutParams();
    73         //对于应用程序实用的系统类型
    74         //TYPE_SYSTEM_ALERT允许接收事件    TYPE_SYSTEM_OVERLAY只是悬浮
    75         lp.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
    76                 |WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
    77         lp.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
    78                 |WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;//用户点击悬浮窗之外区域能传递事件
    79         lp.gravity= Gravity.LEFT|Gravity.TOP;
    80         lp.x=300;
    81         lp.y=300;
    82         lp.width=WindowManager.LayoutParams.WRAP_CONTENT;
    83         lp.height=WindowManager.LayoutParams.WRAP_CONTENT;
    84        lp.format= PixelFormat.TRANSPARENT;
    85         manager.addView(imageView, lp);
    86 
    87 
    88     }
    89 
    90 
    91 
    92 
    93 }
    main

    最后需要添加权限

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  • 相关阅读:
    myeclipse_导入js文件报错
    myeclipse_tomcat在debug模式中报错的信息
    myeclipse_修改@author的值
    项目总结_web文件上传问题
    项目总结_导入JSTL标签库
    在Ubuntu上实现人脸识别登录
    Data Science and Matrix Optimization-课程推荐
    Digix2019华为算法精英挑战赛代码
    Shell script notes
    optim.py-使用tensorflow实现一般优化算法
  • 原文地址:https://www.cnblogs.com/bimingcong/p/4915828.html
Copyright © 2011-2022 走看看