Android Super Simple Drag button hover Button Snap Button float button

Source: Internet
Author: User
Tags gety

First Kind

The second Kind

The first method of implementation

XML layout

<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:orientation= "vertical" >    <button        android:id= "@+id/startbtn"        android:layout_width = "35DP"        android:layout_height= "35DP"        android:layout_centerinparent= "true"        android:background= "@ Drawable/addstock "/></relativelayout>


Activity call

Displaymetrics DM = getresources (). Getdisplaymetrics (); screenwidth = Dm.widthpixels;screenheight = dm.heightPixels;// Toast.maketext (Getactivity (), screenwidth + "= =" + screenheight + "="//+ vheight, 0). Show ();//Drag button btn = (Button) view.f Indviewbyid (R.ID.STARTBTN); Btn.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {// TODO auto-generated Method Stubif (bool) {Latestnetdatapackage lnpackage = new Latestnetdatapackage (r.string.command_ Latestnet, Fundkind, Sorttype,requesttype, Curpage, Fund_count, 0);d atapackage = Lnpackage; Networktaskmanager.addrequesttorequestcache (Lnpackage, Hlistviewfragment.this, Hlistviewfragment.this, Getactivity ()); Btn.setbackgroundresource (r.drawable.deletestock); bool = false;} else {latestnetdatapackage lnpackage = new Latestnetdatapackage (r.string.command_latestnet, FundKind, SortType, RequestType, Curpage, Fund_count, 1);d atapackage = Lnpackage; Networktaskmanager.addrequesttorequestcache (Lnpackage, Hlistviewfragment.this, hlistViewfragment.this,getactivity ()); Btn.setbackgroundresource (r.drawable.addstock); bool = True;}}); Btn.setontouchlistener (New Ontouchlistener () {int lastx, lasty;//record move last position private int Btnheight;public Boolean OnTouch (View V, motionevent event) {//get actionint EA = event.getaction (); switch (EA) {case Motionevent.action_down://Press LASTX = (int) event.getrawx (); lasty = (int) event.getrawy (); screenwidth = View.getwidth (); screenheight = View.getheight ( ); btnheight = Btn.getheight ();//Toast.maketext (Getactivity (), "Action_down:" + Lastx + ",//" + lasty, 0). Show (); Break;ca SE motionevent.action_move://Move//move in dynamic set position int dx = (int) event.getrawx ()-Lastx;int dy = (int) Event.getrawy ()-Lasty ; int left = V.getleft () + dx;int top = v.gettop () + Dy;int right = V.getright () + Dx;int bottom = V.getbottom () + dy;if (l EFT < 0) {left = 0;right = left + V.getwidth ();} if (right > ScreenWidth) {right = Screenwidth;left = Right-v.getwidth ();} if (Top < 0) {top = 0;bottom = top + v.getheight ();} if (Bottom > screenheight) {bottom = Screenheight;top = Bottom-v.getheight ();} V.layout (left, top, right, bottom);//Toast.maketext (Getactivity (), "position:" + Left + "," +//Top + "," + Right + ", + Bottom, 0)//. Show ()//Set the current position again LASTX = (int) event.getrawx (); lasty = (int) event.getrawy (); Break;case motionevent.a CTION_UP://lift//Inward adsorption//int dx1 = (int) event.getrawx ()-Lastx;//int dy1 = (int) Event.getrawy ()-Lasty;//int left1 = V . GetLeft () + dx1;//int Top1 = v.gettop () + dy1;//int right1 = v.getright () + dx1;//int bottom1 = V.getbottom () + dy1;//if (Left1 < (SCREENWIDTH/2)) {//if (Top1 <) {//v.layout (left1, 0, right1, btnheight);/} else if (Bottom1 > (screenHeight-200)) {//v.layout (Left1, (screenheight-btnheight), right1, screenheight),//} else {//v.layout (0, Top1, Btnheight, bottom1);//}//} else {/ /if (Top1 <) {//v.layout (left1, 0, right1, btnheight);/} else if (Bottom1 > (screenHeight-200)) {//v.layout (l EFT1, (Screenheight-btnheigHT), right1, screenheight);/} else {//v.layout ((screenwidth-btnheight), Top1, ScreenWidth, bottom1);//}//}//break;} return false;}});

View refers to the layout

The second method of implementation

XML layout

<com.ui.view.dragframelayout        android:id= "@+id/becausefloat"        android:layout_width= "Fill_parent        " android:layout_height= "0.0DP"        android:layout_gravity= "center_vertical"        android:layout_weight= "1" >        <framelayout            android:id= "@+id/container"            android:layout_width= "Match_parent"            android: layout_height= "0DP"            android:layout_weight= "1"/>        <imageview            android:id= "@+id/dragimg            " Android:layout_width= "35DP"            android:layout_height= "35DP"            android:background= "@drawable/deletestock"/ >    </com.ui.view.DragFrameLayout>


Custom Control Com.ui.view.DragFrameLayout

Package Com.ui.view;import Com.ui.r;import Android.content.context;import android.graphics.rect;import Android.location.location;import Android.util.attributeset;import Android.util.log;import Android.view.motionevent;import Android.view.view;import Android.view.viewconfiguration;import Android.widget.framelayout;public class Dragframelayout extends Framelayout {private View view;private int width, heigh; private int Screenwid, screenhei;private boolean Isclickdrag = false;private Boolean Istouchdrag = false;private float STA RtX, starty;private checkclick checkclick = new Checkclick ();p rivate dragimageclicklistener dragimagelistener;public Dragimageclicklistener Getdragimagelistener () {return dragimagelistener;} public void Setdragimagelistener (Dragimageclicklistener dragimagelistener) {This.dragimagelistener = Dragimagelistener;} Public interface Dragimageclicklistener {public abstract void OnClick (); Private class Checkclick implements Runnable {@Overridepublic void Run () {//TODO auto-Generated method Stubisclickdrag = false; LOG.I ("Drag", "=====checktap====");}} Public Dragframelayout (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);//TODO Auto-g Enerated Constructor Stub}public dragframelayout (context context, AttributeSet Attrs) {Super (context, attrs);//TODO Auto-generated constructor stub}public void Draginit (view view) {Screenwid = GetWidth (); Screenhei = GetHeight (); width = VI Ew.getwidth (); heigh = View.getheight ();} @Overridepublic boolean onintercepttouchevent (motionevent ev) {//TODO auto-generated method Stubswitch (Ev.getaction () {case MotionEvent.ACTION_DOWN:float x = Ev.getx (); Float y = ev.gety (); Rect frame = new rect (), if (view = = null) {view = Findviewbyid (r.id.dragimg);d raginit (view);} View.gethitrect (frame), if (Frame.contains (int) (x), (int) (y))) {Istouchdrag = True;startx = X;starty = Y;return true;} break;} return false;} @Overrideprotected void OnLayout (Boolean changed, int left, int top, int right,int bottom) {Super. OnLayout (changed, left, top, right, bottom);} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubfloat x = Event.getx (); float y = Event.gety (); Rect frame = new rect (), switch (Event.getaction ()) {case MotionEvent.ACTION_DOWN:view.getHitRect (frame);  Frame.contains ((int) (x), (int) (y))) {StartX = X;starty = Y;istouchdrag = True;isclickdrag = true;postdelayed (Checkclick, Viewconfiguration.gettaptimeout ());} Break;case MotionEvent.ACTION_MOVE:float Distanx = Math.Abs (x-startx); float Distany = Math.Abs (Y-starty); if (math.sqrt (Distany * distany + Distanx * Distanx) >) {Isclickdrag = false;} Move (x, y); Break;case MotionEvent.ACTION_CANCEL:isClickDrag = False;istouchdrag = False;break;case motionevent.action _up:if (Isclickdrag = = True) {Dragimagelistener.onclick (); Removecallbacks (Checkclick);} Isclickdrag = False;istouchdrag = false;//This is the control adsorption four weeks//if (x > Width && x < screenwid-width && Y & Gt heigh//&& Y < SCReenhei-heigh) {//int Mintype = mindistance (x, y);//LOG.I ("tags", Screenhei + "==mintype=" + Mintype);//switch (MinType ) {//case left://x = width;//break;//case right://x = screenwid-width;//break;//case top://y = heigh;//break;//case BOTT om://y = Screenhei-heigh;//break;//default://break;//}//move (x, y);//}break;case motionevent.action_outside: Isclickdrag = False;istouchdrag = False;break;} return true;}  Private final static int left = 1;private final static int right = 2;private final static int TOP = 3;private final static int BOTTOM = 4;private int mindistance (float x, float y) {log.i ("tags", "x=" + x + "==y=" + y); Boolean left, top;if (x &L T;= (Screenwid-x)) {left = true;} else {left = false;} if (y <= (screenhei-y)) {top = true;} else {top = false;} if (left&&top) {if (x<=y) {return left;} Else{return TOP;}} if (left&& (!top)) {if (x<= (screenhei-y)) {return left;} Else{return BOTTOM;}} if ((!left) &top) {if ((screenwid-x) <= y) {return right;} Else{return TOP;}}if ((!left) & (!top)) {if ((screenwid-x) <= (screenhei-y)) {return right;} Else{return BOTTOM;}} return 0;} private void Move (float x, float y) {int left = (int.) (X-WIDTH/2), int top = (int) (Y-HEIGH/2), if (left <= 0) lef t = 0;if (top <= 0) top = 0;if (Left > screenwid-width) left = screenwid-width;if (Top > Screenhei-heigh) Top = Screenhei-heigh; Framelayout.layoutparams params = (framelayout.layoutparams) view.getlayoutparams ();p Arams.setmargins (left, top, ( Screenwid-left-width), (Screenhei-top-heigh)); View.setlayoutparams (params); Requestlayout ();} Public double getdistance (double lat1, double lon1, double lat2, double lon2) {float[] results = new FLOAT[1]; Location.distancebetween (LAT1, Lon1, LAT2, Lon2, results); return results[0];}}


Activity Invocation Method

Dragimg = (ImageView) Findviewbyid (r.id.dragimg); framelayout = (dragframelayout) Findviewbyid (r.id.becausefloat); Framelayout.setdragimagelistener (New Dragimageclicklistener () {private Boolean isdaix; @Overridepublic void OnClick () {//TODO auto-generated method stubif (Isdaix) {dragimg.setbackgroundresource (r.drawable.deletestock); IsDaix = false;} else {dragimg.setbackgroundresource (r.drawable.addstock); Isdaix = true;} Toast.maketext (Mainactivity.this, "click", Toast.length_long). Show ();});

Android Super Simple Drag button hover Button Snap Button float button

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.