android view可视区域,Android使用WindowManager构造悬浮view
一般在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:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
android:id="@+id/showbtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="show" />
android:id="@+id/hidebtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="hide" />
item_layout.xml:
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/root"
android:orientation="vertical">
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"/>
实现效果如下:
以上就是本文的全部内容,希望对大家学习Android有所帮助,也希望大家多多支持脚本之家。
android view可视区域,Android使用WindowManager构造悬浮view相关推荐
- android 拍照固定区域,android相机区域拍照,快速集成
使用相机拍摄指定区域,自己封装了个工具类,直接使用就可以 代码使用如下 public class MainActivity extends Activity implements OnClickLis ...
- android 图片热点区域,Android开发之扫描附近wifi热点并列表显示
最近项目中用到了wifi模块,今天做一个简单的总结. 参考:http://www.2cto.com/kf/201310/253617.html 1.怎样获取wifi对象并进行操作 要操作WIFI设备, ...
- android 文本输入区域,android – 键盘模糊输入文本区域
我有一个显示在 http://ionicframework.com/docs/api/service/$ionicPopup/内的表单.问题是当光标放在最终输入(文本区域)内时,键盘显示但输入不会滚动 ...
- Android 系统(49)---Android获取窗口可视区域大小: getWindowVisibleDisplayFrame()
Android获取窗口可视区域大小: getWindowVisibleDisplayFrame() getWindowVisibleDisplayFrame()方法 getWindowVisibleD ...
- android布局 哪个控件在最上层_Android 在最上层添加悬浮View(兼容Android 8.0)
记着在很久以前写Android的时候做一个在最上层悬浮的一个View非常容易,什么权限啊什么的根本没有那么多限制,,但今天试着写了一下发现处处碰壁,出现了各种各样的问题,后来在网上看了一些资料,发现现 ...
- android 分屏投影,Android 双屏异显实现的三种方式
在各种产品脑洞大开的时代,需求也是日益新异,笔者最近开发了一套双屏异显app.现在做一些总结 1.双屏异显第一种实现方式(官方提供的Presentation) Android 提供了一个叫 Prese ...
- android 悬停浮动框,Android滑动到顶部悬停
无图说卵,先上图 jianshu-top.gif 查阅资料后,发现网上大部分都是用这种方法实现的: 多写一个和需要悬浮的部分一模一样的layout,先把浮动区域的可见性设置为gone.当浮动区域滑动到 ...
- iOS 截图 对view指定区域 以及 对图片 指定区域截图
//MARK:获取截图,对view 制定区域截图static func getCropImage(_ view:UIView,size:CGSize) -> UIImage {//false是透 ...
- android camera viewport rect,如何判断元素是否在可视区域ViewPort
个性签名: 生如夏花,逝如冬雪:人生如此,何悔何怨. 前言: 经常需要计算元素的大小或者所在页面的位置,offsetWidth,clientWidth,scrollWidth,scrollTop这几个 ...
最新文章
- Base62x比Base64的编码速度更快吗?
- Python标准模块—Regular Expressions
- Linux -- free 命令 内存适用状态监控
- placeholder如何实现换行
- Leetcode每日一题:58.length-of-last-word(最后一个单词的长度)
- 【深度优先搜索】计蒜客:置换的玩笑
- 学习libpcap库,写例子代码--tcp_config.txt
- linux登陆命令行,如何使用linux命令行登陆
- 卖文玩微商怎么引流?在泛滥的人群中脱颖而出
- php 阿里云短信接口 demo最新
- 中芯国际的喜与忧:扣非后盈利后将脱帽,管理层分歧、有进有退
- OpenCV/kornia/Pillow/Halcon/NI Vision/MIL/*计算机视觉资料汇总
- mysql怎么限制输入男女_excel表格中如何限制只输入男女
- 程序员女朋友的相册是这样的
- android 5.0合并分区,中兴天极2 S291合并分区刷Android 5.1刷机教程
- idea开启远程调试
- 渣土车空车未盖盖识别系统 OpenCv
- 店宝宝:淘宝特价版直面拼多多靠的是什么?
- 手摸手教会你三次握手和四次挥手
- sidirect 连接西门子_如何配置DASSIDirect与西门子