dragView 屏幕拖拽并且弹出菜单的控件

dragView

  • 因项目新需求需要添加一个屏幕拖拽按钮可以弹出菜单的控件,因为不是我做的闲来无事写一个demo吧 可能存在一些小bug(毕竟就写了几个小时)兄弟姐妹们理解思路就行 具体的可以自己调试一下 废话不多说先来一个gif走一走(调了帧数 可能看着比较快 不要介意)

  • GitHub 传送车https://github.com/guanhaoran/dragView
  • gif有点小 对付看吧 (太大了传github的时候 会出错 放图片还看不出来效果 不要介意)

Markdown

  • 废话不多话直接看代码

    1.写一个类继承 Button 重写构造方法&onTouchEvent(MotionEvent event)

      public DragView(Context context) {
            this(context, null);  }
         public DragView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
         }
       public DragView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            this.context = context;
             //获取屏幕宽高,用于控制控件在屏幕内移动
             DisplayMetrics dm = getResources().getDisplayMetrics();
             mScreenWidth = dm.widthPixels;
            mScreenHeight = dm.heightPixels - 100;//这里减去的100是下边的back键和menu键那一栏的高度,看情况而定
           showPopup = new ShowPopup(context);
          }

    2.onTouchEvent(MotionEvent event)

    大家可能看的不太懂 先大体看下结构 稍后我会在代码里加上注释 确保每条代码都有注释一些没有的 我就不会加注释 也浪费大家时间

      public boolean onTouchEvent(MotionEvent event) {
      super.onTouchEvent(event);
    
      int lastMoveX = 0;
      int lastMoveY = 0;
      switch (event.getAction()) {
    
          case MotionEvent.ACTION_DOWN:
              falg = true;
              startDownX = (int) event.getRawX();
              startDownY = (int) event.getRawY();
              break;
          case MotionEvent.ACTION_MOVE:
              if (showPopup.isShowing()) {
                  showPopup.dissPopup();
              }
              lastMoveX = (int) event.getRawX();
              lastMoveY = (int) event.getRawY();
    
              int dx = lastMoveX - startDownX;
              int dy = lastMoveY - startDownY;
              if (Math.abs(dx) > 2 || Math.abs(dy) > 2) {
                  falg = false;
              }
    
              left = getLeft() + dx;
              top = getTop() + dy;
              right = getRight() + dx;
              bottom = getBottom() + dy;
              if (left < 0) {
                  left = 0;
                  right = left + getWidth();
              }
              if (right > mScreenWidth) {
                  right = mScreenWidth;
                  left = right - getWidth();
              }
              if (top < 0) {
                  top = 0;
                  bottom = top + getHeight();
              }
              if (bottom > mScreenHeight) {
                  bottom = mScreenHeight;
                  top = bottom - getHeight();
              }
              layout(left, top, right, bottom);
              Log.i("____________________", left + "___" + top + "___" + right + "___" + bottom);
              invalidate();
              startDownX = (int) event.getRawX();
              startDownY = (int) event.getRawY();
              break;
          case MotionEvent.ACTION_UP:
              int lastMoveDx = Math.abs((int) event.getRawX() - startDownX);
              int lastMoveDy = Math.abs((int) event.getRawY() - startDownY);
              if (falg) {
                  ToastUtils.showToast(context, "我点击了");
                  showContent(360, true);
              }
              falg = false;
              break;
      }
      return true;

    3.点击按钮出现的一个小圆点 这个使用popupWindow做的包括背景颜色变暗也是在popupWindow中实现的 这个稍后也会有注释 我只是写完了 不喜欢写注释
    (这个毛病很坏 - -)

      private void showContent(int angles, boolean isShow) {
      if (showPopup.isShowing()) {
          showPopup.dissPopup();
      } else {
          int excursionX = -(getWidth() / 2) - 5;
          int excursionY = 0;
          if (left == 0 && right == 0 && top == 0 && bottom == 0) {
              excursionX = -(getWidth() / 2)-5;
              excursionY = 0;
          } else if (left < 50 && top < 50) {
              excursionX = getWidth();
              excursionY = 0;
          } else if (mScreenWidth - right < 50 && top < 50) {
              excursionX = -getWidth() * 2 - 20;
              excursionY = 0;
          } else if (mScreenWidth - right < 50 && mScreenHeight - bottom < getHeight()*2) {
              excursionX = -getWidth() * 2 - 20;
              excursionY = -getHeight() * 3 - 20;
          } else if (mScreenHeight - bottom < getHeight()*2 && left < 50) {
              excursionX = getWidth();
              excursionY = -getHeight() * 3 - 20;
          } else if (left < 50) {
              excursionX = getWidth();
              excursionY = -getHeight() - getHeight() / 2;
          } else if (top < 50) {
              excursionX = -(getWidth() / 2)-10;
              excursionY = -10;
          } else if (mScreenWidth - right < 50) {
              excursionX = -getWidth() * 2 - 10;
              excursionY = -getHeight() - getHeight() / 2 - 10;
          } else if (mScreenHeight - bottom < getHeight() * 2.5) {
              excursionX = -getWidth() / 2 -10;
              excursionY = -getHeight() * 3 - 10;
          } else {
          }
          showPopup.showPopup(this, excursionX, excursionY);
      }
      }

    4.还有一个popupWindow 我就不贴在这里了 大家可以看代码就好了,可能代码包结构很乱大家不要介意,我也是为了效果别的我也没注意

  • 最后我在上传GitHub的时候 哇!!!很痛苦 全是英文(奈何我的英文还不好)传了好久才传上去 下一篇文章我会把GitHub的使用给兄弟们详详细细的说一遍

  • GitHub 传送车https://github.com/guanhaoran/dragView

QQ:765307272

转载于:https://www.cnblogs.com/guanhaoran/p/7428217.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
DragLinearLayout是一个LinearLayout, 他可是其子View在其范围内可拖动、可交换位置。默认情况下,子View是不可拖动的,你需要调用DragLinearLayout.setViewDraggable(child, child)方法让其可拖动。项目地址:https://github.com/justasm/DragLinearLayout 效果图:如何使用和使用LinearLayout一样:                        2. 让子View可拖动默认情况下子View是不可拖动的,你需要调用dragLinearLayout.setViewDraggable()让子View变为可拖动的。DragLinearLayout dragLinearLayout = (DragLinearLayout) findViewById(R.id.container); //让子view可拖动,默认是不能拖动的         for(int i = 1; i < dragLinearLayout.getChildCount(); i ){             View child = dragLinearLayout.getChildAt(i);             dragLinearLayout.setViewDraggable(child, child);          }可以动态添加可拖动子viewfinal View view = View.inflate(context, R.layout.view_layout, null); dragLinearLayout.addDragView(viewview.findViewById(R.id.view_drag_handle));   // ..   dragLinearLayout.removeDragView(view);使用OnViewSwapListener检测子view之间的排序变化事件:dragLinearLayout.setOnViewSwapListener(new DragLinearLayout.OnViewSwapListener() {     @Override     public void onSwap(View firstView, int firstPosition,             View secondView, int secondPosition) {         // update data, etc..     } });当在ScrollView中使用DragLinearLayout的时候,如果你想在拖拽的时候ScrollView也能滚动,需要调用setContainerScrollView(Scroll
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值