Android中如何实现自定义LineLayout实现满屏任意拖动功能

发布时间:2020-07-17 14:20:17 作者:小猪
来源:亿速云 阅读:288

小编这次要给大家分享的是Android中如何实现自定义LineLayout实现满屏任意拖动功能,文章内容丰富,感兴趣的小伙伴可以来了解一下,希望大家阅读完这篇文章之后能够有所收获。

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实现满屏任意拖动功能的文章,如果觉得文章内容写得不错的话,可以把它分享出去给更多人看到。

推荐阅读:
  1. js如何实现鼠标拖动功能
  2. 详解Android如何实现可任意拖动的悬浮窗功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android linelayout roi

上一篇:python自动处理excel的方法

下一篇:体验vSphere 6之4-为VMware ESXi分配iS

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》