Android WindowManager implements floating window effect (1) -- bind with the current Activity, windowmanager floating window

Source: Internet
Author: User

Android WindowManager implements floating window effect (1) -- bind with the current Activity, windowmanager floating window

Recently, some students have designed a graduation project and want to use the suspended window. In fact, it is very simple. We can use the system service WindowManager to implement this function, in this chapter, we will try to create a floating view on the current Activity.

Step 1: Get to know WindowManager

L this interface is used to interact with window manager (window manager, application framework layer.

L getSystemService (Context. WINDOW_SERVICE) can be used to obtain the WM instance.

L inheritance relationship

Public interface WindowManager implements ViewManager

L package

Android. view. WindowManager

L important methods

AddView () add view

RemoveView () delete view

UpdateViewLayout () changes the view parameters.

The Window Manager Service is globally unique. It translates user operations into commands and sends them to various windows displayed on the interface. Activity registers the top-level controls to Window Manager. When a user really touches the screen or keyboard, Window Manager notifies the user. When the control has some requests, it will also be sent back to Window Manager through ViewParent. To complete the entire communication process

 

Step 2: override the onTouchEvent method of ImageView

In the previous step, we learned that WindowManager can be added, deleted, and changed. To achieve the drag effect of the floating window, we need to obtain the coordinates of the ImageView.

L obtain the coordinates of the relative screen, that is, starting from the upper left corner of the screen

Float x = event. getRawX ();

Float y = event. getRawY ()-25; // 25 is the height of the system status bar

L use WindowManager. LayoutParams wmParams to set x and y

WmParams. x = (int) (x-mTouchStartX );

WmParams. y = (int) (y-mTouchStartY );

L use the updateViewLayout () method to set the current position of the floating window.

Step 3: add permissions

Add the following permissions to AndroidManifest. xml:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

The effect is as follows:

Important code:CreateMyApplication

Import android. app. application; import android. view. windowManager; public class MyApplication extends Application {/*** create a global variable * Note. add android: name = ". myApplication "Property **/private WindowManager. layoutParams wmParams = new WindowManager. layoutParams (); public WindowManager. layoutParams getMywmParams () {return wmParams ;}}

Create custom View to inherit ImageView

Import android. content. context; import android. util. log; import android. view. motionEvent; import android. view. windowManager; import android. widget. imageView; public class MyFloatView extends ImageView {private float mTouchStartX; private float mTouchStartY; private float x; private float y; private WindowManager wm = (WindowManager) getContext (). getApplicationContext (). getSystemService (Context. WINDOW_SERVICE); // This wmParams is the obtained global variable, used to save the suspended window attribute private WindowManager. layoutParams wmParams = (MyApplication) getContext (). getApplicationContext ()). getMywmParams (); public MyFloatView (Context context) {super (context); // TODO Auto-generated constructor stub} @ Override public boolean onTouchEvent (MotionEvent event) {// obtain the coordinates of the relative screen, that is, starting from the top left corner of the screen x = event. getRawX (); y = event. getRawY ()-25; // 25 is the height Log of the system status bar. I ("currP", "currX" + x + "=== currY" + y); switch (event. getAction () {case MotionEvent. ACTION_DOWN: // obtain the coordinates of the relative View, that is, the upper-left corner of the View is the origin mTouchStartX = event. getX (); mTouchStartY = event. getY (); break; case MotionEvent. ACTION_MOVE: updateViewPosition (); break; case MotionEvent. ACTION_UP: updateViewPosition (); mTouchStartX = mTouchStartY = 0; break;} return true;} private void updateViewPosition () {// update the floating window position parameter wmParams. x = (int) (x-mTouchStartX); wmParams. y = (int) (y-mTouchStartY); wm. updateViewLayout (this, wmParams );}}

Create Activity

Import android. app. activity; import android. content. context; import android. graphics. pixelFormat; import android. OS. bundle; import android. util. log; import android. view. gravity; import android. view. view; import android. view. windowManager; import android. view. view. onClickListener; import android. view. windowManager. layoutParams; public class MyFloatViewActivity extends Activity {private WindowManager wm = null; private WindowManager. layoutParams wmParams = null; private MyFloatView myFV = null; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // create a floating window createView ();} private void createView () {myFV = new MyFloatView (getApplicationContext (); myFV. setImageResource (R. drawable. angry_birds); // obtain WindowManager wm = (WindowManager) getApplicationContext (). getSystemService (Context. WINDOW_SERVICE); // set the parameter wmParams = (MyApplication) getApplication () for LayoutParams (global variable ()). getMywmParams (); wmParams. type = LayoutParams. TYPE_PHONE; // set window type wmParams. format = PixelFormat. RGBA_8888; // set the image format to transparent background. // set Window flag wmParams. flags = LayoutParams. FLAG_NOT_TOUCH_MODAL | LayoutParams. FLAG_NOT_FOCUSABLE; wmParams. gravity = Gravity. LEFT | Gravity. TOP; // adjust the floating window to the upper left corner. // use the upper left corner of the screen as the origin and set the initial values of x and y to wmParams. x = 0; wmParams. y = 0; // set wmParams for the long and wide data of the floating window. width = 40; wmParams. height = 40; // display the myFloatView image wm. addView (myFV, wmParams) ;}@ Override public void onDestroy () {super. onDestroy (); // destroy the suspended window wm when the program exits (the Activity is destroyed. removeView (myFV );}}

L finally, modify the app permissions in the mobile phone during program installation-available in the floating window

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
Copyright Disclaimer: The copyright of this article is shared by Yantai jereh Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the consent of the author and provide the original article connection clearly on the article page, otherwise, you are entitled to pursue legal liability.
Technical Consultation:

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.