Android Mobile Phone Suspension window small case _android

Source: Internet
Author: User

This example for you to share the Android nine Sudoku picture display of the specific code for your reference, the specific content as follows

– Main Page Face--–

A button public
class Mainactivity extends activity {

 @Override
 protected void onCreate in the layout (Bundle Savedinstancestate) {
  super.oncreate (savedinstancestate);
  Setcontentview (r.layout.activity_main);
  Findviewbyid (R.ID.BTN). Setonclicklistener (New View.onclicklistener () {
   @Override public
   void OnClick (View View) {
   //The purpose is to start the service to open the suspension form
    StartService (new Intent (Mainactivity.this, Floatservice.class));
    Finish ();}}
  );

 }


-service Open the Suspend form ——-

/**
 * Description: Open Floatview by service *
 Author: LDM
 * Time: 20162016/8/17 14:03
 * Email: 1786911211@qq.com
 */Public
class Floatservice extends Service {
 @Override public
 void OnCreate () {
  super.oncreate ( );
  Customviewmanager.getinstance (This). Showfloatviewonwindow ();

 @Override public
 IBinder onbind (Intent Intent) {return
  null;
 }
}

-Suspension Forms management tools Class ——-

/** * Description: * Author: LDM * Time: 20162016/8/17 11:57 * Email: 1786911211@qq.com/public class Customviewmanager {//Up
 The following private context Mcontext;
 This class instance private static Customviewmanager instance;
 Custom Floatview private Floatview Mfloatview;

 Window Management class private WindowManager Mwindowmanager;
  Private Customviewmanager {this.mcontext = context;
  Mfloatview = new Floatview (mcontext);
 Mwindowmanager = (WindowManager) mcontext.getsystemservice (Context.window_service); /** * @param * @description Get instance object through Singleton mode * @author LDM * @time 2016/8/17 11:59/public static CUSTOMVIEWM Anager getinstance (Context Mcontext) {if (null = = instance) {synchronized (Customviewmanager.class) {if (null
    = = Instance) {instance = new Customviewmanager (mcontext);
 }} return instance; /** * @param * @description Displays a custom Floatview * @author LDM * @time 2016/8/17 13:47/public void show on the mobile phone screen Floatviewonwindow () {WindowmanaGer.
  Layoutparams parmas = new Windowmanager.layoutparams ();
  Parmas.width = Mfloatview.getfloatwidth ();
  Parmas.height = Mfloatview.getfloatheight (); Window pattern Placement Position parmas.gravity = Gravity.left |
  Gravity.center;
  If the Gravity property is omitted, it represents the absolute x position of the window.
  parmas.x = 0;
  If the Gravity property is omitted, it represents the absolute y position of the window.
  Parmas.y = 0; Phone window. It is used for telephone interaction (especially inbound).
  It is placed above all applications, under the status bar.
  Parmas.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; Flag_not_focusable lets windows not get the focus so that users cannot send key events and button events to the window quickly//flag_not_touch_modal even when the window has the focus
  Any event other than the window is still sent to other windows after the window.
  Parmas.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; The desired bitmap format. Default is opaque.
  Reference Android.graphics.PixelFormat.
  Parmas.format = pixelformat.rgba_8888;
 Mwindowmanager.addview (Mfloatview, Parmas);

 }
}

-Custom Floatview ——-

/** * Description: * Author: LDM * Time: 20162016/8/17 11:17 * Email: 1786911211@qq.com * * public class Floatview extends View {
 Floating ball width private int floatwidth = 150;
 Suspended ball height private int floatheight = 150;
 Floating ball brush private Paint mpaint;
 Draw a text brush private Paint mtextpaint;

 Private String Text = "50%";
  Public Floatview {Super (context);
 Initpaint ();
 public int getfloatwidth () {return floatwidth;
 public int getfloatheight () {return floatheight;
  Public Floatview (context, AttributeSet attrs) {Super (context, attrs);
 Initpaint ();
  Public Floatview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
 Initpaint (); /** * @param * @description Initialization brush * @author LDM * @time 2016/8/17 11:37/private void Initpaint () {//Set
  Set the suspended ball brush mpaint = new Paint ();
  Mpaint.setcolor (Color.green);
  Mpaint.setantialias (TRUE);
  Mpaint.setdither (TRUE); Set Text Brush mtextpaint = new Paint ();
  Mtextpaint.settextsize (25);
  Mpaint.setantialias (TRUE);
  Mtextpaint.setcolor (Color.White);
 Mtextpaint.setfakeboldtext (TRUE); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {//Set size setmeasureddimension (flo
 Atwidth, Floatheight); /** * @param * @description Drawing pattern * @author LDM * @time 2016/8/17 11:44/@Override protected void OnDraw (
  Canvas Canvas) {Super.ondraw (Canvas);
  Draw suspended Ball canvas.drawcircle (FLOATWIDTH/2, FLOATHEIGHT/2, FLOATWIDTH/2, Mpaint);
  Draw Text Paint.fontmetrics metrics = Mtextpaint.getfontmetrics ();
  Text size calculation can refer to: http://mikewang.blog.51cto.com/3826268/871765/float textWidth = mtextpaint.measuretext (text);
  float x = floatwidth/2-TEXTWIDTH/2;
  float dy =-(metrics.descent + metrics.ascent)/2;
  Float y = floatheight/2 + dy;
 Canvas.drawtext (text, x, y, mtextpaint);


 }
}

Finally, do not forget to add permissions in the Androidmanifest.xml:

 <!--add permissions-->
 <uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>


And of course, remember to add a service component

 <service android:name= ". Service. Floatservice "/>


Some mobile phone running, found that we do not appear to the suspension of the form, you can enter the mobile Phone Settings center, click Apply Settings, in the specified application permission settings to open the corresponding set of suspension form switch.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.