自定义View随手指滑动

  本文为博主原创--欢迎转载--但是请注明出处(珍惜他人劳动成果)!

本文实现的效果为图片(或者别的控件)跟随手指的滑动,不会超过屏幕边界,下面直接上代码,为什么代码报红我也不清楚哈,第一次写博客.

布局--

<?xml version="1.0" encoding="utf-8"?>
<com.dingying.servicetest.MyView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"/>
</com.dingying.servicetest.MyView>

1,自定义View继承ViewGroup,构造方法就选两个参数(XML使用)的就可以了

public class MyView extends ViewGroup {

    private TextView mTv;
    private int mChildWidthSize;
    private int mChildHeightSize;
    private int mLeft;
    private int mTop;
    private int mWidthSize;
    private int mHeightSize;

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
2,重写onLayout方法,因为继承的是ViewGroup

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
   
}
3,重写onMeasure方法,测量出子控件和屏幕的大小

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // 获取子控件
    mTv = (TextView) getChildAt(0);
    
    // 测量子控件的声明
    measureChildren(widthMeasureSpec,heightMeasureSpec);
    // 获取子控件的大小
    mChildWidthSize = mTv.getMeasuredWidth();
    mChildHeightSize = mTv.getMeasuredHeight();

    // 获取屏幕的大小(本身)
    mWidthSize = MeasureSpec.getSize(widthMeasureSpec);
    mHeightSize = MeasureSpec.getSize(heightMeasureSpec);
    
    // 设置控件大小
    setMeasuredDimension(mWidthSize, mHeightSize);
}
4,重写onTouchEvent方法

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // 获取点击的坐标,当点击坐标不在控件上面时,不触发后面的事件
            int startX = (int) event.getX();
            int startY = (int) event.getY();
            Rect rect = new Rect(mLeft,mTop,mLeft+mChildWidthSize,mTop+mChildHeightSize);
            if(!(rect.contains(startX,startY))) {
              return false;
            }

            break;
        case MotionEvent.ACTION_MOVE:
            // 获取移动的坐标
            float endX = event.getX();
            float endY = event.getY();
            // 移动后左边和顶边的坐标
            mLeft = (int) (endX - mChildWidthSize/2);
            mTop = (int) (endY - mChildHeightSize /2);
            break;
        case MotionEvent.ACTION_UP:

            break;
    }
    // 刷新布局,也就是重新执行onLayout方法
    requestLayout();
    // 必须返回true,只有返回true的时候moveup事件才能执行
    return true;
}
5,在onlayout方法中判断是否超出屏幕和子控件的布局

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    if(mLeft < 0) {
        mLeft = 0;
    }
    if(mTop < 0) {
        mTop = 0;
    }
    if(mLeft+mChildWidthSize > mWidthSize) {
        mLeft = mWidthSize - mChildWidthSize;
    }
    if(mTop+mChildHeightSize > mHeightSize) {
        mTop = mHeightSize- mChildHeightSize;
    }
    mTv.layout(mLeft,mTop,mLeft+mChildWidthSize,mTop+mChildHeightSize);
}
这样就大功告成了!---如果对你有帮忙,请记得点赞哦--哈哈!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Kotlin自定义View中,可以通过重写`onInterceptTouchEvent`方法来限制子滑动控件的滑动。在这个方法中,你可以判断是否要拦截事件,并返回`true`或`false`来决定是否拦截事件。如果返回`true`,则表示拦截事件,子滑动控件将无法滑动;如果返回`false`,则表示不拦截事件,子滑动控件可以正常滑动。 下面是一个示例,展示如何在自定义View中限制子滑动控件的滑动。这个示例中创建了一个`CustomView`类,它包含一个`RecyclerView`作为子视图。我们想要在用户水平滑动`CustomView`时,防止`RecyclerView`的水平滑动,只允许垂直滑动: ``` class CustomView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { private var initialX = 0f private var initialY = 0f private val recyclerView: RecyclerView init { LayoutInflater.from(context).inflate(R.layout.custom_view, this, true) recyclerView = findViewById(R.id.recyclerView) } override fun onInterceptTouchEvent(event: MotionEvent): Boolean { when (event.action) { MotionEvent.ACTION_DOWN -> { initialX = event.x initialY = event.y return false } MotionEvent.ACTION_MOVE -> { val dx = abs(event.x - initialX) val dy = abs(event.y - initialY) return dy > dx } else -> return super.onInterceptTouchEvent(event) } } } ``` 在`onInterceptTouchEvent`方法中,我们首先记录了用户按下手指时的坐标。然后,在用户移动手指时,我们计算水平和垂直方向上的滑动距离,并比较它们。如果垂直方向上的滑动距离大于水平方向上的滑动距离,则返回`true`,表示拦截事件,防止`RecyclerView`的滑动。否则,返回`false`,表示不拦截事件,`RecyclerView`可以正常滑动。 需要注意的是,在这个示例中,我们使用了`LayoutInflater`来从XML布局文件中获取`RecyclerView`视图。如果你使用了不同的方式来创建子视图,请相应地修改初始化代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

plx_csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值