android中实现view可以滑动的六种方法续篇(二)
承接上一篇,上一篇中讲解了实现滑动的第五种方法,如果你还没读过,可点击下面链接:
http://www.cnblogs.com/fuly550871915/p/4985482.html
这篇文章现在来讲解最后一种方法,也可以说是实现滑动的终极大招!不过相比之前的方法,实现的过程要复杂一些,但是实现的效果当然也会徇丽多彩!
六、使用ViewDragHelper实现
(1)基础知识
使用ViewDragHelper可以实现多种多样的滑动效果,只要细心处理,足够满足你的各种拖动的需求了。下面来讲解一下使用ViewDragHelper的步骤。
步骤一、实例化
mViewDragHelper = ViewDragHelper.create(this, callback);
需要注意的是,ViewDragHelper通常定义在一个ViewGroup中。callback是个回调类,下面自会讲到。
步骤二、拦截事件
必须要将触摸事件传递给ViewDragHelper处理。按照下面的模板书写即可。如下:
public boolean onInterceptTouchEvent(android.view.MotionEvent ev)
{
//注意一定要将触摸事件拦截下来
return mViewDragHelper.shouldInterceptTouchEvent(ev);
} public boolean onTouchEvent(MotionEvent event)
{ //必须将触摸事件传递给mDragHleper
mViewDragHelper.processTouchEvent(event); return true;
}
步骤三、重写computeScroll
其实ViewDragHelper内部也是通过Scroller类实现平滑滑动的,因此它也要重写这个方法。按照下面的模板书写即可:
public void computeScroll() {
if(mViewDragHelper.continueSettling(true))
{
ViewCompat.postInvalidateOnAnimation(this);
}
}
步骤四、处理回调的callback
在回调类ViewDragHelper.Callback中,系统定义了大量的监听方法来帮助我们处理各种事件。
首先创建这个callback,模板代码如下:
private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() {
//什么时候开始检测触摸事件
public boolean tryCaptureView(View child, int pointerId) {
return false;
}
}
下面就开始介绍几个常常被重写的方法
方法 tryCaptureView
写法如下:
//什么时候开始检测触摸事件
public boolean tryCaptureView(View child, int pointerId) { return mSecondView == child;
}
该方法是告诉回调的callback什么时候开始检测触摸事件。因为ViewDragHelper定义在一个
ViewGroup中,比如你需要当触摸到第二个子view时,才开始检测触摸事件,就可以如上书写。
方法 clampViewPositionHorizontal
写法如下:
/**
* 水平方向上的移动,默认的返回值为0,此时将它修改为检测到的left
*/
public int clampViewPositionHorizontal(View child, int left, int dx) { return left;
}
该方法控制水平方向的移动,默认返回为0,即不移动.如果你想让水平方向上发生移动,可以像上面那样进行书写。
方法 clampViewPositionVertical
写法如下:
/**
* 垂直方向上的移动,默认值也是0,在这里干脆直接让它返回0
*/
public int clampViewPositionVertical(View child, int top, int dy) {
return 0;
}
该方法的含义与clampViewPositionHorizontal一样
方法onViewReleased
写法如下:
/**
* 当手指抬起时或者说是拖动结束时会回调这个方法
*/
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel); }
注意:该方法在手指抬起或者说是拖动结束时调用。可以在此方法中写一些你需要处理的逻辑。
另外还有一些其他方法,介绍如下:
onViewCaptured() 这个事件在用户触摸到view后回调
onViewDragStateChanged() 这个事件在拖拽状态改变时回调,比如idle,dragging等状态
onViewPositionChanged() 这个事件在位置改变时回调,常用于滑动时更改scale进行缩放等效果。
至此,基本上使用ViewDragHelper进行滑动的方法就讲解完了。但是还要记得在ViewGroup中,要在onFinishInflate方法中将子view获取到。
另外,ViewDragHelper使用smoothSlideViewTo进行滑动,使用ViewCompat.postInvalidateOnAnmation方法进行刷新。这两个方法一定要同时使用!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(2)代码实战
上面说了那么多,还没实践一下呢!现在就来写一个实际例子,这个例子基本上可以当做模板了,以后想要实现更复杂的功能,可以在此基础上修改。因此,要好好研究这个例子。
在写代码前先上一张最终的效果图,如下:

好了,效果很丑陋,但是足够说明问题了。开始看看代码是怎么写的吧。
新建一个项目。然后自定义一个DragViewGroup继承自ViewGroup,在它的里面使用ViewDragHelper愉快的定制吧。如下:
package com.example.testdragview; import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout; public class DragViewGroup extends FrameLayout{ private ViewDragHelper mViewDragHelper; private View mFirstView;
private View mSecondView; private int mFirstViewWidth; public DragViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); mViewDragHelper = ViewDragHelper.create(this, callback);
} public DragViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
mViewDragHelper = ViewDragHelper.create(this, callback);
} public DragViewGroup(Context context) {
super(context);
mViewDragHelper = ViewDragHelper.create(this, callback);
} /**
* 关于mDragHelper的拖动等操作都在这个类里面操作
*/
private ViewDragHelper.Callback callback = new ViewDragHelper.Callback() { //什么时候开始检测触摸事件
public boolean tryCaptureView(View child, int pointerId) { return mSecondView == child;
} /**
* 水平方向上的移动,默认的返回值为0,此时将它修改为检测到的left
*/
public int clampViewPositionHorizontal(View child, int left, int dx) { return left;
} /**
* 垂直方向上的移动,默认值也是0,在这里干脆直接让它返回0
*/
public int clampViewPositionVertical(View child, int top, int dy) {
return 0;
} /**
* 当手指抬起时或者说是拖动结束时会回调这个方法
*/
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel); if(mSecondView.getLeft() <100)
{//如果第二个view的left小于500像素,就不显示第一个view。下面的代码就相当于此 mViewDragHelper.smoothSlideViewTo(mSecondView, 0, 0);
ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);
}else
{//显示第一个view mViewDragHelper.smoothSlideViewTo(mSecondView, 300, 0);
ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);
} } }; /**
* 布局加载完成后调用
* 在这个方法里获取子view
*/
protected void onFinishInflate() {
super.onFinishInflate(); mFirstView = getChildAt(0);
mSecondView = getChildAt(1);
} /**
* 子view的大小改变后回调该方法
* 在这个方法里获取到mFirstView的宽度
*/
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh); mFirstViewWidth = mFirstView.getMeasuredWidth();
} public boolean onInterceptTouchEvent(android.view.MotionEvent ev)
{
//注意一定要将触摸事件拦截下来
return mViewDragHelper.shouldInterceptTouchEvent(ev);
} public boolean onTouchEvent(MotionEvent event)
{ //必须将触摸事件传递给mDragHleper
mViewDragHelper.processTouchEvent(event); return true;
} public void computeScroll() { if(mViewDragHelper.continueSettling(true))
{
ViewCompat.postInvalidateOnAnimation(this);
}
} }
结合前面的基础知识和代码中的注释,应该很好理解了。就是在DragViewGroup里面使用ViewDragHelper定义了一个拖动的效果而已!值得注意的是在onReleased方法中,我们做了这样子的一个设定,即当mSecondView的左边距小于100像素时,就将mSecondView移动到初始位置(造成的效果仍然是遮挡出mFirstView,即关闭mFirstView);当其左边与大于100像素,就将其移动的距离左边300像素的位置(当然是相对于它的ViewGroup的左边距了)。
下面我们建立建立两个用来填充到DragViewGroup中的布局,分别命名为layout1.xml和layout2.xml。如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#ff0000"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你好啊,我是第一个view"
android:textSize="30sp"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#FFFF80"> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拖动我,赶紧拖动我"
android:textSize="30sp"/> </LinearLayout>
下面就修改activity_main.xml中的代码,将这两个子布局放进DragViewGroup中吧,如下:
<LinearLayout 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"
> <com.example.testdragview.DragViewGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
> <include layout = "@layout/layout1"/>
<include layout = "@layout/layout2"/> </com.example.testdragview.DragViewGroup> </LinearLayout>
就这样子,所有的工作都完成了,运行程序吧。效果和上面贴出来的图一样。好了,要想掌握好ViewDragHelper,还需要多加练习。
至此,android中实现view滑动的六种方法都讲完了!
android中实现view可以滑动的六种方法续篇(二)的更多相关文章
- android中实现view可以滑动的六种方法续篇(一)
承接上一篇,如果你没有读过前四章方法,可以点击下面的链接: http://www.cnblogs.com/fuly550871915/p/4985053.html 下面开始讲第五中方法. 五.利用Sc ...
- android中实现view可以滑动的六种方法
在android开发中,经常会遇到一个view需要它能够支持滑动的需求.今天就来总结实现其滑动的六种方法.其实每一种方法的 思路都是一样的,即:监听手势触摸的坐标来实现view坐标的变化,从而实现vi ...
- Android View体系(二)实现View滑动的六种方法
1.View的滑动简介 View的滑动是Android实现自定义控件的基础,同时在开发中我们也难免会遇到View的滑动的处理.其实不管是那种滑动的方式基本思想都是类似的:当触摸事件传到View时,系统 ...
- 详解实现Android中实现View滑动的几种方式
注: 本文提到的所有三种滑动方式的完整demo:ScrollDemo 1. 关于View我们需要知道的 (1)什么是View? Android中的View类是所有UI控件的基类(Base class) ...
- Android 中的View与ViewGroup
Android重点知识--View和ViewGroup与自定义控件 作者:丁明祥 邮箱:2780087178@qq.com 一.基础 ViewGroup 参考资料: Android 手把手教您自定义V ...
- android中的坐标系以及获取坐标的方法
android中有两种坐标系,分别称之为Android坐标系和视图坐标系.而对应的也有一些相关的方法可以获取坐标系中的 坐标值.只有搞清楚这些区别,才能在实现的时候不至于出错或者得不到你想要的效果. ...
- MVC中视图View向控制器传值的方法
MVC中视图View向控制器传值的方法步骤如下: 1.index页面: 页面中只需要一个触发事件的按钮
- android中Bitmap的放大和缩小的方法
android中Bitmap的放大和缩小的方法 时间 2013-06-20 19:02:34 CSDN博客原文 http://blog.csdn.net/ada168855/article/det ...
- Android 自己定义View须要重写ondraw()等方法
Android 自己定义View须要重写ondraw()等方法.这篇博客给大家说说自己定义View的写法,须要我们继承View,然后重写一些 方法,方法多多,看你须要什么方法 首先写一个自己定义的V ...
随机推荐
- 求1+2+3+...+n
求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 卧槽,剑指Offer竟然有这样的题... public ...
- 解决安装office2013时提示已安装相同版本的office
例如出现如上所示的图: ------------------------------------------------------------------ 在尝试使用官方的卸载清理工具无果后, 终极 ...
- (8)分布式下的爬虫Scrapy应该如何做-图片下载(源码放送)
转载主注明出处:http://www.cnblogs.com/codefish/p/4968260.html 在爬虫中,我们遇到比较多需求就是文件下载以及图片下载,在其它的语言或者框架中,我们可能 ...
- sprint3(第八天)
昨天忘了发博客,最近在整合前台和后台的内容,在sprint结束前应该能整合好,然后实现前后台的联系,实现点餐功能. 最近要准备大作业也要复习四六级考试,所以花在项目的时间比较少了,请老师谅解. 燃尽图
- SCRUM:第一天任务实现情况
我认领了我们团队“广商百货”应用平台的密码提示功能,任务暂时还在进行中.虽然建立了数据库,但是数据库里要存放什么数据,我们的小组还在讨论中.因为android的知识还在自学过程中,所以做起来比较慢,也 ...
- Matlab绘图(一二三维)
Matlab绘图 强大的绘图功能是Matlab的特点之一,Matlab提供了一系列的绘图函数,用户不需要过多的考虑绘图的细节,只需要给出一些基本参数就能得到所需图形,这类函数称为高层绘图函数.此外,M ...
- 在uwp仿制WPF的Window
移植WPF软件到uwp时碰到用作对话框的Window有多种处理选择.我个人认为最省事的是用ContentDialog模拟Window. 比如你想把上面这个WPF窗体弄到uwp里面去 1.修改Conte ...
- 不可或缺 Windows Native (6) - C 语言: 函数
[源码下载] 不可或缺 Windows Native (6) - C 语言: 函数 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 函数 示例cFunction.h # ...
- MyBatis中的特殊符号[20160713]
今天中午回到工位已经是12:20多了,没有时间睡觉了,本想着还能提前开始,结果看了点新闻之后,又是12:40了,所以新闻坚决不能看,执行力. 今天主要记录一下MyBatis中的特殊符号的问题,这个问题 ...
- DDD开发框架ABP之导航菜单
每一个网站都会有导航菜单(通常不止一个),ASP.NET Boilerplate(后文简称ABP)提供了一种创建和使用菜单的通用架构,利用架构我们可以方便的创建菜单并显示给用户.本文主要说明菜单的创建 ...