The effect chart is as follows
First step: new Dragview inheritance relativelayout
Package com.rong.activity;
Import COM.RONG.TEST.R;
Import Android.content.Context;
Import Android.graphics.Color;
Import Android.graphics.PixelFormat;
Import Android.util.AttributeSet;
Import android.view.Gravity;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.WindowManager;
Import Android.widget.Button;
Import Android.widget.RelativeLayout; public class Dragview extends Relativelayout {private WindowManager windowmanager;//for a floating window that can be dragged private windowmanager.l
Ayoutparams windowparams;//Floating window parameters private Button MyButton;
Public Dragview (context, AttributeSet attrs) {Super (context, attrs);
Init ();
private void Init () {view.inflate (GetContext (), r.layout.layout_my, this);
MyButton = new Button (GetContext ());
Mybutton.settext ("my");
Mybutton.setbackgroundcolor (color.red);
@Override public boolean ontouchevent (Motionevent event) {//Get the xy position int currentx = (int) event.getx () of the current point;
int currenty = (int) event.gety (); Switch(Event.getaction ())
{case MotionEvent.ACTION_DOWN:if (WindowManager = = null) {Setwindowparams (CurrentX, currenty);
WindowManager = (WindowManager) getcontext (). Getsystemservice (Context.window_service);
Windowmanager.addview (MyButton, windowparams);
} break;
Case motionevent.action_move:windowparams.x = CurrentX;
Windowparams.y = CurrentY;
Windowmanager.updateviewlayout (MyButton, windowparams);
Break
Case MOTIONEVENT.ACTION_UP://Windowmanager.removeview (MyButton);
Break
return true;
"private void Setwindowparams (int x, int y) {//create thumbnail of item windowparams = new Windowmanager.layoutparams (); windowparams.gravity = Gravity.top |
gravity.left;//This must add//Get preview the upper left corner relative to the screen coordinates windowparams.x = x;
Windowparams.y = y;
Set width and height windowparams.width = 200;
Windowparams.height = 200; Windowparams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
Windowparams.format = pixelformat.translucent;
windowparams.windowanimations = 0;
}
}
Step Two: create a new layout file Activity_main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
android:id=" @+id/main_touchlayout "
android:layout_width=" Match_parent "
android: layout_height= "Match_parent"
android:background= "#ffffff"
android:orientation= "vertical" >
< Com.rong.activity.DragView
android:id= "@+id/main_touchview"
android:layout_width= "Match_parent"
android:layout_height= "match_parent"
android:layout_centerinparent= "true"
android:background= " #ff0000 "/>
</RelativeLayout>
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.