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

所属分类: 软件编程 / Android 阅读数: 30
收藏 0 赞 0 分享

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.我以官方模拟器为例开启悬浮权限:

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

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

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

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多