• 悬浮窗效果


    package com.loaderman.customviewdemo;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.graphics.PixelFormat;
    import android.os.Build;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.view.Gravity;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.WindowManager;
    import android.widget.Button;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity implements View.OnTouchListener, View.OnClickListener {
    
    
    
        private Button mCreateWndBtn, mRmvWndBtn;
    
        private ImageView mImageView;
        private WindowManager.LayoutParams mLayoutParams;
        private WindowManager mWindowManager;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                startActivityForResult(myIntent, 100);
            } else {
                initView();
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == 100) {
                initView();
            }
        }
    
        private void initView() {
            mCreateWndBtn = (Button) findViewById(R.id.add_btn);
            mRmvWndBtn = (Button) findViewById(R.id.rmv_btn);
            mCreateWndBtn.setOnClickListener(this);
            mRmvWndBtn.setOnClickListener(this);
    
            mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        }
    
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int rawX = (int) event.getRawX();
            int rawY = (int) event.getRawY();
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_MOVE: {
                    mLayoutParams.x = rawX;
                    mLayoutParams.y = rawY;
                    mWindowManager.updateViewLayout(mImageView, mLayoutParams);
                    break;
                }
                default:
                    break;
            }
            return false;
        }
    
        @Override
        protected void onDestroy() {
            try {
                mWindowManager.removeView(mImageView);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
            super.onDestroy();
        }
    
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.add_btn) {
                mImageView = new ImageView(this);
                mImageView.setBackgroundResource(R.mipmap.ic_launcher);
    
                mLayoutParams = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, 2099,
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED  //控制window的显示特性
                        ,
                        PixelFormat.TRANSPARENT);
                mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
                mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
                mLayoutParams.x = 0;
                mLayoutParams.y = 300;
                mImageView.setOnTouchListener(this);
                mWindowManager.addView(mImageView, mLayoutParams);
            } else if (v.getId() == R.id.rmv_btn) {
                mWindowManager.removeViewImmediate(mImageView);
            }
        }
    }
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/add_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="add view"/>
    
        <Button
            android:id="@+id/rmv_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="remove view"/>
    
    </LinearLayout>

    添加权限:

        <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

    效果:

  • 相关阅读:
    zmap zgrab 环境搭建
    RF是如何工作的?
    RF的优缺点
    国内NLP的那些人那些会
    B-、B+、B*树
    关于LDA的gibbs采样,为什么可以获得正确的样本?
    LDA算法里面Dirichlet分布的两个参数alpha和beta怎样确定?
    如何确定LDA的主题个数
    SMO算法精解
    奇异值与主成分分析(PCA)
  • 原文地址:https://www.cnblogs.com/loaderman/p/10209116.html
Copyright © 2020-2023  润新知