[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.