Android to achieve a magnetic suspension form effect _android

Source: Internet
Author: User
Tags abs sqlite database

This article illustrates the effect of Android with a magnetically suspended form. Share to everyone for your reference, specific as follows:

Magnetic suspension form, similar to 360 green villain

The main achievement is :

1. Suspend all forms above
2. Attractive, adsorbed on the edge of the screen
3. Have click Effect

Below I realize the above three points, simple encapsulation of a floatview

First look at the demo's effect chart, and then look at the code,

Effect Chart :

The Floatview code is as follows

Package Com.manymore13.flowwindowdemo;
Import Android.content.Context;
Import Android.graphics.PixelFormat;
Import Android.graphics.Rect;
Import Android.util.AttributeSet;
Import Android.util.DisplayMetrics;
Import Android.util.Log;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.WindowManager;
Import Android.view.WindowManager.LayoutParams;
Import Android.widget.ImageView;
  /** * @author manymore13 * @version 1.0/public class Floatview extends imageview{private float mtouchx;
  private float Mtouchy;
  private float X;
  private float y;
  private int startx;
  private int starty;
  private context C;
  private int imgid = R.drawable.ic_launcher;
  private int controlledspace = 20;
  private int screenwidth;
  Boolean isshow = false;
  Private Onclicklistener Mclicklistener;
  Private WindowManager WindowManager;
  Private Windowmanager.layoutparams windowmanagerparams = new Windowmanager.layoutparams (); Public Floatview (Context, ATtributeset attrs) {Super (context, attrs);
    Public Floatview (context c) {super (c);
  Initview (c); }//Initialize form public void Initview (context c) {WindowManager = (WindowManager) c.getapplicationcontext (). Getsystem
    Service (Context.window_service);
    ScreenWidth = Windowmanager.getdefaultdisplay (). GetWidth ();
    This.setimageresource (Imgid);
    Windowmanagerparams.type = Layoutparams.type_phone; Windowmanagerparams.format = pixelformat.rgba_8888; Background transparent windowmanagerparams.flags = Layoutparams.flag_not_touch_modal |
    layoutparams.flag_not_focusable; Adjust the suspension window to the upper left corner for easy adjustment of coordinates windowmanagerparams.gravity = Gravity.left |
    Gravity.top;
    Set the x, y initial value windowmanagerparams.x = 0 with the upper-left corner of the screen as the origin.
    WINDOWMANAGERPARAMS.Y = 200;
    Set the suspension window long width data windowmanagerparams.width = layoutparams.wrap_content;
  Windowmanagerparams.height = layoutparams.wrap_content;
  The public void Setimgresource (int id) {imgid = ID; } @Override Public Boolean ontouchevent (Motionevent event) {x = Event.getrawx ();
    y = Event.getrawy ();
        Switch (event.getaction ()) {case Motionevent.action_down: {Mtouchx = Event.getx ();
        Mtouchy = Event.gety ();
        STARTX = (int) event.getrawx ();
        Starty = (int) Event.getrawy ();
      Break
        Case Motionevent.action_move: {updateviewposition ();
      Break Case MOTIONEVENT.ACTION_UP: {if (Math.Abs (X-STARTX) < Controlledspace && Math.Abs (y-s Tarty) < Controlledspace) {if (Mclicklistener!= null) {Mclicklistener.onclick
          (this);
        } log.i ("tag", "x=" +x+ "startx+" +startx+ "y=" +y+ "starty=" +starty);
        if (x <= screenwidth/2) {x = 0;
        }else{x = screenwidth;
        } updateviewposition ();
      Break
  } return Super.ontouchevent (event); }
  //Hide the form public void hide () {if (isshow) {Windowmanager.removeview (this);
    Isshow = false; The form is public void show () {if (Isshow = False) {Windowmanager.addview (this, windowmanagerpar
      AMS);
    Isshow = true;
  @Override public void Setonclicklistener (Onclicklistener l) {this.mclicklistener = l;
     private void Updateviewposition () {//update floating window Position parameter windowmanagerparams.x = (int) (X-MTOUCHX);
     WINDOWMANAGERPARAMS.Y = (int) (y-mtouchy); Windowmanager.updateviewlayout (this, windowmanagerparams);

 Refresh Display}}

Full Instance code click here Download the site .

If you need to use the above class to do this

Floatview = new Floatview (this); Create Form
Floatview.setonclicklistener (this);//Set event, you need to implement Floatview in the OnClick interface
floatview.show ();//Show The form
floatview.hide ();//Hide Form

PS do not forget to add permissions in the manifest (more manifest functions and permissions can refer to this site:Android manifest Features and permissions description Daquan : http://tools.jb51.net/table/AndroidManifest)

There's a bug on the top, and the view moves down when clicked.

public int getstatusbarheight () {
    int titlebarheight = 0;
    Rect frame = new Rect ();
    Mact.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
    Titlebarheight = Frame.top;
    if (Titlebarheight = = 0) {
      int resourceid = Getresources (). Getidentifier ("Status_bar_height", "Dimen", "Android"); C7/>if (ResourceID > 0) {
        titlebarheight = getresources (). Getdimensionpixelsize (ResourceID);
      }
    return titlebarheight;
}

For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Resources Operating Skills Summary", "Android File Operating skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", The Android View view tips summary and the Android Control usage summary

I hope this article will help you with the Android program.

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.