案例演示:
SlideActivity.gif- 1.步骤分析:先上个最原始的写法,后续使用VeiwDragHelper来编写更简单!
- 1.继承ViewGroup
- 2.onMeasure()测量子View的长宽,onLayout()摆放子View
/**
*
* @param widthMeasureSpec:当前容器宽度测量规则
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
leftMenu=getChildAt(0);
main=getChildAt(1);
//左面板:
leftMenu.measure(leftMenu.getLayoutParams().width,heightMeasureSpec);
//主面板:
main.measure(widthMeasureSpec,heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
leftMenu=getChildAt(0);
main=getChildAt(1);
//摆放左面板:
leftMenu.layout(-leftMenu.getMeasuredWidth(),0,0,b);
//摆放主面板:
main.layout(l,t,r,b);
}
- 3.界面滑动:重写onTouchEvent()方法,slideTo(int x,int y):让View滑动到指定的坐标位置,slideBy(int x,int y):根据View的基础位置进行变化滑动
- 4.细节:限制左边界和右边界防止滑动超出:通过int 预测距离=getScrollX()(获取当前View的X坐标)+滑动偏移量来进行边界判断;如果 预测距离>0则处于左界面关闭模式,如果预测距离小于则处于左界面打开模式;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downX=event.getX();
break;
case MotionEvent.ACTION_MOVE:
moveX=event.getX();
//计算滑动偏移量
int offset= (int) (downX-moveX);
//当前滑动位置+滑动偏移量 的大小来判断是否滑动超出
int newPosition=getScrollX()+offset;
//限定右边界
if (newPosition>0){
//让界面处于左菜单关闭模式:
close();
}else if (newPosition<-leftMenu.getMeasuredWidth()){
//让界面处于左菜单打开模式:
open();
}else{
//滑动:有原来的基础位置就起点进行滑动
scrollBy(offset,0);
}
//更新downX,避免滑动递增:
downX=moveX;
break;
case MotionEvent.ACTION_UP:
//根据滚动的当前位置和左面板宽度的一半进行比较,如果大于左面板的一半,则处于打开模式,否则为关闭模式
//当前位置:
int endPosition=getScrollX();
int centerX= (int) (-leftMenu.getMeasuredWidth()/2.0f);
if (endPosition<centerX){
//打开:
open();
}else{
//关闭:
close();
}
break;
}
return true;
}
- 5.滑动速度设置,模拟器数据;
public void switchToggle(){
int startX=getScrollX();
int dx;
if (isOpen){
//关闭:
dx=0-startX;
}else {
//打开:
dx=-leftMenu.getMeasuredWidth()-startX;
}
int duration=(Math.abs(dx)*2);
mScroller.startScroll(startX,0,dx,0,duration);
invalidate();//重绘界面->drawChild()->computeScroll()
}
/**
* 模拟数据
*/
@Override
public void computeScroll() {赔率高?:》#
} 3
if (mScroller.computeScrollOffset()){
//true表示动画没结束
int currentX=mScroller.getCurrX();
scrollTo(currentX,0);
invalidate();//重绘界面
}
super.computeScroll();
}
- 6.事件分发机制:
image
事件分发机制详解
- 2.完整代码:
- 1.SlideView:
public class SlideView extends ViewGroup {
private float downX,moveX,startX,startY,endX,endY;
private View leftMenu,main;
private boolean isOpen=false;
private Scroller mScroller;//滑动模拟器
public SlideView(Context context) {
super(context);
init();
}
public SlideView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SlideView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mScroller=new Scroller(getContext());
}
/**
*
* @param widthMeasureSpec:当前容器宽度测量规则
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
leftMenu=getChildAt(0);
main=getChildAt(1);
//左面板:
leftMenu.measure(leftMenu.getLayoutParams().width,heightMeasureSpec);
//主面板:
main.measure(widthMeasureSpec,heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
leftMenu=getChildAt(0);
main=getChildAt(1);
//摆放左面板:
leftMenu.layout(-leftMenu.getMeasuredWidth(),0,0,b);
//摆放主面板:
main.layout(l,t,r,b);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
downX=event.getX();
break;
case MotionEvent.ACTION_MOVE:
moveX=event.getX();
//计算滑动偏移量
int offset= (int) (downX-moveX);
//当前滑动位置+滑动偏移量 的大小来判断是否滑动超出
int newPosition=getScrollX()+offset;
//限定右边界
if (newPosition>0){
//让界面处于左菜单关闭模式:
close();
}else if (newPosition<-leftMenu.getMeasuredWidth()){
//让界面处于左菜单打开模式:
open();
}else{
//滑动:有原来的基础位置就起点进行滑动
scrollBy(offset,0);
}
//更新downX,避免滑动递增:
downX=moveX;
break;
case MotionEvent.ACTION_UP:
//根据滚动的当前位置和左面板宽度的一半进行比较,如果大于左面板的一半,则处于打开模式,否则为关闭模式
//当前位置:
int endPosition=getScrollX();
int centerX= (int) (-leftMenu.getMeasuredWidth()/2.0f);
if (endPosition<centerX){
//打开:
open();
}else{
//关闭:
close();
}
break;
}
return true;
}
/**
* 打开左界面模式
*/
private void close() {
//直接跳到指定位置
scrollTo(0,0);
isOpen=false;
}
/**
* 关闭左界面模式
*/
private void open() {
scrollTo(-leftMenu.getMeasuredWidth(),0);
isOpen=true;
}
public void switchToggle(){
int startX=getScrollX();
int dx;
if (isOpen){
//关闭:
dx=0-startX;
}else {
//打开:
dx=-leftMenu.getMeasuredWidth()-startX;
}
int duration=(Math.abs(dx)*2);
mScroller.startScroll(startX,0,dx,0,duration);
invalidate();//重绘界面->drawChild()->computeScroll()
}
/**
* 模拟数据
*/
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()){
//true表示动画没结束
int currentX=mScroller.getCurrX();
scrollTo(currentX,0);
invalidate();//重绘界面
}
super.computeScroll();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
startX=ev.getX();
startY=ev.getY();
break;
case MotionEvent.ACTION_MOVE:
endX=ev.getX();
endY=ev.getY();
//计算出滑动偏移量
int offsetX= (int) (endX-startX);
int offsetY= (int) (endY-startY);
//判断是否x的偏移量小于Y的偏移量,如果小于则为横向滑动;
if (offsetX<offsetY && offsetX<2){
//拦截,交给自己的onTouchEvent进行处理;
return true;
}
break;
case MotionEvent.ACTION_UP:
break;
}
return super.onInterceptTouchEvent(ev);
}
}
- 2.activity_slide:
<?xml version="1.0" encoding="utf-8"?>
<com.bunny.widgetdemo.SlideDemo.SlideView
android:id="@+id/slide_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<include layout="@layout/view_menu"/>
<include layout="@layout/view_main"/>
</com.bunny.widgetdemo.SlideDemo.SlideView>
网友评论