Android: ViewDragHelper tutorial
2015-03-31 16:31
232 查看
转自:http://blog.denevell.org/android-viewdraghelper-example-tutorial.html
If you want to drag things around your screen, you’ll want to use the compatability library’s ViewDragHelper class.You’ll first need to make your own ViewGroup extending custom view, since we’ll be intercepting MotionEvents.
Creating an instance
Once you’ve made your custom view extending ViewGroup, you need to create a ViewDragHelper instance. We’ll put it in onAttachedToWindow().@Override protected void onAttachedToWindow() { super.onAttachedToWindow(); mDragHelper = ViewDragHelper.create(this, 1.0f, new OurViewDragHelperCallbacks()); ... }
We’re using the create() factory method, passing in this ViewGroup, the sensitivity for a drag start (1.0f is normally according to the docs), and some callbacks.
Motion events
Before we look at the callbacks, let’s look at how they are activiated, and for that we look at onInterceptTouchEvent() and onTouchEvent().@Override public boolean onInterceptTouchEvent(MotionEvent ev) { boolean shouldInterceptTouchEvent = mDragHelper.shouldInterceptTouchEvent(ev); return shouldInterceptTouchEvent; }
This method uses our drag helper to decide if our class should intercept the touch events or not. We use this so if our child view is a button, for example, we can both press and slide it.
Next we use the onTouchEvent() method to make the view drag helper process this motion event, which will be called according to the interaction of the view and the method above.
@Override public boolean onTouchEvent(MotionEvent event) { mDragHelper.processTouchEvent(event); return true; }
ViewDragHelper callbacks
Now we have the MotionEvents being passed correctly to our ViewDragHelper instance, we can look at the callbacks.new ViewDragHelper.Callback() { @Override public boolean tryCaptureView(View arg0, int pointerId) { return true; }
The above will be passed the views which are dragged, and allow you to say if they should be captured or not. In this case we’re saying deal with any of the ViewGroup’s children.
@Override public int clampViewPositionVertical(View child, int top, int dy) { return top; }
The above method allows us to drag on the vertical axis. If we’re happy with the new position of the drag, we just return the ‘top’ value. You can define clamp the dragging in certain regions using this method.
This has a sister, clampViewPositionHorizonal, that allows you to drag or constrain on the horizonal axis.
@Override public int getViewVerticalDragRange(View child) { return parent.getMeasuredHeight()-child.getMeasuredHeight(); }
The above method is used to calculate the velocity internally, by knowing your view’s dragging space. Again, it has a sister method, replacing Vertical for Horizontal.
@Override public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); if(yvel>0) { mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), parent.getMeasuredHeight()-releasedChild.getMeasuredHeight()); } else { mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), 0); } invalidate(); } });
The above method, and final we’ll look at, is called when the dragging of an element stops. We’re using it to see if the velocity of the drag in the Y axis is positive or negative.
If it’s positive, i.e. we’re dragging down, we take the released view and tell it slide down down to the bottom of the parent. If it’s negative, i.e. we’re dragging up, we slide up to the top.
We finally call invalidate so the animation can begin. There are other methods we can call on the drag helper, like smoothSlideViewTo, but settleCatpuredViewAt takes into account the current velocity.
Ensuring the animation continues
After we call the settleCapturedViewAt or similar methods above, we need to ensure the animatin continues. In the computeScroll() method on our ViewGroup or similar we have the following:@Override public void computeScroll() { super.computeScroll(); if(mDragHelper.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); } }
We call the continueSettling method on the ViewDragHelper instance so our animation continues, and if it’s not yet settled, we then go and call the postInvalidateOnAnimation() method to ensure we keep animating.
There are plenty of other methods to play with here https://developer.android.com/reference/android/support/v4/widget/ViewDragHelper.html
转者注:在自定义容器里面获取里面的子View方法:
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mDragView1=getChildAt(0);
mDragView2=getChildAt(1);
Log.v("TAG","====mDragView 1 is "+mDragView1+"---22 IS "+mDragView2);
}
在构造方法里面获取为空。
相关文章推荐
- Android 一步一步教你使用ViewDragHelper
- Android开发:使用ViewDragHelper实现抽屉拉伸效果
- Android ViewDragHelper使用介绍
- Android下拉刷新库,利用viewdraghelper实现,集成了下拉刷新,底部加载更多,数据初始加载显示loading等功能
- Android 模拟消息悬浮通知(只适用于应用内--ViewDragHelper,不需要权限的悬浮Notification)
- Android--ViewDragHelper完全解析 自定义ViewGroup神器
- Android_ViewDragHelper
- Android ViewDragHelper
- android ViewDragHelper 实现抽屉滑动效果
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- Android自定义ViewGroup神器-ViewDragHelper
- Android使用ViewDragHelper实现侧滑菜单(一)
- Android Scroll(四)----ViewDragHelper
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- Android 使用ViewDragHelper实现向slidingMenu侧滑菜单的效果
- Android -- ViewDragHelper
- Android开发,自定义ViewGroup的神器,ViewDragHelper
- Android -- ViewDragHelper