Christmas, write a program to practice practicing ———— Android Full interface suspension button implementation _android

Source: Internet
Author: User

At first I thought the suspension window, may use the Android to have Popupwindow to realize, although also realizes, but the limitation is very big. For example, Popupwindow must have a carrier view, that is, must be specified on the top of that view to implement. The view is the relative position to display the Popupwindow. This limits its intelligence in the User interaction window, the relative display. Instead of being able to drag the position freely and display it on the desktop.

So I looked up some data and there were two ways to implement it. One is that a custom toast,toast is run over all interfaces, meaning that no interface can overwrite it. The other is the Android Compatmodewrapper class, Conmpatmodewrapper is the base class, and most of the functionality is its internal class Windowmanagerimpl. The object can be obtained by getapplication (). Getsystemservice (Context.window_service). (Note: If it is obtained through Activity.getsystemservice (Context.window_service), it is only the localwindowmanager that belongs to the activity).

After a brief introduction, we look directly at the code implementation, and the comment is already written in the code.

Mainactivity.java
package Com.example.floatviewdemo;
Import Com.example.floatviewdemo.service.FloatViewService;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
public class Mainactivity extends activity {
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
  }
  @Override
 protected void OnStart () {
  Intent Intent = new Intent (mainactivity.this, Floatviewservice.class); c15/>//start Floatviewservice 
     startservice (intent); 
 Super.onstart ();
 }
 
 @Override
 protected void OnStop () {
 //Destroy suspension window
 Intent Intent = new Intent (Mainactivity.this, Floatviewservice.class); 
    Termination of Floatviewservice 
    StopService (intent); 
    Super.onstop ();
 }

Activity_main.xml

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"
  xmlns:tools= "http:// Schemas.android.com/tools "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android:background= "#fff"
  android:paddingbottom= "@dimen/activity_vertical_margin"
  android: paddingleft= "@dimen/activity_horizontal_margin"
  android:paddingright= "@dimen/activity_horizontal_margin"
  android:paddingtop= "@dimen/activity_vertical_margin"
  tools:context= " Com.example.floatviewdemo.MainActivity ">
  <textview
    android:layout_width=" Wrap_content "
    android:layout_height= "Wrap_content"
    android:text= "@string/hello_world"/>
</RelativeLayout>

Service class for realizing suspension window function

Package com.example.floatviewdemo.service;
Import COM.EXAMPLE.FLOATVIEWDEMO.R;
Import Android.annotation.SuppressLint;
Import Android.app.Service;
Import android.content.Intent;
Import Android.graphics.PixelFormat;
Import Android.os.IBinder;
Import Android.util.Log;
Import android.view.Gravity;
Import Android.view.LayoutInflater;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.View.OnTouchListener;
Import Android.view.WindowManager;
Import Android.view.WindowManager.LayoutParams;
Import Android.widget.ImageButton;
Import Android.widget.LinearLayout;
Import Android.widget.Toast; 
  public class Floatviewservice extends Service {private static final String TAG = "Floatviewservice"; 
  Define floating window layouts private linearlayout mfloatlayout; 
  Private Windowmanager.layoutparams Wmparams; 
  Create a floating window set the object of the layout parameter private WindowManager Mwindowmanager; 
  Private ImageButton Mfloatview; @Override public void OnCreate ()  
  {super.oncreate (); 
    LOG.I (TAG, "onCreate");    
  Createfloatview (); @SuppressWarnings ("Static-access") @SuppressLint ("Inflateparams") private void Createfloatview () {Wmparams 
    = new Windowmanager.layoutparams (); The Windowmanagerimpl.compatmodewrapper Mwindowmanager = (WindowManager) getApplication () is obtained by getapplication. Getsystemservice (Getapplication (). 
    Window_service);  
    Sets the window type wmparams.type = Layoutparams.type_phone;  
    Set the picture format, the effect is transparent Wmparams.format = pixelformat.rgba_8888 background;    
    Set the floating window not to be focused (to implement operations other than the visible window outside the floating window) wmparams.flags = layoutparams.flag_not_focusable; Adjust Suspension window display dock position for left top wmparams.gravity = Gravity.left |     
    Gravity.top; 
    In the upper left corner of the screen as the origin, set the x, y initial values, relative to gravity wmparams.x = 0; 
    WMPARAMS.Y = 152; 
    Set the suspension window long width data wmparams.width = WindowManager.LayoutParams.WRAP_CONTENT; 
    Wmparams.height = WindowManager.LayoutParams.WRAP_CONTENT; Layoutinflater Inflater = LAYOUTINFLAter.from (Getapplication ()); 
    Gets the layout mfloatlayout = (linearlayout) inflater.inflate (R.layout.alert_window_menu, null) of the floating window view; 
    Add Mfloatlayout Mwindowmanager.addview (Mfloatlayout, wmparams);
    Floating window button Mfloatview = (ImageButton) Mfloatlayout.findviewbyid (R.ID.ALERT_WINDOW_IMAGEBTN); 
        Mfloatlayout.measure (View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED), View.measurespec 
    . Makemeasurespec (0, View.MeasureSpec.UNSPECIFIED));
  Sets the touch movement Mfloatview.setontouchlistener (New Ontouchlistener () {Boolean isclick) listening to the floating window; @SuppressLint ("clickableviewaccessibility") @Override public boolean Ontouch (View V, motionevent event) {switch (even
   T.getaction ()) {case MotionEvent.ACTION_DOWN:mFloatView.setBackgroundResource (r.drawable.circle_red);
   Isclick = false;
  Break
   Case MotionEvent.ACTION_MOVE:isClick = true; GETRAWX is the coordinates of the touch position relative to the screen, Getx is the coordinates of the button wmparams.x = (int) event.getrawx ()-MFloatview.getmeasuredwidth ()/2;
   Minus 25 is the height of the status bar WMPARAMS.Y = (int) Event.getrawy ()-mfloatview.getmeasuredheight ()/2-75;
   Refresh Mwindowmanager.updateviewlayout (Mfloatlayout, wmparams);
  return true;
   Case MotionEvent.ACTION_UP:mFloatView.setBackgroundResource (R.drawable.circle_cyan);
  Return isclick;//This returns false to the move event, and returns true to release the event, which can be started by clicking No.
  Default:break;
  return false;  
    }
    }); 
        Mfloatview.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) { Toast.maketext (Floatviewservice.this, "Don't give me 100 bucks!") 
      ", Toast.length_short). Show (); 
  } 
    }); 
    @Override public void OnDestroy () {Super.ondestroy (); 
    if (mfloatlayout!= null) {//Remove suspension window Mwindowmanager.removeview (mfloatlayout);
 @Override public IBinder onbind (Intent Intent) {return null; } 
}

The XML file for the floating window

Alert_window_menu.xml
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout "xmlns:android=" http:/
/schemas.android.com/apk/res/android "
  android:layout_width=" wrap_content "
  android:layout_height=" Wrap_ Content "
  android:orientation=" vertical ">
  <imagebutton
    android:id=" @+id/alert_window_imagebtn " 
    android:layout_width=" 50DP "
    android:layout_height=" 50DP "
    android:background=" @drawable/float_ Window_menu "
    android:contentdescription=" @null "
    />
</LinearLayout>

The above is the implementation of the Android full interface suspension button of the full description, I hope you like.

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.