美文网首页
Service创建悬浮框

Service创建悬浮框

作者: 一young的宠爱 | 来源:发表于2019-04-28 15:15 被阅读0次

首先,介绍下作用,及功能,

利用service创建悬浮框,然后这个悬浮框不能影响界面其他的按键,所以需要拖动,然后并且放大状态是放在中间,缩小放在左;

注意:自己的布局宽高不能使用FILL_PARENT,以及
wmParams.gravity不一样,然后默认的x,y不一样,所以需要是情况而定,并且拖动更新wmParams的x,y也一样,要视情况而定取值;
例如:
1.wmParams.gravity = Gravity.BOTTOM;
wmParams.x = 0;
wmParams.y =0;
整个布局显示在底部中间位置;
2.wmParams.gravity 为默认值;
wmParams.x = 0;
wmParams.y =0;
整个布局显示在中间位置;

1.创建管理器:
WindowManager mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
2.设置布局:
LayoutParams wmParams = new LayoutParams();
        wmParams.type = LayoutParams.TYPE_PHONE;//设置类型
        //wmParams.type = LayoutParams.LAST_APPLICATION_WINDOW
        wmParams.format = PixelFormat.TRANSLUCENT; //设置形式
        wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;
        //          wmParams.gravity = Gravity.BOTTOM;
        //          wmParams.x = 0; //坐标
        //          wmParams.y = 0;//坐标
        //获取屏的高和宽
    DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        display.getMetrics(dm);
        int widthPixels = dm.widthPixels;
        int heightPixels = dm.heightPixels;
        float density = dm.density;
        screenWidth = (int) (widthPixels * density);
        screenHeight = (int) (heightPixels * density);
                /*设置布局的位置距离底部40dp,水平居中*/
        wmParams.x = 0;
        wmParams.y = screenHeight /2 - 40;
        wmParams.width = LayoutParams.WRAP_CONTENT;
//      wmParams.height = LayoutParams.WRAP_CONTENT;
        // if( heightPixels<500 )
        // if( dm.densityDpi>200 )
        wmParams.height = 70;

3.添加布局:
    LayoutInflater inflater = LayoutInflater.from(getApplication());

        mFloatTopLayout = (RelativeLayout) inflater.inflate(
                R.layout.menu_float_top, null);
floatBig = (ImageView) mFloatTopLayout.findViewById(R.id.iv_float_big);
    floatSmall = (ImageView) mFloatTopLayout.findViewById(R.id.iv_float_small);

    mWindowManager.addView(mFloatTopLayout, wmParams);
4.触摸拖动效果
    floatSmall.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                llFloatBottom.setVisibility(View.GONE);
                floatBig.setVisibility(View.VISIBLE);
//              wmParams.gravity = Gravity.LEFT;
                wmParams.x = (int) (-screenWidth);
                mWindowManager.updateViewLayout(mFloatTopLayout, wmParams);
            /*  if (top.contains("SystemWifiActivity")) {
                    showFloatLeftLayout();
                } else {
                    llFloatBottom.setVisibility(View.GONE);
                    floatBig.setVisibility(View.VISIBLE);
                }*/
            }
        });
    floatBig.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                           llFloatBottom.setVisibility(View.VISIBLE);
                floatBig.setVisibility(View.GONE);
                updateMiddleView();
            }
        });
    floatBig.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return iTouch(v,event);
            }
        });

    private void updateMiddleView() {
        wmParams.x = 0;
        wmParams.y = screenHeight / 2 - MARGIN_BOTTOM;
//      wmParams.gravity = Gravity.CENTER_HORIZONTAL;
        mWindowManager.updateViewLayout(mFloatTopLayout, wmParams);
    }

    private boolean iTouch(View v, MotionEvent event) {
        boolean bret = false;
        Rect frame = new Rect();
        mFloatTopLayout.getWindowVisibleDisplayFrame(frame);
        //int statusBarHeight = frame.top;
        x = event.getRawX();
        y = event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mTouchX = (event.getX()+v.getLeft());
                mTouchY = (event.getY()+v.getTop());
                //mTouchX = event.getX();
                //mTouchY = event.getY();
                bMoved = false;

                x1 = event.getX();
                y1 = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                updateViewPosition();
                bMoved = true;
                break;
            case MotionEvent.ACTION_UP:
                //updateViewPosition();
                mTouchX = mTouchY = 0;
                if( bMoved )
                    bret = true;
                bMoved = false;
                x2 = event.getX();
                y2 = event.getY();

                break;
        }
        return bret;
    }

    private void updateViewPosition() {
        try {
            mLastX = wmParams.x = (int) (x - mTouchX - screenWidth / 2);
            mLastY = wmParams.y = (int) (y - mTouchY - screenHeight / 2);
            Log.e("www", "mTouchX = " + mTouchX + ",mTouchY = " + mTouchY);
            Log.e("www", "x = " + x + ",y = " + y);
            //wmParams.width = LayoutParams.WRAP_CONTENT;
            //wmParams.height = LayoutParams.WRAP_CONTENT;
            mWindowManager.updateViewLayout(mFloatTopLayout, wmParams);
        } catch (Exception e) {

        }
    }


5.补充下这个代码的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/iv_float_big"
        android:visibility="gone"
        android:background="@drawable/selector_video_big"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:id="@+id/ll_float_bottom"
        android:background="@drawable/float_bottom_bg"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/iv_float_home"
           android:background="@drawable/selector_float_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView
            android:id="@+id/iv_float_back"
            android:background="@drawable/selector_float_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView
            android:id="@+id/iv_float_voice"
            android:background="@drawable/selector_float_volum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView
            android:id="@+id/iv_float_clear"
            android:background="@drawable/selector_float_clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <ImageView
            android:id="@+id/iv_float_small"
            android:background="@drawable/selector_float_small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</RelativeLayout>

相关文章

  • Service创建悬浮框

    首先,介绍下作用,及功能, 利用service创建悬浮框,然后这个悬浮框不能影响界面其他的按键,所以需要拖动,然后...

  • 当前App跳转其他App指定的Activity

    还有 当一个App service里跳转其他App Activity时(我是在写悬浮框里的service遇到的)

  • Android视频悬浮窗

    这个悬浮窗是一个类似于微信通话的小屏视频框,利于Service开启和保持。悬浮是利用WindowManager实现...

  • 悬浮框

    使用悬浮框 悬浮窗监听器 悬浮窗管理器

  • 悬浮框

    1.在项目下的build.gradle中 2.添加依赖 3.设置

  • html td鼠标进入显示悬浮框

    鼠标移动到指定框中,显示悬浮框展示指定内容 显示悬浮框的内容 对应的js显示标题内容 function info(...

  • echarts地图下钻右击添加返回上一级

    1.首先在页面中新建一个替代默认右击的悬浮框 2.屏蔽自身的右击事件 3.定位右击悬浮框的位置 3.给悬浮框添加单击事件

  • selenium+python 定位悬浮元素

    背景 在web中,悬浮框的特点是当检测鼠标进入时,发生弹出悬浮框事件,当鼠标退出时,悬浮框在页面上消失。此时,若依...

  • Android 悬浮框

    Android 悬浮框在IM应用或者音视频应用中经常会有用到,这也是我第二次开发这悬浮框这个功能了,为了避免以后再...

  • iOS 悬浮框

    悬浮按钮,先上图 很长时间没写了,手法生疏,直接上代码吧 SheetControlView.h SheetCont...

网友评论

      本文标题:Service创建悬浮框

      本文链接:https://www.haomeiwen.com/subject/ipcryqtx.html