Floating forms for android Development

Source: Internet
Author: User

Floating forms for android Development

 

Suddenly interested in the floating form, a small Demo was made to query the information, the effect is to click the button, close the current Activity, display the floating window, the window can be dragged, double-click to disappear. As follows:

Its usage principle is very simple. It is implemented by the management class WindowManager.
1. First, add the permission to AndroidManifest. xml:

 

2. Implementation of floating window layout

 
Public class extends toplayout extends LinearLayout {public extends toplayout (Context context) {super (context); setOrientation (LinearLayout. VERTICAL); // horizontal arrangement // Set width and height this. setLayoutParams (new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); View view = LayoutInflater. from (context ). inflate (R. layout. lateral Layout, null); this. addView (view );}
 

3. display it in activity.

 
// Obtain the system form mWindowManager = (WindowManager) getApplicationContext (). getSystemService (window); // The layout style of the Form. mLayout = new WindowManager. layoutParams (); // set the display type of the Form -- TYPE_SYSTEM_ALERT (system prompt) mLayout. type = WindowManager. layoutParams. TYPE_SYSTEM_ALERT; // set the form focus and touch: // FLAG_NOT_FOCUSABLE (key input focus cannot be obtained) mLayout. flags = WindowManager. layoutParams. FLAG_NOT_FOCUSABLE; // sets the display mode mLayout. format = PixelFormat. RGBA_8888; // sets the Alignment Method mLayout. gravity = Gravity. TOP | Gravity. LEFT; // set the form width and height mLayout. width = WindowManager. layoutParams. WRAP_CONTENT; mLayout. height = WindowManager. layoutParams. WRAP_CONTENT;
 

The MainActivity code is as follows:

Package com. yc. yc_suspendingform; import android. app. activity; import android. graphics. pixelFormat; import android. graphics. rect; import android. OS. bundle; import android. util. log; import android. view. gravity; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; import android. view. windowManager; import android. widget. button; import com. yc. yc_floatingform.R; public class MainActivity extends Activity {private WindowManager mWindowManager; private WindowManager. layoutParams mLayout; private DesktopLayout mDesktopLayout; private long startTime; // declare the width and height of the screen float x, y; int top; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); createWindowManager (); createmediatoplayout (); Button btn = (Button) findViewById (R. id. btn); btn. setOnClickListener (new OnClickListener () {public void onClick (View v) {showDesk () ;}) ;}/ *** create a floating form */private void createmediatoplayout () {mnames toplayout = new names toplayout (this); mnames toplayout. setOnTouchListener (new OnTouchListener () {float mTouchStartX; float mTouchStartY; @ Override public boolean onTouch (View v, MotionEvent event) {// obtain the coordinates of the relative screen, that is, x = event is taken as the origin point in the upper left corner of the screen. getRawX (); y = event. getRawY ()-top; // 25 is the height Log of the system status bar. I (startP, startX + mTouchStartX + === startY + mTouchStartY); 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 (); Log. I (startP, startX + mTouchStartX + === startY + mTouchStartY); long end = System. currentTimeMillis ()-startTime; // if (end <300) {closeDesk () ;}starttime = System. currentTimeMillis (); break; case MotionEvent. ACTION_MOVE: // update the floating Window Location Parameter mLayout. x = (int) (x-mTouchStartX); mLayout. y = (int) (y-mTouchStartY); mWindowManager. updateViewLayout (v, mLayout); break; case MotionEvent. ACTION_UP: // update the floating Window Location Parameter mLayout. x = (int) (x-mTouchStartX); mLayout. y = (int) (y-mTouchStartY); mWindowManager. updateViewLayout (v, mLayout); // you can record the last position in mTouchStartX = mTouchStartY = 0; break;} return true ;}});} @ Override public void onWindowFocusChanged (boolean hasFocus) {super. onWindowFocusChanged (hasFocus); Rect rect = new Rect (); // obtain the entire view. Note that if you want to set the title style, this must appear after the title style, otherwise, an error occurs in getWindow (). getDecorView (). getWindowVisibleDisplayFrame (rect); top = rect. top; // The height of the status bar, so rect. height, rect. width is the width of the system height Log. I (top, + top);}/*** display writable toplayout */private void showDesk () {mWindowManager. addView (mDesktopLayout, mLayout); finish () ;}/ *** close javastoplayout */private void closeDesk () {mWindowManager. removeView (mDesktopLayout); finish () ;}/ *** set WindowManager */private void createWindowManager () {// obtain the system form mWindowManager = (WindowManager) getApplicationContext (). getSystemService (window); // The layout style of the Form. mLayout = new WindowManager. layoutParams (); // set the display type of the Form -- TYPE_SYSTEM_ALERT (system prompt) mLayout. type = WindowManager. layoutParams. TYPE_SYSTEM_ALERT; // set the form focus and touch: // FLAG_NOT_FOCUSABLE (key input focus cannot be obtained) mLayout. flags = WindowManager. layoutParams. FLAG_NOT_FOCUSABLE; // sets the display mode mLayout. format = PixelFormat. RGBA_8888; // sets the Alignment Method mLayout. gravity = Gravity. TOP | Gravity. LEFT; // set the form width and height mLayout. width = WindowManager. layoutParams. WRAP_CONTENT; mLayout. height = WindowManager. layoutParams. WRAP_CONTENT ;}}

 


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.