Android uses WindowManager to make a drag-and-drop control _android

Source: Internet
Author: User

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.

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.