Android top-level floating window

Source: Internet
Author: User

First, let's look at the Code Description AndroidManifest.. xml [html] After the effects are not extended. <? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "cn. hpc. assistant" android: versionCode = "1" android: versionName = "1.0"> <! -- The following permissions are required --> <uses-permission android: name = "android. permission. SYSTEM_ALERT_WINDOW "/> <uses-sdk android: minSdkVersion =" 8 "android: targetSdkVersion =" 14 "/> <uses-permission android: name =" android. permission. GET_TASKS "/> <application android: name = ". floatApplication "android: allowBackup =" true "android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <act Ivity android: name = "cn. hpc. assistant. mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest> App class [java] package cn. hpc. assistant; import android. app. application; import android. view. windowManager; public class FloatAppl Ication extends Application {private WindowManager. layoutParams wmParams = new WindowManager. layoutParams (); public WindowManager. layoutParams getWmParams () {return wmParams;} MainActivity class [java] package cn. hpc. assistant; import android. app. activity; import android. content. context; import android. graphics. pixelFormat; import android. graphics. point; import android. OS. bundle; import android. view. Gravity; import android. view. windowManager; import android. view. windowManager. layoutParams; public class MainActivity extends Activity {private WindowManager wm = null; private WindowManager. layoutParams wmParams = null; private FloatFrame myFV = null; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // creates a suspension. Floating window createFloatView (); this. finish (); // After the suspended window is displayed, the Activity automatically exits.} private void createFloatView () {Point windowSize = new Point (); this. getWindowManager (). getdefadisplay display (). getSize (windowSize); myFV = new FloatFrame (getApplicationContext (); // obtain WindowManager wm = (WindowManager) getApplicationContext (). getSystemService (Context. WINDOW_SERVICE); // set the parameter wmParams = (FloatApplic Ation) getApplication ()). getWmParams ();/*** The following are WindowManager. for more information about LayoutParams attributes, see the SDK Documentation */wmParams. type = 2003; // 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;/** the effect of the flags attribute is the same as "locked ". The floating window cannot be touched. It does not accept any events and does not affect subsequent event responses. * WmParams. flags = LayoutParams. FLAG_NOT_TOUCH_MODAL | * LayoutParams. FLAG_NOT_FOCUSABLE | LayoutParams. FLAG_NOT_TOUCHABLE; */wmParams. gravity = Gravity. LEFT | Gravity. TOP; // adjust the floating window to the upper left corner. // wmParams. gravity = Gravity. CENTER_VERTICAL | Gravity. RIGHT; // adjust the floating window to the upper left corner // set the default display position // wmParams. x = 0; <span style = "font-family: Arial, Helvetica, sans-serif;"> // start with the upper left corner of the screen, set the initial values of x and y </span> // wmParams. y = 0; wmP Arams. x = windowSize. x; <span style = "font-family: Arial, Helvetica, sans-serif;"> // to the right of the screen, right away from </span> wmParams. y = windowSize. y/2; // set wmParams for the long and wide data of the floating window. width = android. view. viewGroup. layoutParams. WRAP_CONTENT; // 40; wmParams. height = android. app. actionBar. layoutParams. WRAP_CONTENT; // 40; // display the myFloatView image wm. addView (myFV, wmParams) ;}} FloatFrame class [java] package cn. hpc. assistant; import java. u Til. arrayList; import java. util. list; import android. app. activityManager; import android. app. activityManager. runningTaskInfo; import android. content. componentName; import android. content. context; import android. content. intent; import android. content. pm. packageManager; import android. content. pm. resolveInfo; import android. OS. handler; import android. OS. message; import android. text. textUtils; import Android. util. log; import android. view. gestureDetector; import android. view. motionEvent; import android. view. view; import android. view. windowManager; import android. widget. button; import android. widget. linearLayout; public class FloatFrame extends LinearLayout {private float mTouchRawX; private float mTouchRawY; private WindowManager wm = (WindowManager) getContext (). getApplicationContext (). getSy StemService (Context. WINDOW_SERVICE); // This wmParams is the obtained global variable, used to save the suspended window attribute private WindowManager. layoutParams wmParams = (FloatApplication) getContext (). getApplicationContext ()). getWmParams (); private Context mContext; View viewExtFrame; Button btnExt; public FloatFrame (Context context) {super (context); mContext = context; gestureDetector = new GestureDetector (context, gestureListener ); view view = View. inflate (context, R. layout. float_layout_main, null); viewExtFrame = view. findViewById (R. id. id_float_ext_frame); btnExt = (Button) view. findViewById (R. id. id_btn_ext); int ids [] = {R. id. id_btn_ext, R. id. id_btn_exit}; for (int id: ids) {view. findViewById (id ). setOnClickListener (onClick);} view. findViewById (R. id. id_btn_exit ). setOnTouchListener (frameOnTouchListener); this. addView (view);} privat E void clickView (int id) {switch (id) {case R. id. id_btn_ext: int visible = viewExtFrame. getVisibility (); if (visible = View. VISIBLE) {viewExtFrame. setVisibility (View. INVISIBLE); btnExt. setText ("<");} else {viewExtFrame. setVisibility (View. VISIBLE); btnExt. setText (">");} break; case R. id. id_btn_exit: wm. removeView (FloatFrame. this); // click to exit and destroy the suspended window Log. e ("float view", "exit"); break; default:} // Click View in response to the Button in the floating window. onClickListener onClick = new View. onClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub clickView (v. getId () ;}}; final static int MESSAGE_MOVE = 0x1001; final static int MESSAGE_DOWN = 0x1002; final static int MESSAGE_UP = 0x1003; handler mHandler = new Handler () {@ Override public void handleMessage (Message msg) {switch (MS G. what) {case MESSAGE_MOVE: updateViewPosition (); break; case MESSAGE_DOWN: break; case MESSAGE_UP: break; default :}}; // click Listener OnTouchListener frameOnTouchListener = new OnTouchListener () {public boolean onTouch (View v, MotionEvent event) {if (null = gestureDetector | null = event) {return false;} return gestureDetector. onTouchEvent (event) ;}}; // GestureDetector for Gesture Recognition. onGestureL Istener gestureListener = new GestureDetector. onGestureListener () {@ Override public boolean onDown (MotionEvent e) {// TODO Auto-generated method stub Log. d ("OnGestureListener", "onDown"); mTouchRawX = e. getX (); mTouchRawY = e. getY (); return false ;}@ Override public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {return true ;}@ Override public void onLongPres S (MotionEvent e) {// TODO Auto-generated method stub // Log. d ("OnGestureListener", "onLongPress") ;}@ Override public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {mTouchRawX = e2.getRawX (); mTouchRawY = e2.getRawY (); mHandler. sendEmptyMessage (MESSAGE_MOVE); return true ;}@ Override public void onShowPress (MotionEvent e) {// Log. d ("OnGestureListener", "onSh OwPress ") ;}@ Override public boolean onSingleTapUp (MotionEvent e) {// Log. d ("OnGestureListener", "onSingleTapUp"); return false ;}}; private GestureDetector gestureDetector; private void updateViewPosition () {// update the floating window position parameter wmParams. x = (int) mTouchRawX; wmParams. y = (int) mTouchRawY; wm. updateViewLayout (this, wmParams);} floating window layout file: [html] <LinearLayout xmlns: android = "http://schemas.android.co M/apk/res/android "xmlns: tools =" http://schemas.android.com/tools "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: orientation =" horizontal "> <! -- Display and hide extended containers --> <LinearLayout android: id = "@ + id/id_float_ext_frame" android: layout_width = "wrap_content" android: layout_height = "600dip" android: background = "@ android: drawable/alert_dark_frame" android: orientation = "vertical" android: visibility = "gone"> <ImageView android: layout_width = "wrap_content" android: layout_height = "fill_parent" android: src = "@ drawable/ic_launcher"/> </LinearLayout> <Button android: id = "@ + id/id_btn_ext" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "... "android: background =" @ android: drawable/ic_dialog_dialer "android: textColor =" # FF00FF00 "/> <Button android: id =" @ + id/id_btn_exit "android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "exit" android: textColor = "# FF0000FF"/> </LinearLayout>

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.