自定义一个可以随意拖动的view

一个简单的自定义view, 可以随意拖动一个view

视频上传不了 。。。。。。 你就看动没动吧!

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

package com.example.missliproject.view;

/**
 * @author lwh
 * @description :
 * @date 2021/3/19.
 */

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.missliproject.R;


public class DragView extends FrameLayout {
    
    

    private float mStartX;
    private float mStartY;
    private int rawX;
    private int rawY;
    private int lastX;
    private int lastY;
    private int pHeight;
    private int pWidth;
    private long mLastTime;
    private ViewGroup parent;

    private ImageView  iv_close;
    private TextView iv_pic;
    private OnDragViewClickListener dragViewClickListener;
    private boolean flag = false;

    public void SetClickListener(OnDragViewClickListener dragViewClickListener) {
    
    
        this.dragViewClickListener = dragViewClickListener;
    }

    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);

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.layout_dragview, this);

        iv_close = view.findViewById(R.id.iv_close);
        iv_pic = view.findViewById(R.id.iv_pic);

    }

    //判断是否触摸view
    private boolean isTouchInView(ImageView view, float xAxis, float yAxis) {
    
    
        if (view == null) {
    
    
            return false;
        }
        int[] location = new int[2];
        view.getLocationOnScreen(location);//以屏幕为原点当前这个view的坐标
        int left = location[0];//获取当前Imagview的横坐标(在屏幕中的)
        int top = location[1];//获取当前Imagview的纵坐标(在屏幕中的)
        int right = left + view.getMeasuredWidth();//这是view右边的坐标
        int bottom = top + view.getMeasuredHeight();//这个view下边的坐标
        if (yAxis >= top && yAxis <= bottom && xAxis >= left
                && xAxis <= right) {
    
    
            return true;
        }
        return false;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
    
        //获取相对屏幕的坐标,即以屏幕左上角为原点
        rawX = (int) event.getRawX();
        rawY = (int) event.getRawY();
        switch (event.getAction()) {
    
    
            case MotionEvent.ACTION_DOWN:    //捕获手指触摸按下动作
                //获取相对View的坐标,即以此View左上角为原点
                mStartX = event.getRawX();
                mStartY = event.getRawY();
                if (isTouchInView(iv_close, rawX, rawY)) {
    
    
                    flag = true;
                }
                mLastTime = System.currentTimeMillis();
                lastX = rawX;//记录按下去x的坐标
                lastY = rawY;//记录按下去y的坐标
                if (getParent() != null) {
    
    
                    parent = (ViewGroup) getParent();
                    pHeight = parent.getHeight();
                    pWidth = parent.getWidth();
                }
                break;
            case MotionEvent.ACTION_MOVE:   //捕获手指触摸移动动作
                int dx = rawX - lastX;//横轴变化的距离
                int dy = rawY - lastY;//纵轴变化的距离
                float x = getX() + dx;//
                float y = getY() + dy;
                //判断是否到边界
                x = x < 0 ? 0 : x > pWidth - getWidth() ? pWidth - getWidth() : x;
                y = getY() < 0 ? 0 : getY() + getHeight() > pHeight ? pHeight - getHeight() : y;
                setX(x);
                setY(y);
                lastX = rawX;
                lastY = rawY;
                break;
            case MotionEvent.ACTION_UP:    //捕获手指触摸离开动作
                if (System.currentTimeMillis() - mLastTime < 800) {
    
    
                    if (Math.abs(mStartX - event.getRawX()) < 10.0 && Math.abs(mStartY - event.getRawY()) < 10.0) {
    
    
                        //处理点击的事件
                        if (flag) {
    
    
                            dragViewClickListener.onDragViewListener("delete", "");
                        } else
                            dragViewClickListener.onDragViewListener("pic", "");
                    }
                }
                flag = false;
                break;
        }
        return true;
    }
    public interface OnDragViewClickListener {
    
    
        void onDragViewListener(String name, String context);
    }
}

    <com.example.missliproject.view.DragView
        android:id="@+id/drag_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
        drag_view = findViewById(R.id.drag_view);
        drag_view.SetClickListener(new DragView.OnDragViewClickListener() {
    
    
            @Override
            public void onDragViewListener(String name, String context) {
    
    

                if(name.equals("delete")){
    
    
                    Toast.makeText(HomeActivity.this, "删除", Toast.LENGTH_SHORT).show();
                }
                if(name.equals("pic")){
    
    
                    Toast.makeText(HomeActivity.this, "图片", Toast.LENGTH_SHORT).show();
                }


            }
        });

就这。。。

猜你喜欢

转载自blog.csdn.net/weixin_44669831/article/details/115026814

相关文章