[Android] Simple implementation of desktop lyrics floating effect, android floating

Source: Internet
Author: User

[Android] Simple implementation of desktop lyrics floating effect, android floating

When using "Netease cloud music", I found a function to display "desktop lyrics", so I thought about implementing it myself. I checked the information and implemented it using WindowManage. Some problems also occurred in the implementation process. After reading the official documentation, the solution was also solved. Paste the simple code here.

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 TextView attribute layoutParams = new WindowManager. layoutParams (); LayoutParams. width = WindowManager. layoutParams. WRAP_CONTENT; layoutParams. height = WindowManager. layoutParams. WRAP_CONTENT; // the key here, so that the control is always at the top of the 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 cannot be small. Otherwise, the following" Something went wrong when moving the lyrics ~ You can try [instructions on the official website] layoutParams. gravity = Gravity. LEFT | Gravity. TOP; // create custom TextView myView = new MyView (this); myView. setText ("Test Touch"); myView. setTextColor (Color. BLACK); myView. setBackgroundColor (Color. WHITE); // listen to the OnTouch event to achieve the "Mobile 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 used to obtain the coordinate position relative to the Device. Note the difference between getX/Y and layoutParams. x = (int) event. getRawX (); layoutParams. y = (int) event. getRawY (); // update the wm location of "desktop lyrics. updateViewLayout (myView, layoutParams); // The removeView below can remove "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, in fact, it is better to use TextView directly. public class MyView extends TextView {public MyView (Context context) {super (context );}}}

Full: https://github.com/MummyDing/DisplayLyricsonDesktop

[Reprinted with the source]

Author: MummyDing

Source: http://blog.csdn.net/mummyding/article/details/



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.