Service创建悬浮框

2019-04-28  本文已影响0人  一young的宠爱

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

利用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>
上一篇 下一篇

猜你喜欢

热点阅读