当前位置: 移动技术网 > IT编程>移动开发>Android > Android仿360悬浮小球自定义view实现示例

Android仿360悬浮小球自定义view实现示例

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

油茶种植可行性报告,李德裕劾僧,刘进图

android仿360悬浮小球自定义view实现示例

效果图如下:



实现当前这种类似的效果 和360小球 悬浮桌面差不错类似。这种效果是如何实现的呢。废话不多说 ,直接上代码。

1.新建工程,添加悬浮窗权限。

<uses-permission android:name="android.permission.system_alert_window" />

2.自定义一个floatmessagermainwindow

import android.content.context;
import android.graphics.pixelformat;
import android.graphics.point;
import android.view.gravity;
import android.view.layoutinflater;
import android.view.view;
import android.view.windowmanager;
import android.widget.imageview;
import android.widget.toast;

import com.android.view.floatmessagepopledialog;

/**
 * created by liupanpan on 2017/3/16.
 */

public class floatmessagermainwindow {
 private context context;
 private view view;
 private windowmanager.layoutparams mparams = null;
 private windowmanager windowmanager = null;
 private static floatmessagermainwindow floatmessagermainwindow;

 public floatmessagermainwindow(context context, view view) {
  this.context = context;
  this.view = view;
  showwindow(context);
 }

 public static floatmessagermainwindow getfloatmessagermainwindow(context context, view view) {
  if (floatmessagermainwindow == null) {
   synchronized (floatmessagermainwindow.class) {
    if (floatmessagermainwindow == null) {
     floatmessagermainwindow = new floatmessagermainwindow(context, view);
    }
   }
  }
  return floatmessagermainwindow;
 }

 private void showwindow(final context context) {
//  if (!iswindowdismiss) {
//   log.e(tag, "view is already added here");
//   return;
//  }
//  iswindowdismiss = false;
  if (windowmanager == null) {
   windowmanager = (windowmanager) context.getapplicationcontext().getsystemservice(context.window_service);
  }

  point size = new point();
  windowmanager.getdefaultdisplay().getsize(size);
  int screenwidth = size.x;
  int screenheight = size.y;

  mparams = new windowmanager.layoutparams();
  mparams.packagename = context.getpackagename();
  mparams.width = windowmanager.layoutparams.wrap_content;
  mparams.height = windowmanager.layoutparams.wrap_content;
  mparams.flags = windowmanager.layoutparams.flag_not_touch_modal
    | windowmanager.layoutparams.flag_not_focusable | windowmanager.layoutparams.flag_layout_inset_decor
    | windowmanager.layoutparams.flag_layout_in_screen;
  mparams.type = windowmanager.layoutparams.type_system_error;
//  mparams.softinputmode = windowmanager.layoutparams.soft_input_adjust_resize |
//    windowmanager.layoutparams.soft_input_state_hidden;
  mparams.format = pixelformat.rgba_8888;
  mparams.gravity = gravity.left | gravity.top;
  mparams.x = screenwidth - dp2px(context, 450);
  mparams.y = screenheight - dp2px(context, 550);


  imageview imageview = new imageview(context);
  imageview.setimageresource(r.mipmap.icon_tab_item_message_pressed);
  imageview.setonclicklistener(new view.onclicklistener() {
   @override
   public void onclick(view v) {
    toast.maketext(context, "image=========", toast.length_short).show();
    view view = layoutinflater.from(context).inflate(r.layout.float_pople_room_layout, null);
    floatmessagepopledialog.getinstance(context, r.style.webviewtheme).setcontextview(view);
   }
  });
//  floatview = new avcallfloatview(context);
//  floatview.setparams(mparams);
//  floatview.setisshowing(true);
  windowmanager.addview(imageview, mparams);
 }

 private int dp2px(context context, float dp) {
  final float scale = context.getresources().getdisplaymetrics().density;
  return (int) (dp * scale + 0.1f);
 }
}

调用方法:

floatmessagermainwindow.getfloatmessagermainwindow(context, null);

实现到此 ,点击按钮就可以实现 悬浮窗。(此处可能会出现相应的崩溃,崩溃原因是悬浮窗的 悬浮权限开启问题。)

4.我以官方模拟器为例开启悬浮权限:

打开允许在其他应用上的管理权限

此时再次打开工程,点击按钮,就可以实现悬浮效果。

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网