Android floating window thing

    Whether it is a small ball on the desktop, floating lyrics, or a real-time monitoring window, etc., these are all called floating windows here. Although they look different, they are the same in essence. I think these are accessories, like glasses, earrings, or tattoos worn by beauties. Only with these beauties can they be more beautiful. Then some people will argue that "clear water comes out of hibiscus, and it is natural to carve." I can only answer you this way, there are very few people who can reach this level, let alone things.

    No more gossip, let's get down to business.
    First of all, we need to paste a View on the window. Of course, we must be able to manage this window, just like the notification bar of the old man who guards the door. You do not have management rights. Even if you paste a small advertisement, the old man will still tear you up. Lose. That makes sense, so

mWindowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
We must be able to manage windows.
Then how do we paste
		mLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
				LayoutParams.TYPE_SYSTEM_ERROR, LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSPARENT);
		mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
To set the size, position, transparency, etc. we want to paste.
then how to paste
	private void refreshView() {
		
		if (isViewAdd) {
			mWindowManager.updateViewLayout(mView, mLayoutParams);
		} else {
			mWindowManager.addView(mView, mLayoutParams);
			isViewAdd = true;
		}
	}
If it has been pasted, we just need to update it, and if it is not pasted, we can only post it.
What is the last post
mView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.floating, null);
This is what we are going to post. As for what is in the floating layout, you can write whatever you need, and you can do whatever you want. If you have imagination you can make beautiful carvings.
For example, if we want to make a floating control panel that controls music, we can define three ImageViews in our layout, one for the previous song, one for play and pause, and one for the next song
		mPre = (ImageView)mView.findViewById(R.id.pre);
		mStartStop = (ImageView)mView.findViewById(R.id.start_stop);
		mNext = (ImageView)mView.findViewById(R.id.next);

		mNext.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					mNext.setBackgroundColor(0xff0000ff);
					mContext.sendBroadcast(new Intent("com.android.music.musicservicecommand.next"));
				}else if(event.getAction() == MotionEvent.ACTION_UP){
					mNext.setBackgroundColor(0x88000000);
				}
				return true;
			}
		});

Then when we click to complete our task, it’s as simple as that, but such a window can only be displayed, which is meaningless, so first of all, we need to make it move

		mView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					
					mTouchStartX = event.getX();
					mTouchStartY = event.getY();
					break;
				
				case MotionEvent.ACTION_MOVE:
					
					refreshView((int)(event.getRawX() - mTouchStartX), (int)(event.getRawY() - mTouchStartY));
					break;
					
				case MotionEvent.ACTION_UP:
					
					mTouchStartX = 0;
					mTouchStartY = 0;
					break;
				}
				return false;
			}
		});

This is the code we dragged, of course we also posted the refreshView inside
	private void refreshView(int x, int y) {
		
		if (statusBarHeight == 0) {
			
			View rootView = mView.getRootView();
			Rect r = new Rect();
			rootView.getWindowVisibleDisplayFrame(r);
			statusBarHeight = r.top;
		}
		
		mLayoutParams.x = x;
		mLayoutParams.y = y - statusBarHeight;
		refreshView();
	}

Of course, we can't drag the floating window to the status bar.
Such a floating window exists in every interface. If we only want to display it on the desktop, what should we do? This is also simple. We just need to know when it is the desktop, and then
    private void showView(){
    	if(isEnd){
    		mView.setVisibility(View.GONE);
    	}else{
    		if(isShowAll){
    			mView.setVisibility(View.VISIBLE);
    		}else{
    			if(isCurLauncher){
    				mView.setVisibility(View.VISIBLE);
    			}else{
    				mView.setVisibility(View.GONE);
    			}
    		}
    	}
    }

We just need to hide and reveal our floating window. So how do we judge that it is on the desktop? There are many ways, if you are doing scheme design, it is simple, just make a mark on the activity that enters the desktop, and then detect that the mark has changed, just like updating the floating window; if you are doing it alone apk, there is a way

	class CheckTopActivityTast extends AsyncTask<Object, Object, String> {

		@Override
		protected String doInBackground(Object... arg0) {
			// TODO Auto-generated method stub

			String top_activity = null;
			boolean islauncher = false;
			while (true) {
				top_activity = getTopActivityClassName();
				if(top_activity.contains("launcher")){
					islauncher = true;
				}else{
					islauncher = false;
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
				if(isCurLauncher == islauncher){
					continue;
				}else{
					isCurLauncher = islauncher;
					Message msg = new Message();
					msg.what = 1111;
					mHandler.sendMessage(msg);
				}
			}
		}
		
	}
	
    private synchronized String getTopActivityClassName(){  
    	ActivityManager mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); 
    	List<RunningTaskInfo>  tasksInfo = mActivityManager.getRunningTasks(1);  
    	if(null!=tasksInfo&&tasksInfo.size() > 0){  
    		return tasksInfo.get(0).topActivity.getClassName();  
    	}  
    	return "";  
    } 

Just start a thread and keep observing the top activity of the top activity. If it is the desktop Launcher, just update it, it's that simple. Of course, we all do this in the service, so the floating window is the floating window.

Complete code http://download.csdn.net/detail/zhiyuan263287/6861095

Floating windows are just such a thing, and then it is a matter of one's creativity. How much imagination you have, you can make such a wonderful design.



Guess you like

Origin blog.csdn.net/zhiyuan263287/article/details/18448335