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 ...
随机推荐
- Python 番外 消息队列设计精要
消息队列已经逐渐成为企业IT系统内部通信的核心手段.它具有低耦合.可靠投递.广播.流量控制.最终一致性等一系列功能,成为异步RPC的主要手段之一.当今市面上有很多主流的消息中间件,如老牌的Active ...
- C语言初学 使用while语句统计输入字符个数
#include<stdio.h> main() { int n=0; printf("输入任意个数的字符:\n"); while(getchar()!='\n')n+ ...
- Idea使用记录--添加Problems&&解决Autowired报错could not autowire
今天在使用Idea的时候,发现Idea在整个每次找到错误代码非常不方便(Idea如果类中有错误,没有打开过类并不会提示,比如构建工程后缺少jar包问题).我想快速看到工程哪里出问题类似于eclipse ...
- zlog小试(C语言日志工具)
test.c #include <stdio.h> #include "zlog.h" int main(int argc, char** argv) { int rc ...
- 怪胎:Android开发ImageView图片无法显示
今天碰到一个非常奇怪的问题: 在Android中ImageView无法显示加载的本地SDCard图片. 具体过程是:先调用本地照相机程序摄像,然后将拍摄的图片加载在ImageView中显示. publ ...
- PhoneGap and Titanium
http://mobile.51cto.com/web-338270.htm http://www.udpwork.com/item/6117.html http://blog.cnbang.net/ ...
- cf D. Sereja ans Anagrams
http://codeforces.com/contest/368/problem/D #include <cstdio> #include <cstring> #includ ...
- df 和du 命令统计磁盘空间不准确
Linux & Unix 中 df 和 du 命令统计磁盘空间数值不一致 经常会使用 df 和 du 分别查看磁盘空闲空间和占用空间,偶尔会发现 df(空闲空间) 会小于磁盘大小减去 du(占 ...
- PHP常用魔术方法(__toString魔术方法)
<?php //文件名:index.php define('a',__DIR__); include '/IMooc/Loader.php'; spl_autoload_register('\\ ...
- JMeter Building a Database Test Plan
Building a Database Test Plan In this section, you will learn how to create a basic Test Planto test ...