Android自定义浮框,Android实现全局悬浮框
本文实例为大家分享了Android实现全局悬浮框的具体代码,供大家参考,具体内容如下
效果图:
代码实现:
Androidmanifest.xml添加弹框权限
自定义悬浮窗类FloatWindow.java
public class FloatWindow implements View.OnTouchListener {
private Context mContext;
private WindowManager.LayoutParams mWindowParams;
private WindowManager mWindowManager;
private View mFloatLayout;
private float mInViewX;
private float mInViewY;
private float mDownInScreenX;
private float mDownInScreenY;
private float mInScreenX;
private float mInScreenY;
private TextView infoText;
public FloatWindow(Context context) {
this.mContext = context;
initFloatWindow();
}
private void initFloatWindow() {
LayoutInflater inflater = LayoutInflater.from(mContext);
if(inflater == null)
return;
mFloatLayout = (View) inflater.inflate(R.layout.layout_float, null);
infoText = mFloatLayout.findViewById(R.id.textView);
mFloatLayout.setOnTouchListener(this);
mWindowParams = new WindowManager.LayoutParams();
mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
if (Build.VERSION.SDK_INT >= 26) {//8.0新特性
mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}else{
mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
mWindowParams.format = PixelFormat.RGBA_8888;
mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mWindowParams.gravity = Gravity.START | Gravity.TOP;
mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return floatLayoutTouch(motionEvent);
}
private boolean floatLayoutTouch(MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
// 获取相对View的坐标,即以此View左上角为原点
mInViewX = motionEvent.getX();
mInViewY = motionEvent.getY();
// 获取相对屏幕的坐标,即以屏幕左上角为原点
mDownInScreenX = motionEvent.getRawX();
mDownInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext);
mInScreenX = motionEvent.getRawX();
mInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext);
break;
case MotionEvent.ACTION_MOVE:
// 更新浮动窗口位置参数
mInScreenX = motionEvent.getRawX();
mInScreenY = motionEvent.getRawY() - getSysBarHeight(mContext);
mWindowParams.x = (int) (mInScreenX- mInViewX);
mWindowParams.y = (int) (mInScreenY - mInViewY);
// 手指移动的时候更新小悬浮窗的位置
mWindowManager.updateViewLayout(mFloatLayout, mWindowParams);
break;
case MotionEvent.ACTION_UP:
// 如果手指离开屏幕时,xDownInScreen和xInScreen相等,且yDownInScreen和yInScreen相等,则视为触发了单击事件。
if (mDownInScreenX == mInScreenX && mDownInScreenY == mInScreenY){
}
break;
}
return true;
}
public void showFloatWindow(){
if (mFloatLayout.getParent() == null){
DisplayMetrics metrics = new DisplayMetrics();
// 默认固定位置,靠屏幕右边缘的中间
mWindowManager.getDefaultDisplay().getMetrics(metrics);
mWindowParams.x = metrics.widthPixels;
mWindowParams.y = metrics.heightPixels/2 - getSysBarHeight(mContext);
mWindowManager.addView(mFloatLayout, mWindowParams);
}
}
public void updateText(final String s) {
infoText.setText(s);
}
public void hideFloatWindow(){
if (mFloatLayout.getParent() != null)
mWindowManager.removeView(mFloatLayout);
}
public void setFloatLayoutAlpha(boolean alpha){
if (alpha)
mFloatLayout.setAlpha((float) 0.5);
else
mFloatLayout.setAlpha(1);
}
// 获取系统状态栏高度
public static int getSysBarHeight(Context contex) {
Class> c;
Object obj;
Field field;
int x;
int sbar = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
sbar = contex.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return sbar;
}
}
自定义悬浮窗界面布局文件layout_float.xml
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/float_win"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:text="hello"
android:textSize="12sp"
app:layout_constraintLeft_toLeftOf="@id/imageView"
app:layout_constraintRight_toRightOf="@id/imageView"
app:layout_constraintTop_toBottomOf="@id/imageView"/>
在Activity中使用悬浮窗。
public class MainActivity extends AppCompatActivity {
private Button btnShow;
FloatWindow floatWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 权限判断
if (Build.VERSION.SDK_INT >= 23) {
if(!Settings.canDrawOverlays(getApplicationContext())) {
// 启动Activity让用户授权
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + getPackageName()));
startActivityForResult(intent,10);
} else {
// 执行6.0以上绘制代码
initView();
}
} else {
// 执行6.0以下绘制代码
initView();
}
}
@Override
protected void onResume() {
super.onResume();
// 权限判断
if (Build.VERSION.SDK_INT >= 23) {
if(Settings.canDrawOverlays(getApplicationContext())) {
initView();
}
} else {
//执行6.0以下绘制代码
initView();
}
}
private void initView() {
setContentView(R.layout.activity_main);
floatWindow = new FloatWindow(getApplicationContext());
btnShow = findViewById(R.id.btn_show);
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (null != floatWindow) {
floatWindow.showFloatWindow();
}
}
});
Button btnrefresh = findViewById(R.id.btn_refresh);
btnrefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int random = (int) (Math.random() * 10);
if (null != floatWindow) {
floatWindow.updateText(String.valueOf(random));
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != floatWindow) {
floatWindow.hideFloatWindow();
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
Android自定义浮框,Android实现全局悬浮框相关推荐
- android固定悬浮框,Android实现全局悬浮框
本文实例为大家分享了Android实现全局悬浮框的具体代码,供大家参考,具体内容如下 效果图: 代码实现: Androidmanifest.xml添加弹框权限 自定义悬浮窗类FloatWindow.j ...
- android 仿美团悬浮,类似美团悬浮框的效果
具体代码: ublic class MainActivity extends Activity implements OnScrollListener{ /** * 自定义的MyScrollView ...
- 全局悬浮框(只能在app开启式显示)
1.点击事件启动服务 public void startFloatingButtonService(View view) {Log.e("TAG", "startFloa ...
- android 自定义loading,Android自定义动画-StarLoadingView
今天来分享第二个自定义loading的动画,起了个名字叫 蹦跶的星星 ,还是老规矩先介绍,后上图. 实现效果在最后,GIF有点大,手机流量慎重. 介绍 首先声明做这个动画的初衷是为了学习和分享,所以从 ...
- android 自定义命名空间,Android自定义ActionBar实例
本文实例讲述了android自定义actionbar的实现方法.分享给大家供大家参考.具体实现方法如下: android 3.0及以上已经有了actionbar的api,可以通过引入support p ...
- android 自定义图形,Android自定义View之图形图像(模仿360的刷新球自定
概述: 360安全卫士的那个刷新球(姑且叫它刷新球,因为真的不知道叫什么好,不是dota里的刷新球!!),里面像住了水一样,生动可爱,看似简单,写起来不太简单,本例程只是实现了它的部分功能而已,说实话 ...
- android 自定义event,Android运用onTouchEvent自定义滑动布局
写在自定义之前 我们也许会遇到,自定义控件的触屏事件处理,先来了解一下View类中的,onTouch事件和onTouchEvent事件. 1.boolean onTouch(View v, Motio ...
- Android 自定义 圆环,Android自定义view实现圆环效果实例代码
先上效果图,如果大家感觉不错,请参考实现代码. 重要的是如何实现自定义的view效果 (1)创建类,继承view,重写onDraw和onMesure方法 public class CirclePerc ...
- android自定义抽奖,Android自定义view制作抽奖转盘
本文实例为大家分享了Android自定义view制作抽奖转盘的具体代码,供大家参考,具体内容如下 效果图 TurntableActivity package com.bawei.myapplicati ...
最新文章
- 秦刚:积累财富最简单的一种模式:打造一项高收入技能
- 【pmcaff】人人黑产品经理,你中招了嘛!
- python.day05
- 能让你成为更优秀程序员的10个C语言资源
- apache htpasswd.exe创建密码
- MSDN Visual系列:利用关联来过滤MOSS中的BDC数据
- 笔记本电脑风扇声音大怎么办_车子才开几年发动机就发出哒哒哒的声音怎么办?不一定是大问题_搜狐汽车...
- Linux 运维必备的 13 款实用工具,拿好了~
- IB网卡驱动安装以及Infiniband连接带宽测试
- 【3D建模】2020最好用的3款3D建模软件!新手入门必备建模软件!
- 【直观详解】什么是正则化
- 计算机屏幕出现条纹w7,电脑重装win7后屏幕出现条纹怎么办
- 2020移动apn接入点哪个快_手机资讯:5G 网络和现在 iPhone 使用的 Wi-Fi 有什么区别速度会更快吗...
- 【蓝桥杯】 《3W字数总结》 蓝桥杯Java必备基础知识以及国赛真题解析
- Appium APP UI自动化测试框架介绍
- 计算机图形学 旋转平移原理,计算机图形旋转操作详细步骤
- Lambda表达式详解
- App测试中Android和IOS测试区别
- java formula one 用法_使用Formula One生成Excel报表-
- 红外测试操作步骤_红外光谱法测定样品方法
热门文章
- NSCharacterSet 简单用法
- linux shell基础(1)
- 忍者X2简介+安装包+安装环境说明 [复制链接]
- 安卓也是html写的么,css能判断手机是安卓还是ios吗?
- php框架 mysql拼接操作_PHP对数据库MySQL的连接操作
- 接口测试——接口测试流程
- 忘了微信密码怎么办_微信支付密码怎么改?微信支付密码忘了怎么办?详细教程来了!...
- 认真学习系列:《深入理解计算机系统》读书笔记
- word中如何优雅的显示代码
- 线性判别式分析-LDA-Linear Discriminant Analysis