In the use of "NetEase cloud Music", found that there is a display "desktop lyrics" function, so think of their own implementation. Check the information, is realized with Windowmanage. The implementation process also appeared some problems, read the official documents, also solved. Put a simple code on it.
public class Mainactivity extends Activity implements view.ontouchlistener{MyView MyView; WindowManager WM; Windowmanager.layoutparams Layoutparams; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); WM = (WindowManager) getapplicationcontext (). Getsystemservice (Context.window_service); Set the properties of TextView layoutparams = new Windowmanager.layoutparams (); Layoutparams.width = WindowManager.LayoutParams.WRAP_CONTENT; Layoutparams.height = WindowManager.LayoutParams.WRAP_CONTENT; Here is the key to make the control always at the top layoutparams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY; Layoutparams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; This gravity also can not be less, otherwise, the following "move lyrics" when there will be a problem ~ can try [official website documentation is explained] Layoutparams.gravity = gravity.left| Gravity.top; Create a custom TextView MyView = new MyView (this); Myview.settext ("Test Touch"); Myview.settextcolor (Color.Black); Myview.setbackgroundcolor (Color.White); Monitor the OnTouch event in order to implement the "Move lyrics" function Myview.setontouchlistener (this); Wm.addview (MyView, layoutparams); } @Override public boolean OnTouch (View V, motionevent event) {switch (event.getaction ()) {case MOTIONEVENT.ACTION_UP://getrawx/y is to obtain a coordinate position relative to the device note difference getx/y[relative to view] Layoutparams.x = (i NT) event.getrawx (); LAYOUTPARAMS.Y = (int) Event.getrawy (); Update the location of the "desktop lyrics" Wm.updateviewlayout (myview,layoutparams); The following removeview can remove the "desktop lyrics"//wm.removeview (MyView); Break Case motionevent.action_move:layoutparams.x = (int) event.getrawx (); LAYOUTPARAMS.Y = (int) Event.getrawy (); Wm.updateviewlayout (Myview,layoutparams); Break } return false; }//Inherit TextView Well, seems a bit superfluous, in fact, directly with TextView is good public class MyView extends textview{public MyView (Context cont EXT) {super (context); } }}
Complete code: Https://github.com/MummyDing/DisplayLyricsonDesktop
"Reprint Please specify the source"
Author:mummyding
Source: http://blog.csdn.net/mummyding/article/details/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Android" desktop lyrics hover effect simple implementation