当前位置:网站首页>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
边栏推荐
- Computational Thinking and economic thinking
- Shardingsphere proxy 4.1 sub database and sub table
- Set the location permission in the shutter to "always allow"
- 如何自制一个安装程序,将程序打包生成安装程序的办法
- Bug- solve the display length limitation of log distinguished character encoding (edittext+lengthfilter)
- Ruiji takeout project (II)
- Unique Wulin, architecture selection manual (including PDF)
- tokenizers>=0.11.1,!=0.11.3,<0.13 is required for a normal functioning of this module,
- Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
- 字符串 实现 strStr()
猜你喜欢
manhattan_ Slam environment configuration
Basic use and cluster construction of consult
Notes on writing questions in C language -- monkeys eat peaches
Redis(一)原理与基本使用
manhattan_slam环境配置
How do wechat sell small commodity programs do? How to open wechat apps to sell things?
Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
瑞萨RA系列-开发环境搭建
Modbus protocol and serialport port read / write
i++ 和 ++i的真正区别
随机推荐
Experience in writing C
Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
How to "transform" small and micro businesses (II)?
依赖属性、依赖附加属性以及类型转换
manhattan_ Slam environment configuration
Gradle download warehouse is slow
What should be paid attention to in PMP examination?
Repo sync will automatically switch the correspondence between the local branch and the remote branch - how to customize this behavior
Webapi performance optimization
Computational Thinking and economic thinking
IdentityServer4 定义概念
原生小程序开发注意事项总结
Flask博客实战 - 实现侧边栏最新文章及搜索
MySQL create given statement
Modbus协议与SerialPort端口读写
JS tool function, self encapsulating a throttling function
How much does a small program cost? How much does a small program cost? It's clear at a glance
Neat Syntax Design of an ETL Language (Part 2)
MySQL source code reading (II) login connection debugging
String longest common prefix