当前位置:网站首页>Floating window --- create an activity floating window (can be dragged)
Floating window --- create an activity floating window (can be dragged)
2022-06-25 10:10:00 【Peach doesn't come out】
Floating window --- establish Activity Floating window ( Drag )
Based on the present activity Create a floating window , Life cycle and activity binding , Jump to the next activity Will be covered
establish activity Floating window
【1】 obtain activity The root layout of
activity The root layout is FrameLayout, Its id by content
FrameLayout rootLayout = getWindow().getDecorView().findViewById(android.R.id.content);stay main_activity.xml Simply write down the layout in , Expect to add a custom... On the upper layer of the white green junction view

【2】 Custom layout controls , Add it to the root layout , Add by unique identification view
Create a file called layout_simple_floating.xml file , Simply add a imageview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ivFloatingImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/floating_img" />
</LinearLayout>through LayoutInflater.from(context).inflate(int layout, ViewGroup root) Method to dynamically load the layout , And add LayoutParams Parameters ; adopt addView(Viiew view) To add a custom layout to the root layout
/**
* In three steps
* [1] First judge whether the current has been created tag Corresponding view, If tag Empty or created view Don't deal with it
* [2] obtain activity Root layout
* [3] Custom control layout , Add it to the root layout
*
* @param tag identification view
*/
private void buildSimpleFloatingWindow(String tag) {
if (TextUtils.isEmpty(tag) || floatingMap == null || floatingMap.containsKey(tag)) {
Log.v(TAG, "tag Is empty or view Created ");
return;
}
FrameLayout rootLayout = getWindow().getDecorView().findViewById(android.R.id.content);
View view = LayoutInflater.from(this).inflate(R.layout.layout_simple_floating, null);
if (rootLayout == null || view == null) {
Log.v(TAG, " The root layout or custom layout is empty , could not be built ");
return;
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
, ViewGroup.LayoutParams.MATCH_PARENT);
params.gravity = Gravity.LEFT + Gravity.TOP;
params.topMargin = 1000;
params.leftMargin = 380;
view.setLayoutParams(params);
rootLayout.addView(view);
floatingMap.put(tag, view);
}Add a method to control its display and hiding
private void showFloatingView(String tag) {
if (TextUtils.isEmpty(tag) || floatingMap == null || !floatingMap.containsKey(tag)) {
Log.v(TAG, "tag Is empty or view Not created , Unable to display ");
return;
}
floatingMap.get(tag).setVisibility(View.VISIBLE);
}
private void hideFloatingView(String tag) {
if (TextUtils.isEmpty(tag) || floatingMap == null || !floatingMap.containsKey(tag)) {
Log.v(TAG, "tag Is empty or view Not created , Can't hide ");
return;
}
floatingMap.get(tag).setVisibility(View.INVISIBLE);
}
【3】 Add a sliding effect to the custom layout
rewrite OnTouchListener
Drag process for finger press (ACTION_DOWN) --> Drag the (ACTION_MOBE) --> Fingers off (ACTION_UP)
public class FloatingTouchListener implements View.OnTouchListener {
private static final String TAG = "FloatingTouchListener";
private int downX;
private int downY;
private int downRawX;
private int downRawY;
private int screenWidth;
private int screenHeight;
private IFloatingDragListener mListener;
public interface IFloatingDragListener {
void onFloatingClick();
void onFloatingDrag();
}
public FloatingTouchListener(View view, IFloatingDragListener listener) {
WindowManager wm = (WindowManager) view.getContext().getSystemService(Context.WINDOW_SERVICE);
this.screenWidth = wm.getDefaultDisplay().getWidth();
this.screenHeight = wm.getDefaultDisplay().getHeight();
this.mListener = listener;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v == null) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = (int) event.getX();
downY = (int) event.getY();
downRawX = (int) event.getRawX();
downRawY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) event.getX() - downX;
int moveY = (int) event.getY() - downY;
int left, top, right, bottom;
left = v.getLeft() + moveX;
top = v.getTop() + moveY;
right = left + v.getWidth();
bottom = top + v.getHeight();
// Boundary treatment
if (left < 0) {
left = 0;
right = left + v.getWidth();
}
if (right > screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if (top < 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom > screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
break;
case MotionEvent.ACTION_UP:
int upX = (int) event.getRawX();
int upY = (int) event.getRawY();
if (Math.abs(upX - downRawX) < ViewConfiguration.get(v.getContext()).getScaledTouchSlop() &&
Math.abs(upY - downRawY) < ViewConfiguration.get(v.getContext()).getScaledTouchSlop()) {
Log.v(TAG, " Click event ");
if (mListener != null) {
mListener.onFloatingClick();
}
} else {
Log.v(TAG, " Drag events ");
if (mListener != null) {
mListener.onFloatingDrag();
}
}
break;
}
return true;
}
}Before build Add... To the code of touchlistener The sentence of , A callback pops up toast

The effect is as follows :
establish activity Floating window , Control can slide
Enclosed demo Address : https://download.csdn.net/download/qq_38110571/13756987
边栏推荐
- Rxjs TakeUntil 操作符的学习笔记
- JS【中高级】部分的知识点我帮你们总结好了
- WebApi性能优化
- Grabcut image segmentation in opencv
- Oracle查询自带JDK版本
- Kotlin keyword and operator
- 什么是 CRA
- The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores
- Notes on writing questions in C language -- monkeys eat peaches
- i++ 和 ++i的真正区别
猜你喜欢

CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕

Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days

Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL

How to build a wechat applet? How to open an applet?

Get started quickly with jetpack compose Technology

8. Intelligent transportation project (1)

How to do the wechat selling applet? How to apply for applets

Webapi performance optimization
![[MySQL learning notes 21] storage engine](/img/3a/a3cd573281efc689cafdb7d7562ce0.png)
[MySQL learning notes 21] storage engine

链表 删除链表中的节点
随机推荐
Exception: gradle task assemblydebug failed with exit code 1
瑞吉外卖项目(二)
Gradle download warehouse is slow
Modbus协议与SerialPort端口读写
Flask博客实战 - 实现侧边栏最新文章及搜索
Mongodb's principle, basic use, clustering and partitioned clustering
Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
原生小程序开发注意事项总结
Why should the terminal retail industry choose the member management system
Neat Syntax Design of an ETL Language (Part 2)
Etcd tutorial - Chapter 4 etcd cluster security configuration
Shuttle JSON, list, map inter transfer
Puzzle (019.2) hexagonal lock
Modbus protocol and serialport port read / write
(forwarding articles) after skipping multiple pages, shuttle returns to the first page and passes parameters
Guiding principle - read source code
How to apply for a widget on wechat how to get a widget on wechat
How much does a small program cost? How much does a small program cost? It's clear at a glance
Force buckle -104 Maximum depth of binary tree
Learning notes of rxjs takeuntil operator