欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android 自定义LineLayout实现满屏任意拖动功能的示例代码

程序员文章站 2023-11-06 17:45:10
1.前言在开发中,会有需求实现控件在屏幕随意拖动,这就需要自定义view,然后在ontouchevent事件中,处理motionevent.action_move事件,然后通过坐标点传值给onlayo...

1.前言

在开发中,会有需求实现控件在屏幕随意拖动,这就需要自定义view,然后在ontouchevent事件中,处理motionevent.action_move事件,然后通过坐标点传值给onlayout方法,来实现控件的任意拖动,具体代码如下:

import android.content.context;
import android.util.attributeset;
import android.view.display;
import android.view.motionevent;
import android.view.windowmanager;
import android.widget.linearlayout;

public class draglinelayout extends linearlayout {

 private int mwidth;
 private int mheight;
 private int mscreenwidth;
 private int mscreenheight;
 private context mcontext;
 private onlocationlistener mlocationlistener;/*listen to the rect */
 //是否拖动
 private boolean isdrag = false;

 public boolean isdrag() {
  return isdrag;
 }

 public dragview(context context, attributeset attrs) {
  super(context, attrs);
  this.mcontext = context;
 }

 @override
 protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
  super.onmeasure(widthmeasurespec, heightmeasurespec);
  mwidth = getmeasuredwidth();
  mheight = getmeasuredheight();
  mscreenwidth = getscreenwidth(mcontext);
  mscreenheight = getscreenheight(mcontext) - getstatusbarheight();
 }

 public int getstatusbarheight() {
  int resourceid = mcontext.getresources().getidentifier("status_bar_height", "dimen", "android");
  return mcontext.getresources().getdimensionpixelsize(resourceid);
 }

 public int getscreenwidth(context context) {
  windowmanager manager = (windowmanager) context
    .getsystemservice(context.window_service);
  display display = manager.getdefaultdisplay();
  return display.getwidth();
 }

 public int getscreenheight(context context) {
  windowmanager manager = (windowmanager) context
    .getsystemservice(context.window_service);
  display display = manager.getdefaultdisplay();
  return display.getheight();
 }

 private float mdownx;
 private float mdowny;


 @override
 public boolean ontouchevent(motionevent event) {
  super.ontouchevent(event);
  if (this.isenabled()) {
   switch (event.getaction()) {
    case motionevent.action_down:
     isdrag = false;
     mdownx = event.getx();
     mdowny = event.gety();
     break;
    case motionevent.action_move:
     final float mxdistance = event.getx() - mdownx;
     final float mydistance = event.gety() - mdowny;
     int l, r, t, b;
     //当水平或者垂直滑动距离大于10,才算是拖动事件
     if (math.abs(mxdistance) > 10 || math.abs(mydistance) > 10) {
      isdrag = true;
      l = (int) (getleft() + mxdistance);
      r = l + mwidth;
      t = (int) (gettop() + mydistance);
      b = t + mheight;
      //边界判断,不让布局滑出界面
      if (l < 0) {
       l = 0;
       r = l + mwidth;
      } else if (r > mscreenwidth) {
       r = mscreenwidth;
       l = r - mwidth;
      }
      if (t < 0) {
       t = 0;
       b = t + mheight;
      } else if (b > mscreenheight) {
       b = mscreenheight;
       t = b - mheight;
      }
      //回调移动后的坐标点
      if(mlocationlistener!=null){
       mlocationlistener.locationrect((l+r)/2,(t+b)/2);
      }
      this.layout(l, t, r, b);
     }
     break;
    case motionevent.action_up:
     setpressed(false);
     break;
    case motionevent.action_cancel:
     setpressed(false);
     break;
   }
   return true;
  }
  return false;
 }
 public void setlocationlistener(onlocationlistener locationlistener) {
  this.mlocationlistener = locationlistener;
 }
 public interface onlocationlistener {
  void locationrect(float locationx, float locationy);
 }
}

2.在代码中的运用

<com.xinrui.guestservice.view.draglinelayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="@dimen/dp_200"
 android:layout_height="@dimen/dp_110"
 android:orientation="vertical">
 <relativelayout
  android:layout_width="match_parent"
  android:layout_height="@dimen/dp_50">
 <edittext
  android:id="@+id/input_edt"
  android:layout_width="match_parent"
  android:layout_height="@dimen/dp_50"
  android:background="@drawable/edit_bg" />
 </relativelayout>
 <relativelayout
  android:layout_width="match_parent"
  android:layout_height="@dimen/dp_55"
  android:layout_margintop="@dimen/margin_5"
  android:background="@drawable/paint_bg">

  <textview
   android:id="@+id/paint_typeface"
   android:layout_width="@dimen/dp_50"
   android:layout_height="@dimen/dp_50"
   android:layout_alignparentleft="true"
   android:layout_alignparenttop="true"
   android:layout_margintop="@dimen/margin_5"
   android:background="@drawable/main_selector_write"
   android:clickable="true" />

  <textview
   android:id="@+id/paint_fontsize"
   android:layout_width="@dimen/dp_50"
   android:layout_height="@dimen/dp_50"
   android:layout_alignparenttop="true"
   android:layout_marginleft="@dimen/dp_10"
   android:layout_margintop="@dimen/margin_5"
   android:layout_torightof="@id/paint_typeface"
   android:background="@drawable/main_selector_write"
   android:clickable="true" />
 </relativelayout>
</com.xinrui.guestservice.view.draglinelayout>

3.这样就可以在activity 加载这个xml 来实现任意拖动功能

总结

到此这篇关于android 自定义linelayout实现满屏任意拖动功能的示例代码的文章就介绍到这了,更多相关android 自定义linelayout实现满屏任意拖动内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!