当前位置: 移动技术网 > 移动技术>移动开发>Android > Android手机悬浮窗口小案例

Android手机悬浮窗口小案例

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下

–主页面——–

//布局中就一个button
public class mainactivity extends activity {

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  findviewbyid(r.id.btn).setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view view) {
   //目的就是启动service来打开悬浮窗体
    startservice(new intent(mainactivity.this, floatservice.class));
    finish();
   }
  });

 }
}

—service开启悬浮窗体——-

/**
 * description:通过service来开启floatview
 * 作者:ldm
 * 时间:20162016/8/17 14:03
 * 邮箱:1786911211@qq.com
 */
public class floatservice extends service {
 @override
 public void oncreate() {
  super.oncreate();
  customviewmanager.getinstance(this).showfloatviewonwindow();
 }

 @override
 public ibinder onbind(intent intent) {
  return null;
 }
}

—悬浮窗体管理工具类——-

/**
 * description:
 * 作者:ldm
 * 时间:20162016/8/17 11:57
 * 邮箱:1786911211@qq.com
 */
public class customviewmanager {
 //上下文
 private context mcontext;
 //本类实例
 private static customviewmanager instance;
 //自定义的floatview
 private floatview mfloatview;
 //窗口管理类
 private windowmanager mwindowmanager;

 private customviewmanager(context context) {
  this.mcontext = context;
  mfloatview = new floatview(mcontext);
  mwindowmanager = (windowmanager) mcontext.getsystemservice(context.window_service);
 }

 /**
  * @param
  * @description 通过单例模式获取实例对象
  * @author ldm
  * @time 2016/8/17 11:59
  */
 public static customviewmanager getinstance(context mcontext) {
  if (null == instance) {
   synchronized (customviewmanager.class) {
    if (null == instance) {
     instance = new customviewmanager(mcontext);
    }
   }
  }
  return instance;
 }

 /**
  * @param
  * @description 在手机屏幕上显示自定义的floatview
  * @author ldm
  * @time 2016/8/17 13:47
  */
 public void showfloatviewonwindow() {
  windowmanager.layoutparams parmas = new windowmanager.layoutparams();
  parmas.width = mfloatview.getfloatwidth();
  parmas.height = mfloatview.getfloatheight();
  //窗口图案放置位置
  parmas.gravity = gravity.left | gravity.center;
  // 如果忽略gravity属性,那么它表示窗口的绝对x位置。
  parmas.x = 0;
  //如果忽略gravity属性,那么它表示窗口的绝对y位置。
  parmas.y = 0;
  ////电话窗口。它用于电话交互(特别是呼入)。它置于所有应用程序之上,状态栏之下。
  parmas.type = windowmanager.layoutparams.type_system_alert;
  //flag_not_focusable让window不能获得焦点,这样用户快就不能向该window发送按键事件及按钮事件
  //flag_not_touch_modal即使在该window在可获得焦点情况下,仍然把该window之外的任何event发送到该window之后的其他window.
  parmas.flags = windowmanager.layoutparams.flag_not_focusable;
  // 期望的位图格式。默认为不透明。参考android.graphics.pixelformat。
  parmas.format = pixelformat.rgba_8888;
  mwindowmanager.addview(mfloatview, parmas);
 }
}

—自定义的floatview——-

/**
 * description:
 * 作者:ldm
 * 时间:20162016/8/17 11:17
 * 邮箱:1786911211@qq.com
 */
public class floatview extends view {
 //悬浮球宽度
 private int floatwidth = 150;
 //悬浮球高度
 private int floatheight = 150;
 //悬浮球画笔
 private paint mpaint;
 //绘制文字画笔
 private paint mtextpaint;
 private string text = "50%";

 public floatview(context context) {
  super(context);
  initpaint();
 }


 public int getfloatwidth() {
  return floatwidth;
 }


 public int getfloatheight() {
  return floatheight;
 }

 public floatview(context context, attributeset attrs) {
  super(context, attrs);
  initpaint();
 }

 public floatview(context context, attributeset attrs, int defstyleattr) {
  super(context, attrs, defstyleattr);
  initpaint();
 }

 /**
  * @param
  * @description 初始化画笔
  * @author ldm
  * @time 2016/8/17 11:37
  */
 private void initpaint() {
  //设置悬浮球画笔
  mpaint = new paint();
  mpaint.setcolor(color.green);
  mpaint.setantialias(true);
  mpaint.setdither(true);
  //设置文字画笔
  mtextpaint = new paint();
  mtextpaint.settextsize(25);
  mpaint.setantialias(true);
  mtextpaint.setcolor(color.white);
  mtextpaint.setfakeboldtext(true);
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  //设置大小
  setmeasureddimension(floatwidth, floatheight);
 }

 /**
  * @param
  * @description 绘制图案
  * @author ldm
  * @time 2016/8/17 11:44
  */
 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  //绘制悬浮球
  canvas.drawcircle(floatwidth / 2, floatheight / 2, floatwidth / 2, mpaint);
  //绘制文字
  paint.fontmetrics metrics = mtextpaint.getfontmetrics();
  //文字大小计算可以参考:http://mikewang.blog.51cto.com/3826268/871765/
  float textwidth = mtextpaint.measuretext(text);
  float x = floatwidth / 2 - textwidth / 2;
  float dy = -(metrics.descent + metrics.ascent) / 2;
  float y = floatheight / 2 + dy;
  canvas.drawtext(text, x, y, mtextpaint);
 }
}


最后,在androidmanifest.xml中不要忘记添加权限:

 <!--添加权限-->
 <uses-permission android:name="android.permission.system_alert_window" />


当然也要记得添加service组件

 <service android:name=".service.floatservice" />


有的手机运行后,发现没有出现我们想要的悬浮窗体,可以进入手机设置中心,点击应用设置,在指定的应用权限设置中打开悬浮窗体相应的设置开关即可。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网