android viewdraghelper使用
2015-06-19 15:17
393 查看
viewdraghelper是v4包提供的一个为了简化手势操作的类,下面是一个slidermenu的例子来展示viewdraghelper的一些基本使用方法。
package com.example.test;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
/**
* User: cb Date: 2015-06-19 Time: 10:26
*/
@SuppressLint("NewApi")
public class SliderMenuLayout extends ViewGroup {
private ViewDragHelper viewDragHelper;
private View menuView;
private View contentView;
private int menuWidth = 500;
private DisplayMetrics dm;
private boolean isOpen;
public void setMenuWidth(int menuWidth) {
this.menuWidth = menuWidth;
}
public SliderMenuLayout(Context context) {
super(context);
setWillNotDraw(false);
}
public SliderMenuLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
}
public SliderMenuLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWillNotDraw(false);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
dm = getContext().getResources().getDisplayMetrics();
menuView = findViewById(R.id.menu);
LayoutParams params = menuView.getLayoutParams();
params.width = menuWidth;
params.height = dm.heightPixels;
contentView = findViewById(R.id.content);
LayoutParams params2 = contentView.getLayoutParams();
params2.width = dm.widthPixels;
params2.height = dm.heightPixels;
viewDragHelper = ViewDragHelper.create(this, 1.0f, new DragCallback());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChild(menuView, MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);
measureChild(contentView, MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);
}
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
contentView.layout(0, 0, contentView.getMeasuredWidth(), contentView.getMeasuredHeight());
menuView.layout(-menuWidth, 0, 0, menuView.getMeasuredHeight());
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return viewDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public void computeScroll() {
super.computeScroll();
if (viewDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
viewDragHelper.processTouchEvent(event);
return true;
}
// call back
public class DragCallback extends ViewDragHelper.Callback {
@Override
public boolean tryCaptureView(View view, int i) {
return true;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (child == contentView) {
if (left > menuWidth) {
return menuWidth;
} else if (left < 0) {
return 0;
}
} else if (child == menuView) {
if (left > 0) {
return 0;
} else if (left < -menuWidth) {
return -menuWidth;
}
}
return left;
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
if (changedView == contentView) {
menuView.layout(-menuWidth + left, 0, left, menuView.getMeasuredHeight());
} else if (changedView == menuView) {
contentView.setLeft(contentView.getLeft() + dx);
}
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
if (yvel > 0) {
isOpen = false;
viewDragHelper.settleCapturedViewAt(releasedChild == contentView ? 0 : -menuWidth, 0);
} else {
isOpen = true;
viewDragHelper.settleCapturedViewAt(releasedChild == contentView ? menuWidth : 0, 0);
}
invalidate();
}
}
public void toggle() {
viewDragHelper.smoothSlideViewTo(menuView, isOpen ? -menuWidth : 0, 0);
ViewCompat.postInvalidateOnAnimation(this);
isOpen = !isOpen;
}
}
-------------------华丽分割线--------------------
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/open"
android:layout_width="match_parent"
android:layout_height="30dip" />
<com.example.test.SliderMenuLayout
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#541231"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_weight="1"
android:text="sdsdsdsdsd" />
<TextView
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_weight="1"
android:text="只能爽肤水的沙发沙发沙发沙发上" />
</LinearLayout>
<LinearLayout
android:id="@+id/menu"
android:layout_width="200dip"
android:layout_height="match_parent"
android:background="#ffff12"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_weight="1"
android:text="sdsdsdsdsd" />
<TextView
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_weight="1"
android:text="只能爽肤水的沙发沙发沙发沙发上" />
</LinearLayout>
</com.example.test.SliderMenuLayout>
</LinearLayout>
package com.example.test;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
/**
* User: cb Date: 2015-06-19 Time: 10:26
*/
@SuppressLint("NewApi")
public class SliderMenuLayout extends ViewGroup {
private ViewDragHelper viewDragHelper;
private View menuView;
private View contentView;
private int menuWidth = 500;
private DisplayMetrics dm;
private boolean isOpen;
public void setMenuWidth(int menuWidth) {
this.menuWidth = menuWidth;
}
public SliderMenuLayout(Context context) {
super(context);
setWillNotDraw(false);
}
public SliderMenuLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setWillNotDraw(false);
}
public SliderMenuLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setWillNotDraw(false);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
dm = getContext().getResources().getDisplayMetrics();
menuView = findViewById(R.id.menu);
LayoutParams params = menuView.getLayoutParams();
params.width = menuWidth;
params.height = dm.heightPixels;
contentView = findViewById(R.id.content);
LayoutParams params2 = contentView.getLayoutParams();
params2.width = dm.widthPixels;
params2.height = dm.heightPixels;
viewDragHelper = ViewDragHelper.create(this, 1.0f, new DragCallback());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChild(menuView, MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);
measureChild(contentView, MeasureSpec.EXACTLY, MeasureSpec.EXACTLY);
}
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
contentView.layout(0, 0, contentView.getMeasuredWidth(), contentView.getMeasuredHeight());
menuView.layout(-menuWidth, 0, 0, menuView.getMeasuredHeight());
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return viewDragHelper.shouldInterceptTouchEvent(ev);
}
@Override
public void computeScroll() {
super.computeScroll();
if (viewDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
viewDragHelper.processTouchEvent(event);
return true;
}
// call back
public class DragCallback extends ViewDragHelper.Callback {
@Override
public boolean tryCaptureView(View view, int i) {
return true;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
if (child == contentView) {
if (left > menuWidth) {
return menuWidth;
} else if (left < 0) {
return 0;
}
} else if (child == menuView) {
if (left > 0) {
return 0;
} else if (left < -menuWidth) {
return -menuWidth;
}
}
return left;
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
if (changedView == contentView) {
menuView.layout(-menuWidth + left, 0, left, menuView.getMeasuredHeight());
} else if (changedView == menuView) {
contentView.setLeft(contentView.getLeft() + dx);
}
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
super.onViewReleased(releasedChild, xvel, yvel);
if (yvel > 0) {
isOpen = false;
viewDragHelper.settleCapturedViewAt(releasedChild == contentView ? 0 : -menuWidth, 0);
} else {
isOpen = true;
viewDragHelper.settleCapturedViewAt(releasedChild == contentView ? menuWidth : 0, 0);
}
invalidate();
}
}
public void toggle() {
viewDragHelper.smoothSlideViewTo(menuView, isOpen ? -menuWidth : 0, 0);
ViewCompat.postInvalidateOnAnimation(this);
isOpen = !isOpen;
}
}
-------------------华丽分割线--------------------
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/open"
android:layout_width="match_parent"
android:layout_height="30dip" />
<com.example.test.SliderMenuLayout
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#541231"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_weight="1"
android:text="sdsdsdsdsd" />
<TextView
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_weight="1"
android:text="只能爽肤水的沙发沙发沙发沙发上" />
</LinearLayout>
<LinearLayout
android:id="@+id/menu"
android:layout_width="200dip"
android:layout_height="match_parent"
android:background="#ffff12"
android:orientation="horizontal" >
<TextView
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_weight="1"
android:text="sdsdsdsdsd" />
<TextView
android:layout_width="0dip"
android:layout_height="30dip"
android:layout_weight="1"
android:text="只能爽肤水的沙发沙发沙发沙发上" />
</LinearLayout>
</com.example.test.SliderMenuLayout>
</LinearLayout>
相关文章推荐
- Android获得缩略图的代码注释
- android studio1.2加载so和jar文件
- Android TextView drawableLeft 在代码中实现
- android studio中安装git
- Google 将设立 Android 安全奖励项目 奖金达 38000 美元
- android学习笔记四——Service
- Android底层控制系统设置的命令集合
- Android 动画
- Android Studio 常用功能介绍
- Android 有道词典的简单实现方法介绍
- IIS 添加mime 支持 apk,exe,.woff,IIS MIME设置 ,Android apk下载的MIME 设置 苹果ISO .ipa下载mime 设置
- 【Android】四大组件+生命周期
- Android GPS学习笔记—JNI层实现
- android应用程序如何调用支付宝接口
- Android 百度地图2.4.2版本标注动画效果
- 一步步走进Android MaterialDesign
- android中ListView的Iterm无法点击
- Android Proguard returned with error code 1. See console 之注意事项
- 七海限时招募海盗,会写 Android 的海盗!
- 解决TextView强制换行