长按之后出发拖动事件

 adapterDefault.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {            @Override            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {                mScrollView.scrollTo(0, 0);                //震动                setVibrator();                view.setDrawingCacheEnabled(true);                dragOperate(view.getDrawingCache(), defaultEffectList.get(position), view);                return true;            }        });
 int itemViewWidth = 243, itemViewHeight = 174;//悬浮按钮的宽高    int windowWidth = 0;

 

 /**     * 特效拖拽     *     * @param drawingCache     */    public void dragOperate(Bitmap drawingCache, SoundEffectBean bean, View itemView) {        try {            windowWidth = UIHelper.getScreenPixWidth(this);                      ivEffectDrag.setTag(bean);            ivEffectDrag.setText(bean.getSoundName());            itemViewWidth = itemView.getWidth();            itemViewHeight = itemView.getHeight();            LogUtil.e("dispatchTouchEvent itemViewWidth " + itemViewWidth + " " + itemViewWidth);            LogUtil.e("dispatchTouchEvent itemViewWidth2 " + UIHelper.px2dip(ReplaceEffectActivity.this, itemViewWidth));            ivEffectDrag.setVisibility(View.VISIBLE);        } catch (Exception e) {            e.printStackTrace();        }    }    @Override    public boolean dispatchTouchEvent(MotionEvent event) {        final int x = (int) event.getRawX();        final int y = (int) event.getRawY();        switch (event.getAction() & MotionEvent.ACTION_MASK) {            case MotionEvent.ACTION_DOWN:            case MotionEvent.ACTION_MOVE:                LogUtil.e("dispatchTouchEvent " + x + " " + y);                FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) ivEffectDrag                        .getLayoutParams();                // 还有初始itemViewWidth,itemViewHeight为0,因为先走ontouch方法                int left = x - itemViewWidth / 2;                int top = y - topBarHeight - itemViewHeight;                //边界处理                if (left < 0) {                    left = 0;                }                if (left > ((ViewGroup) ivEffectDrag.getParent()).getWidth() - itemViewWidth) {                    left = ((ViewGroup) ivEffectDrag.getParent()).getWidth() - itemViewWidth;                }                if (top < 0) {                    top = 0;                }                if (top > ((ViewGroup) ivEffectDrag.getParent()).getHeight() - itemViewHeight) {                    top = ((ViewGroup) ivEffectDrag.getParent()).getHeight() - itemViewHeight;                }                layoutParams.leftMargin = left;                layoutParams.topMargin = top;                ivEffectDrag.setLayoutParams(layoutParams);                break;            case MotionEvent.ACTION_CANCEL:            case MotionEvent.ACTION_UP:                if (ivEffectDrag.getVisibility() == View.VISIBLE) {                    ivEffectDrag.setVisibility(View.GONE); //这里因为设置gone了会重绘,到时候get宽高为0                    float rX = UIHelper.px2dip(ReplaceEffectActivity.this, x);                    float rY = UIHelper.px2dip(ReplaceEffectActivity.this, y);                    float w = UIHelper.px2dip(ReplaceEffectActivity.this, windowWidth);                    int position = 0;                    boolean inRange = false;                    if (rY > 75 && rY < 130) {//第一行                        if (rX < w / 4) {                            position = 0;                        } else if (rX >= w / 4 && rX < w * 2 / 4) {                            position = 1;                        } else if (rX >= w * 2 / 4 && rX < w * 3 / 4) {                            position = 2;                        } else if (rX >= w * 2 / 4) {                            position = 3;                        }                        inRange = true;                    } else if (rY >= 130 && rY < 185) {//第二行                        if (rX < w / 4) {                            position = 4;                        } else if (rX >= w / 4 && rX < w * 2 / 4) {                            position = 5;                        } else if (rX >= w * 2 / 4 && rX < w * 3 / 4) {                            position = 6;                        } else if (rX >= w * 2 / 4) {                            position = 7;                        }                        inRange = true;                    }                    if (inRange) {                        LogUtil.e("position=" + position);                        SoundEffectBean soundEffectBean = (SoundEffectBean) ivEffectDrag.getTag();                        showReplaceTip(soundEffectBean, position);                    /*    addSpecialsEvent.setPosition(position);                        EventBus.getDefault().post(addSpecialsEvent);*/                    }//                    }                }                break;        }        ((ViewGroup) ivEffectDrag.getParent()).invalidate();        if (ivEffectDrag.getVisibility() == View.VISIBLE) {            return false;        }        return super.dispatchTouchEvent(event);    }

 

更多相关文章

  1. Android 图片的浏览、缩放、拖动和自动居中
  2. Android控件拖动
  3. android mtk6592 禁止拖动主界面图标
  4. ListView 滑动边界的模糊效果
  5. Android 常用的ui(单选框 多选框 下拉列表框 拖动条)
  6. android 拖拽图片&拖动浮动按钮到处跑
  7. android拖动条和评分组件的使用

随机推荐

  1. android sdk setup时呈现:Failed to fetc
  2. Android:实现类似RadioButton自动换行及
  3. 整理android环境离线安装搭建方法——支
  4. android学习笔记20--------------Relativ
  5. ProgressBar进度条
  6. Android编译报错
  7. Android(安卓)泛型使用
  8. Android之Input子系统事件分发流程
  9. Android(安卓)弹出菜单
  10. Android(安卓)Timer 更好方法