• Android控件拖拽后页面刷新回来原始位置问题



    前言

    在工作中遇到了这个问题,拖动一个图片按钮,切换页面后发现按钮又回到了原始的位置.


    一、解决思路

    百度了下大致方法多为一种,即通过重写onTouchEvent()记录前后移动的相对坐标,然后根据相对坐标设置控件位置

    二、使用步骤

    1.拖动示例

    代码如下(示例):

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                //得到事件的坐标
                int eventX = (int) event.getRawX();
                int eventY = (int) event.getRawY();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //得到父视图的right/bottom
                        if (maxRight == 0) {//保证只赋一次值
                            maxRight = parentView.getRight();
                            maxBottom = parentView.getBottom();
                        }
                        //第一次记录lastX/lastY
                        lastX = eventX;
                        lastY = eventY;
                        isMove = false;
                        downTime = System.currentTimeMillis();
                        if (haveDelay()) {
                            canDrag = false;
                        } else {
                            canDrag = true;
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (haveDelay() && !canDrag) {
                            long currMillis = System.currentTimeMillis();
                            if (currMillis - downTime >= delay) {
                                canDrag = true;
                            }
                        }
                        if (!canDrag) {
                            break;
                        }
                        //计算事件的偏移
                        int dx = eventX - lastX;
                        int dy = eventY - lastY;
                        if (dx != 0 && dy != 0) {
                            //根据事件的偏移来移动imageView
                            int left = view.getLeft() + dx;
                            int top = view.getTop() + dy;
                            int right = view.getRight() + dx;
                            int bottom = view.getBottom() + dy;
                            //限制left  >=0
                            if (left < 0) {
                                right += -left;
                                left = 0;
                            }
                            //限制top
                            if (top < 0) {
                                bottom += -top;
                                top = 0;
                            }
                            //限制right <=maxRight
                            if (right > maxRight) {
                                left -= right - maxRight;
                                right = maxRight;
                            }
                            //限制bottom <=maxBottom
                            if (bottom > maxBottom) {
                                top -= bottom - maxBottom;
                                bottom = maxBottom;
                            }
                            //不要这么设置View.layout(int left,int top,int right,int bottom)方法
    //                        view.setLeft(left);
    //                        view.setTop(top);
    //                        view.setRight(right);
    //                        view.setBottom(bottom);
                             //要用setLayoutParams这么设置
                            setViewLocation(view, left, top, right, bottom);
                            //再次记录lastX/lastY
                            lastX = eventX;
                            lastY = eventY;
                            isMove = true;
                        }
                        break;
                    default:
                        break;
                }
                return isMove;
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    2.方法示例

    代码如下(示例):

        private static void setViewLocation(View view, int left, int top, int right, int bottom) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(right - left, bottom - top);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
            ViewParent parent = view.getParent();
            View p = (View) parent;
            int marginRight = p.getWidth() - right;
            int marginBottom = p.getHeight() - bottom;
            params.setMargins(left, top, marginRight, marginBottom);
            view.setLayoutParams(params);
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    总结

    以上就是今天要讲的内容,本文主要实现ImageView拖拽时限制区域及解决切换页面时ImageView位置重置问题。

  • 相关阅读:
    设计模式之【观察者模式】
    【案例】3D地球(vue+three.js)
    什么才是好代码?
    基于SSM的客户管理系统设计与实现
    诙谐有趣的《UVM实战》笔记——第二章 一个简单的UVM验证平台
    three.js学习笔记(二十一)——页面加载进度条
    【Linux】基础:Linux环境基础开发工具——yum
    Docker 中 jdk8容器里无法使用 JDK 的 jmap 等命令的问题
    cookie和session区别
    在Flink中集成和使用Hudi
  • 原文地址:https://blog.csdn.net/weixin_37587365/article/details/126887603