跳至主要内容

android draw on top danmaku

    private void createFloatView(){
        final Button button=new Button(getApplicationContext());
        button.setText("hello");
        final WindowManager windowManager=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        params.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        params.format= PixelFormat.RGBA_8888;
        params.flags=WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        params.width=100;
        params.height=100;
//        button.setOnTouchListener(new View.OnTouchListener() {
//            int lastx,lasty;
//            int paramx,paramy;
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                switch (event.getAction()){
//                    case MotionEvent.ACTION_DOWN:
//                        lastx=(int)event.getRawX();
//                        lasty=(int)event.getRawY();
//                        paramx=params.x;
//                        paramy=params.y;
//                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        int dx=(int)event.getRawX()-lastx;
//                        int dy=(int)event.getRawY()-lasty;
//                        params.x=paramx+dx;
//                        params.y=paramy+dy;
//                        windowManager.updateViewLayout(button,params);
//                        break;
//                }
//                return true;
//            }
//        });        windowManager.addView(button,params);
    }
    public final static int REQUEST_CODE = 101;

    public void checkDrawOverlayPermission() {
        /** check if we already  have permission to draw over other apps */        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(getApplicationContext())) {
                /** if not construct intent to request permission */                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                /** request permission via start activity for result */                startActivityForResult(intent, REQUEST_CODE);
            }
        }
    }

    @Override    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        /** check if received result code         is equal our requested code for draw permission  */        if (requestCode == REQUEST_CODE) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.canDrawOverlays(this)) {
                    // continue here - permission was granted                    createFloatView();
                }
            }
        }
    }

评论

此博客中的热门博文

android hide actionbar

public class MainActivity extends Activity { ActionBar actionBar; //声明ActionBar @Override protected void onCreate( Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView( R .layout.activity_main); actionBar = getSupportActionBar(); //得 到ActionBar actionBar.hide(); //隐藏ActionBar } }

go url encoding

func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits. func  QueryUnescape func QueryUnescape (s string ) ( string , error ) QueryUnescape does the inverse transformation of QueryEscape, converting %AB into the byte 0xAB and '+' into ' ' (space). It returns an error if any % is not followed by two hexadecimal digits.