Android使用WindowManager构造悬浮view_Android

一般在android显示一个View都是通过Activity的setContentView设置的,但是还有一种方法,可以直接使用WindowManager在整个应用的最上层绘制我们需要显示的view,总体的效果类似于AlertDialog的弹出效果。

使用WindowManager构造这样的一个悬浮View也比较简单,直接通过windowmanager.addView()方法即可。

package com.gearmotion.app.windowmanagermotion;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {

 Button mShowBtn;
 Button mHideBtn;
 WindowManager mWm;
 LayoutInflater mLayoutInflater;
 View mWindowView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mShowBtn = (Button) this.findViewById(R.id.showbtn);
  mHideBtn = (Button) this.findViewById(R.id.hidebtn);
  mShowBtn.setOnClickListener(this);
  mHideBtn.setOnClickListener(this);
  init();
 }

 private void init() {
  mWm = (WindowManager) this.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
  mLayoutInflater = LayoutInflater.from(this);
 }

 @Override
 public void onClick(View v) {
  if (mShowBtn.hashCode() == v.hashCode()) { //显示WindowManager
   show();
  }
  if (mHideBtn.hashCode() == v.hashCode()) { //隐藏windowmanager
   hide();
  }
 }

 private void show() {
  mWindowView = mLayoutInflater.inflate(R.layout.item_layout, null);
  View popView = mWindowView.findViewById(R.id.root);
  //设置popView的触摸事件,以便点击空白区域的时候使悬浮view消失
  popView.setOnTouchListener(this);
  WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
  //窗口类型同系统弹出框
  lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
  //响应输入法
  //lp.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
  //透明层
  lp.format = PixelFormat.TRANSPARENT;
  lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  lp.height = WindowManager.LayoutParams.MATCH_PARENT;
  lp.gravity = Gravity.CENTER_VERTICAL;
  mWm.addView(mWindowView, lp);
 }

 private void hide() {
  if (mWindowView != null && mWindowView.getParent() != null) {
   mWm.removeView(mWindowView);
  }
 }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
  int x = (int) event.getX();
  int y = (int) event.getY();
  //获取主view的可视区域
  Rect globalRect = new Rect();
  //获取悬浮view的可视区域
  Rect tmpRect = new Rect();
  v.getGlobalVisibleRect(globalRect);
  View child = ((ViewGroup) v).getChildAt(0);
  child.getHitRect(tmpRect);
  if (!tmpRect.contains(x, y) && globalRect.contains(x, y)) {
   hide();
  }
  return true;
 }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity">

 <Button
  android:id="@+id/showbtn"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:gravity="center"
  android:text="show" />

 <Button
  android:id="@+id/hidebtn"
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:gravity="center"
  android:text="hide" />
</LinearLayout>

item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:id="@+id/root"
 android:orientation="vertical">

 <TextView
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:text="I am WindowManager layout view"
  android:textSize="20sp"
  android:gravity="center"
  android:layout_gravity="center"
  android:background="#FFF8DC"
  android:textColor="#7AC5CD"/>
</LinearLayout>

实现效果如下:

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
, view
, 悬浮
windowmanager
windowmanager 悬浮窗、windowmanager view、popupwindow 悬浮、windowmanager、windowmanagerservice,以便于您获取更多的相关知识。

时间: 2024-07-30 19:13:23

Android使用WindowManager构造悬浮view_Android的相关文章

Android使用WindowManager构造悬浮view

一般在android显示一个View都是通过Activity的setContentView设置的,但是还有一种方法,可以直接使用WindowManager在整个应用的最上层绘制我们需要显示的view,总体的效果类似于AlertDialog的弹出效果. 使用WindowManager构造这样的一个悬浮View也比较简单,直接通过windowmanager.addView()方法即可. package com.gearmotion.app.windowmanagermotion; import an

悬浮窗-Android 通过WindowManager.addVidw创建的窗口系统是否会自动销毁?

问题描述 Android 通过WindowManager.addVidw创建的窗口系统是否会自动销毁? 如题,我做了一个类似悬浮窗的小辅助工具,设计初衷是要求在任何界面都能一直保持显示,并设计了一个开关来决定是否一直显示和隐藏.但是在运行测试的过程中发现,这个悬浮窗会在某些时候自动消失掉,但是我跟进过并log过removeView的地方,都没看到是我的误操作导致的销毁. 因此怀疑是否是系统在某些特定的环境或者情况下会自动回收这些Window的View,顺便贴下我的部分代码 WindowManag

android-求助:Android ScrollView中生成windowManager桌面悬浮窗问题

问题描述 求助:Android ScrollView中生成windowManager桌面悬浮窗问题 我想在scrollView中做一个可以拖动的悬浮窗,但是拖动的时候 悬浮窗没动,ScrollView上线滑动了,请求解决办法 解决方案 Android桌面悬浮窗 解决方案二: 给窗口设置触摸监听,然后触摸的时候,让ScrollView不要拦截事件,交由窗体处理,代码如下 lv.setOnTouchListener(new View.OnTouchListener() { @Override pub

android悬浮窗-android写了一个悬浮窗,但是输入法显示不出来了,希望能得到朋友们的帮助,谢谢了。

问题描述 android写了一个悬浮窗,但是输入法显示不出来了,希望能得到朋友们的帮助,谢谢了. 用android编写了悬浮窗,项目是用Unity3d做的,项目中的输入法软键盘无法显示了,能接收到按键,但是软键盘看不到. windowParams的参数如下,主要的问题在flags windowParams.type = LayoutParams.TYPE_PHONE; windowParams.format = PixelFormat.RGBA_8888; windowParams.flags

android 带磁性的悬浮窗体

http://blog.csdn.net/manymore13/article/details/8577286 带磁性的悬浮窗体,类似于360绿色小人 主要实现的是: 1.悬浮所有窗体之上 2.有吸引力,吸附于屏幕边上 3.有点击效果 下面我就实现上面三点,简单封装了个FloatView  先看下本次Demo的效果图,然后再看代码, 效果图: FloatView代码如下 package com.manymore13.flowwindowdemo; import android.content.C

Android使用WindowManager制作一个可拖动的控件_Android

效果图如下 第一步:新建DragView继承RelativeLayout package com.rong.activity; import com.rong.test.R; import android.content.Context; import android.graphics.Color; import android.graphics.PixelFormat; import android.util.AttributeSet; import android.view.Gravity;

Android使用WindowManager制作一个可拖动的控件

效果图如下 第一步:新建DragView继承RelativeLayout package com.rong.activity; import com.rong.test.R; import android.content.Context; import android.graphics.Color; import android.graphics.PixelFormat; import android.util.AttributeSet; import android.view.Gravity;

Android中可自由移动悬浮窗口的Demo

 前段时间捣鼓出Android悬浮窗口的实现,今天抽空写了一个可自由移动悬浮窗口的Demo. 简要说明如下: 1.通过覆写悬浮View中onTouchEvent方法实现自由移动悬浮窗口. 2.悬浮窗口坐标的移动实际是windowMananager.LayoutParams中x和y的变换,但是要注意设置相应的gravity. 3.用windowManager创建的View,当不需要时,务必记住使用windowManager的removeView方法来移除,请在Activity相关生命周期中自行添加

Android轻松实现RecyclerView悬浮条

在我们在刷Instagram的动态时,你是否注意到这样一个小小的动效,就是当一条动态(以卡片形式呈现)向上滑动时,动态卡片的头部会始终悬浮在列表最上方,直到下一张动态卡片的头部将它顶掉并替换它悬浮着.言语可能说不清楚,就直接来看一下它的效果好了. 综合我上面的文字描述加上这张Gif图,我想大家应该知道这是个什么样的效果了吧.那么不废话了,接下来我就来说说一种很简单的实现方法吧. 思路 虽然实现起来炒鸡简单,但还是花了我一个多小时的时间思考实现.先说说思考过程吧,那天中午,Instagram给我推