Android 自定义随意拖拽布局

效果图如下:

1. 自定义view如下:
package com.example.myapplication

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.RelativeLayout
import kotlin.math.abs

class CustomDragView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr) {

    private var mWidth = 0
    private var mHeight = 0
    private var screenWidth = 0
    private var screenHeight = 0

    private var downX = 0f
    private var downY = 0f

    //是否拖动
    var isDrag = false

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = measuredWidth
        mHeight = measuredHeight
        screenWidth = ScreenUtil.getScreenWidth(context)
        screenHeight = ScreenUtil.getScreenHeight(context)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (this.isEnabled) {
            when (event.action) {
                //DOWN时,即刚开始的触摸点相对view的坐标。
                MotionEvent.ACTION_DOWN -> {
                    isDrag = false
                    downX = event.x
                    downY = event.y
                }
                MotionEvent.ACTION_MOVE -> {
                    //滑动的距离 = 触摸点滑动到的坐标 - 开始触摸的坐标 (都是相对于view本身)
                    val xDistance = event.x - downX
                    val yDistance = event.y - downY
                    var l: Int
                    var r: Int
                    var t: Int
                    var b: Int
                    //当水平或者垂直滑动距离大于10,才算拖动事件
                    if (abs(xDistance) > 10 || abs(yDistance) > 10) {
                        isDrag = true
                        l = (left + xDistance).toInt()
                        r = l + mWidth
                        t = (top + yDistance).toInt()
                        b = t + mHeight
                        //不划出边界判断,此处应按照项目实际情况,因为本项目需求移动的位置是手机全屏,
                        // 所以才能这么写,如果是固定区域,要得到父控件的宽高位置后再做处理
                        if (l < 0) {
                            l = 0
                            r = l + mWidth
                        } else if (r > screenWidth) {
                            r = screenWidth
                            l = r - mWidth
                        }
                        if (t < 0) {
                            t = 0
                            b = t + mHeight
                        } else if (b > screenHeight) {
                            b = screenHeight - mHeight
                            t = b - mHeight
                        }
                        layout(l, t, r, b)
                    }

                }

                MotionEvent.ACTION_UP -> {
                    // 这里宽度必须确定宽高
                    val params = LayoutParams(mWidth, mHeight)
                    params.setMargins(left, top, 0, 0)
                    layoutParams = params
                    isPressed = false
                }
                MotionEvent.ACTION_CANCEL -> isPressed = false

            }
            return true
        }
        return false
    }

}
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
81
82
83
84
85
86
87
88
89
90
91
92
使用到的工具类:

package com.example.myapplication;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class ScreenUtil {

    private static int width = 0;
    private static int height = 0;
    private static int showHeight = 0;
    private static int statusHeight = 0;
    private static float density = 0;

    public static int getScreenWidth(Context context) {
        if (width == 0) {
            WindowManager manager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            Display display = manager.getDefaultDisplay();
            width = display.getWidth();
        }
        return width;
    }

    public static int getScreenHeight(Context context) {
        if (height == 0) {
            WindowManager manager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            Display display = manager.getDefaultDisplay();
            height = display.getHeight();
        }
        return height;
    }

    public static int getScreenShowHeight(Context context) {
        if (showHeight == 0) {
            showHeight = getScreenHeight(context) - getStatusBarHeight(context);
        }
        return showHeight;
    }

    public static int getStatusBarHeight(Context context) {
        if (statusHeight > 0) {
            return statusHeight;
        }
        Class<?> c = null;
        Object obj = null;
        java.lang.reflect.Field field = null;
        int x = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusHeight = context.getResources().getDimensionPixelSize(x);
            return statusHeight;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return statusHeight;
    }

    public static float getScreenDensity(Context context) {
        if (density == 0) {
            try {
                DisplayMetrics dm = new DisplayMetrics();
                WindowManager manager = (WindowManager) context
                        .getSystemService(Context.WINDOW_SERVICE);
                manager.getDefaultDisplay().getMetrics(dm);
                density = dm.density;
            } catch (Exception ex) {
                ex.printStackTrace();
                density = 1.0f;
            }
        }
        return density;
    }

    public static float getScreentMinLength(Context context) {
        return Math.min(getScreenHeight(context), getScreenWidth(context));
    }

    /**
     * 根据指定k的系数获取屏幕在max范围内的最大长宽,默认宽比较小
     *
     * @param context
     * @param k
     * @return
     */
    public static DrawWrap getCutWrap(Context context, float k, float max) {
        float tWidth = getScreenWidth(context);
        float tHeight = getScreenHeight(context);

        if (tWidth * max * k > tHeight) {
            return new DrawWrap(tHeight * max / k, tHeight * max);
        } else {
            return new DrawWrap(tWidth * max, tWidth * max * k);
        }
    }

    public static class DrawWrap {
        public float width;
        public float height;

        public DrawWrap(float width, float height) {
            this.width = width;
            this.height = height;
        }
    }

    public static int dip2px(Context context, float dipValue) {
        return (int) (dipValue * getScreenDensity(context) + 0.5f);
    }

    /**
     * 将sp值转换为px值,保证文字大小不变
     *
     * @param context
     * @param spValue (DisplayMetrics类中属性scaledDensity)
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 获取屏幕中控件顶部位置的高度--即控件顶部的Y点
     *
     * @return
     */
    public static int getScreenViewTopHeight(View view) {
        return view.getTop();
    }

    /**
     * 获取屏幕中控件底部位置的高度--即控件底部的Y点
     *
     * @return
     */
    public static int getScreenViewBottomHeight(View view) {
        return view.getBottom();
    }

    /**
     * 获取屏幕中控件左侧的位置--即控件左侧的X点
     *
     * @return
     */
    public static int getScreenViewLeftHeight(View view) {
        return view.getLeft();
    }

    /**
     * 获取屏幕中控件右侧的位置--即控件右侧的X点
     *
     * @return
     */
    public static int getScreenViewRightHeight(View view) {
        return view.getRight();
    }

    /*
     * 获取控件宽
     */
    public static int getWidth(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return (view.getMeasuredWidth());
    }

    /*
     * 获取控件高
     */
    public static int getHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return (view.getMeasuredHeight());
    }

}

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
2.xml中使用:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity4">

    <com.example.myapplication.CustomDragView
        android:id="@+id/customView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/purple_700" />

</RelativeLayout>
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值