"Android" desktop lyrics hover effect simple implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.