ViewDragHelper的使用

本文详细解析了ViewDragHelper的工作原理及其在Android应用中如何实现自由移动ViewGroup内部控件的功能,尤其针对仿QQ侧滑菜单的开发过程进行了深入探讨,包括自定义Layout、初始化ViewDragHelper及滑动事件的设置。

一、ViewDragHelper的原理

   是一个能够自用移动ViewGroup内部View的控件。

   通过获取ViewGroup的点击事件,之后通过Scroller滑动来进行对ViewGroup内部控件的移动。

二、ViewDragHelper的作用

   ①、自由移动ViewGroup的内部控件

   ②、仿QQ的侧滑栏

   ③、拼图游戏啊之类的。

核心就是能够让View自由移动

三、制作简单的仿QQ的侧滑菜单

①、自定义一个Layout,因为ViewDragHelper是对ViewGroup的处理。

public class SlideMenuFrameLayout extends FrameLayout {
public SlideMenuFrameLayout(Context context) {
        super(context);
    }

    public SlideMenuFrameLayout(Context context, AttributeSet attrs{
        super(context, attrs);
    }

    public SlideMenuFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
}
SlideMenuFrameLayout

创建好后,我们什么都不做。

之后在layout中创建SlideMenuFrameLayout的内部布局:

<com.cgx.sildingmenu.SlideMenuFrameLayout 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"
    tools:context="com.cgx.sildingmenu.MainActivity">

    <!--侧滑菜单-->
    <LinearLayout
        android:id="@+id/left_menu"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#000"
        android:layout_gravity="left"></LinearLayout>

    <!--主菜单-->
    <LinearLayout
        android:id="@+id/right_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#fff"
        android:layout_gravity="right">
    </LinearLayout>
</com.cgx.sildingmenu.SlideMenuFrameLayout>
activity_main

在SlideMenuFrameLayout中添加侧滑Layout,和主Layout

②、初始化ViewDragHelper

首先:创建一个ViewDragHelper

mViewDragHelper = ViewDragHelper.create(this,null);//null代表暂不设置

其次:将点击事件交给mViewDragHelper:

@Override
public boolean onTouchEvent(MotionEvent event) {
mViewDragHelper.processTouchEvent(event);
return true;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mViewDragHelper.shouldInterceptTouchEvent(ev);
}

最后:设置Scroller刷新:

@Overridepublic void computeScroll() {
super.computeScroll();
if (mViewDragHelper.continueSettling(true)){
ViewCompat.postInvalidateOnAnimation(this);
}
}
View Code

③、初始化ViewGroup的内部View也是是Menu和Conent

//当ViewGroup读取完layout中的xml的时候回调

@Override
protected void onFinishInflate() {
super.onFinishInflate();
mLinearMenu = (LinearLayout) getChildAt(0);
mLinearContent = (LinearLayout)getChildAt(1);
}

//当调用完onMeasure()方法的时候,调用该方法

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mMenuWidth = mLinearMenu.getMeasuredWidth();
}

④、设置滑动事件:

首先:创建 ViewDragHelper.Callback callback = new ViewDragHelper.Callback();用来接收事件的回调

①、设定允许哪个View移动

@Override
public boolean tryCaptureView(View child, int pointerId) {
//允许LinearContent滑动
return mLinearContent == child;
}

②、设定滑动的距离

//允许左右移动
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
return left;
}

③、设定当释放的时候menu是打开还是关闭

@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
if (mLinearContent.getLeft() < mMenuWidth/2){
mViewDragHelper.smoothSlideViewTo(mLinearContent,0,0);
}
else {
mViewDragHelper.smoothSlideViewTo(mLinearContent,mMenuWidth,0);
}
ViewCompat.postInvalidateOnAnimation(SlideMenuFrameLayout.this);
}

 

转载于:https://www.cnblogs.com/rookiechen/p/5745858.html

ViewDragHelper是一个帮助我们实现View拖拽和滑动效果的工具类,使用它可以简单地实现一些常见的交互效果,例如拖拽、滑动、边缘拖拽等。以下是使用ViewDragHelper的一般步骤: 1. 创建ViewDragHelper对象 ``` ViewDragHelper mDragHelper = ViewDragHelper.create(parentView, 1.0f, new DragHelperCallback()); ``` 2. 编写DragHelperCallback类 ``` private class DragHelperCallback extends ViewDragHelper.Callback { // 重写tryCaptureView方法,判断是否捕获当前View @Override public boolean tryCaptureView(View child, int pointerId) { return true; } // 重写clampViewPositionHorizontal和clampViewPositionVertical方法,返回拖拽后View的位置 @Override public int clampViewPositionHorizontal(View child, int left, int dx) { final int leftBound = getPaddingLeft(); final int rightBound = getWidth() - child.getWidth() - leftBound; final int newLeft = Math.min(Math.max(left, leftBound), rightBound); return newLeft; } @Override public int clampViewPositionVertical(View child, int top, int dy) { final int topBound = getPaddingTop(); final int bottomBound = getHeight() - child.getHeight() - topBound; final int newTop = Math.min(Math.max(top, topBound), bottomBound); return newTop; } } ``` 3. 在View的onTouchEvent中处理事件 ``` @Override public boolean onTouchEvent(MotionEvent event) { mDragHelper.processTouchEvent(event); return true; } ``` 4. 在View的onDraw方法中绘制View ``` @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制View } ``` 以上是使用ViewDragHelper的一般步骤,具体使用还需要根据实际需求进行调整。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值