悬浮窗的实现

作者: 瑟闻风倾 | 来源:发表于2018-10-12 14:10 被阅读19次

    1. FloatWindowView类

    package comi.example.liy.firstbasicproject.entityClass;
    
    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import comi.example.liy.firstbasicproject.R;
    
    /**
     * Created by liy on 2018-10-12 13:09
     */
    public class FloatWindowView extends LinearLayout {
        private WindowManager windowManager;
        private TextView textView1;
        private TextView textView2;
    
        public static int viewWidth;
        public static int viewHeight;
    
        private int inputStartX = 0;
        private int inputStartY = 0;
        private int viewStartX = 0;
        private int viewStartY = 0;
        private int inMovingX=0;
        private int inMovingY=0;
        private WindowManager.LayoutParams mParams;
    
        public FloatWindowView(Context context) {
            super(context);
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            LayoutInflater.from(context).inflate(R.layout.float_window,this);
            View view = findViewById(R.id.float_window_layout);
            textView1 = (TextView) view.findViewById(R.id.accumulative_consumption);
            textView2 = (TextView)view.findViewById(R.id.accumulative_quantity);
            viewHeight = view.getLayoutParams().height;
            viewWidth = view.getLayoutParams().width;
        }
        @Override
        public boolean  onTouchEvent(MotionEvent event){
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    inputStartX = (int)event.getRawX();
                    inputStartY = (int)event.getRawY();
                    viewStartX = mParams.x;
                    viewStartY = mParams.y;
                    break;
                case MotionEvent.ACTION_MOVE:
                    inMovingX = (int)event.getRawX();
                    inMovingY = (int)event.getRawY();
                    updatePosition();
                    break;
                default:
                    break;
            }
            Log.v("X-Y",
                    "inputStartX: "+inputStartX+"\n"
                            +"inputStartY: "+inputStartY+"\n"
                            +"viewStartX: "+viewStartX+"\n"
                            +"viewStartY: "+viewStartY+"\n"
                            +"inMovingX: "+inMovingX+"\n"
                            +"inMovingY: "+inMovingY+"\n");
            return super.onTouchEvent(event);
        }
    
        private void updatePosition(){
            mParams.x =viewStartX + inMovingX - inputStartX;
            mParams.y =viewStartY + inMovingY- inputStartY;
            windowManager.updateViewLayout(this,mParams);
        }
    
        public void setmParams( WindowManager.LayoutParams params){
            mParams = params;
        }
    
        public void setData(String data1,String data2){
            textView1.setText(data1);
            textView2.setText(data2);
        }
    }
    

    2. float_window.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#99149FEB"
        android:padding="5dp"
        android:layout_gravity="center"
        android:id="@+id/float_window_layout">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="本月消费(元):"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:textSize="@dimen/character_3"/>
            <TextView
                android:id="@+id/accumulative_consumption"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:textSize="@dimen/character_3"
                android:text="" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:textSize="@dimen/character_3"
                android:text="累计笔数(笔):" />
            <TextView
                android:id="@+id/accumulative_quantity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="@dimen/character_3"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:text="" />
        </LinearLayout>
    </LinearLayout>
    

    3. FloatWindowActivity类

    package comi.example.liy.firstbasicproject.ui.activity;
    
    import android.app.Activity;
    import android.graphics.PixelFormat;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.Gravity;
    import android.view.WindowManager;
    
    import comi.example.liy.firstbasicproject.R;
    import comi.example.liy.firstbasicproject.entityClass.FloatWindowView;
    import comi.example.liy.firstbasicproject.persist.Settings;
    
    /**
     * Created by liy on 2018-10-12 13:15
     */
    public class FloatWindowActivity extends Activity {
        private Settings settings;//设置
        private FloatWindowView floatWindowView;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_float_window);
            settings = Settings.getInstance(this);
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            creatFloatWindowView();
            showFloatWindowData("200","1000");
        }
    
        @Override
        protected void onStop() {
            getWindowManager().removeView(floatWindowView);
            floatWindowView = null;
            super.onStop();
        }
    
        //创建悬浮窗
        private void creatFloatWindowView(){
            WindowManager windowManager = getWindowManager();
            int screenHeight = windowManager.getDefaultDisplay().getHeight();
            int screenWidth = windowManager.getDefaultDisplay().getWidth();
            WindowManager.LayoutParams params = new WindowManager.LayoutParams();
            if(floatWindowView == null){
                floatWindowView = new FloatWindowView(this);
                params.type = WindowManager.LayoutParams.TYPE_APPLICATION;
                params.format = PixelFormat.RGBA_8888;
                params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
                params.gravity = Gravity.START| Gravity.TOP;
                params.width =FloatWindowView.viewWidth;
                params.height = FloatWindowView.viewHeight;
                params.x = screenWidth;
                params.y = screenHeight/2;
                floatWindowView.setmParams(params);
                windowManager.addView(floatWindowView,params);
            }
        }
    
        //显示悬浮窗数据
        public void showFloatWindowData(String data1,String data2){
            floatWindowView.setData(data1,data2);
        }
    
    }
    

    4. activity_float_window.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    </LinearLayout>
    

    5. 显示效果如下图所示,移动手指可上下左右滑动。

    floatWindow.png

    相关文章

      网友评论

        本文标题:悬浮窗的实现

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