标签:
现在有好多好多应用都用到了抽屉面板
就是滑动的时候
从左侧或者右侧拉出来
然后主界面会变小一点
ViewDragHelper,看了一下,父类直接是Object
源码1500行
/**
* ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number
* of useful operations and state tracking for allowing a user to drag and reposition
* views within their parent ViewGroup.
*/
ViewDragHelper 是一个写自定义ViewGroup的通用类.
提供了好多对用户拖拽的实用操作和状态追踪
在父类ViewGroup中复位view
为什么继承FrameLayout,因为FrameLayout简单简洁效率高
public class DragLayout extends FrameLayout{
}
public DragLayout(Context context) {
super(context);
}
public DragLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
<com.lich.customcomponent.widget.DragLayout
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"
android:background="@drawable/bg"
tools:context=".activity.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:background="#ff0000"
android:orientation="vertical"
android:id="@+id/ll_left"
android:layout_height="match_parent"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:background="#00ff00"
android:orientation="vertical"
android:id="@+id/ll_right"
android:layout_height="match_parent"></LinearLayout>
</com.lich.customcomponent.widget.DragLayout>
ViewDragHelper的构造函数是私有的
所以我们不能new出来
我们要调用create方法
我们先看看create方法
public static ViewDragHelper create(ViewGroup forParent, float sensitivity, Callback cb)
3个参数
1.ViewGroup forParent 父控件
2.float sensitivity 敏感度,默认1.0,敏感度越大越敏感
3.Callback cb 回调
这里的sensitivity敏感度和源码中的一个值mTouchSlop嘻嘻相关
mTouchSlop就是最小敏感范围,敏感度越小,那么范围越大,
敏感度越大,那么范围越小
public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
ViewDragHelper viewDragHelper = ViewDragHelper.create(this, 1.0f, callback);
}
ViewDragHelper.Callback callback=new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return false;
}
};
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mViewDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
try {
mViewDragHelper.processTouchEvent(event);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
ViewDragHelper.Callback callback=new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return true;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
return left;
}
};
标签:
原文地址:http://blog.csdn.net/qq_33781658/article/details/51598656