当前位置:网站首页>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

 

 

原网站

版权声明
本文为[Peach doesn't come out]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200545120863.html