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);
}
}
③、初始化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);
}
ViewDragHelper的使用的更多相关文章
- Android -- ViewDragHelper
ViewDragHelper SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用,其实研究他们的源码你会发现这两个类都运用了ViewDragHelper来处理拖动. ...
- Viewdraghelper解析
2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用, 其实研究他们的源码你会发现这两个类都运用了ViewDr ...
- Android之ViewDragHelper
在自定义ViewGroup中,很多效果都包含用户手指去拖动其内部的某个View(eg:侧滑菜单等),针对具体的需要去写好onInterceptTouchEvent和onTouchEvent这两个方法是 ...
- Android 一步一步教你使用ViewDragHelper
在自定义viewgroup的时候 要重写onInterceptTouchEvent和onTouchEvent 这2个方法 是非常麻烦的事情,好在谷歌后来 推出了ViewDragHelper这个类.可以 ...
- Android ViewDragHelper源码解析
在自定义ViewGroup的过程中,如果涉及到View的拖动滑动,ViewDragHelper的使用应该是少不了的,它提供了一系列用于用户拖动子View的辅助方法和相关的状态记录,像Navigatio ...
- Android 中 View移动总结:ViewDragHelper学习及用法详解
如上图简单呈现出两个方块后,提出一个需求: 1.拖动方块时,方块(即子View)可以跟随手指移动. 2.一个方块移动时,另一个方块可以跟随移动. 3.将方块移动到左边区域(右边区域)后放开(即手指离开 ...
- ViewDragHelper详解
2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用,其实研究他们的源码你会发现这两个类都运用了ViewDra ...
- ViewDragHelper练习使用
转载博客地址:http://www.cnblogs.com/flyme2012/p/4076674.html 这个Demo是用来练习VIewDragHelper的,也是仿照网上一个大神的代码.我通过他 ...
- Android ViewDragHelper完全解析 自定义ViewGroup神器
Android ViewDragHelper完全解析 自定义ViewGroup神器 转载请标明出处: http://blog.csdn.net/lmj623565791/article/detai ...
随机推荐
- struct tm->time() localtime() gmtime()
struct tm->time() localtime() gmtime() struct tm { int tm_sec; /*代表目前秒数,正常范围为0-59,但允许至61秒 */ int ...
- matlab的绘图保存
matlab的绘图和可视化能力是不用多说的,可以说在业内是家喻户晓的.Matlab提供了丰富的绘图函数,比如ez**系类的简易绘图函数,surf.mesh系类的数值绘图函数等几十个.另外其他专业工 ...
- Ubuntu常用命令速查手册-珍藏版-完整版
sudo apt-get install 软件名 安装软件命令 sudo nautilus 打开文件(有root权限) su root 切换到“root” ls 列出当前目录文件(不包括隐含文件) l ...
- cf B. Maximum Absurdity
http://codeforces.com/contest/332/problem/B #include <cstdio> #include <cstring> #includ ...
- windows批处理命令教程
批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用Cmd.exe按照该文件中各个命令 ...
- qt实现头像上传功能(写了4个类,朝十晚八的博客,非常好)
想必大家都使用过qt的自定义头像功能吧,那么图1应该不会陌生,本片文章我就是要模拟一个这样的功能,虽然没有这么强大的效果,但是能够满足一定的需求. 图1 qq上传图片 首先在讲解功能之前,我先给出一片 ...
- back_inserter的用法
1,代码如下: #include<iostream> #include<list> #include<algorithm> #include<iterator ...
- hdu2460-Network:边的双连通分量
题目大意:给出一个无向图以及Q次询问,每次询问增加一条无向边,要求输出增加这条边后剩余的桥的数目. 算法:类似于求割点的方法,先做一次dfs求出所有的桥,并且维护这棵dfs树,当一次询问加入一条边(a ...
- Squid--hash代码分析
#ifndef SQUID_HASH_H #define SQUID_HASH_H //几个函数和变量的别名 typedef void HASHFREE(void *); typedef int HA ...
- javascript笔记6之函数
/* function box() { //函数的声明 alert('我只有被调用才可以执行!'); //函数本身没有运行功能 } //必须调用才可以执行 box(); //调用 function b ...