类似launcher桌面滑动效果

来源:互联网 发布:金牛考勤网络连接 编辑:程序博客网 时间:2024/05/01 06:00
这个例子中涉及到了以下几个知识点:
1) attrs.xml文件的使用
2) GestureDetector.OnGestureListener监听手势
3) onLayout()、onMeasure()、onTouchEvent()的使用
接下来说一下我实现的思路:
1) 每个桌面就是一个大组件,水平的排列在线性布局文件中,每个桌面适合屏幕一样大小,所以要拓展LinearLayout,重写其中的onMeasure()、onLayout()方法
2) 由于要实现随手势滑动,所以只要实现GestureDetector.OnGestureListener接口中的onDown()、onScroll()方法就可以
3) 由于要接收触屏事件,所以要实现onTouchEvent()
接下来我们来看一下代码吧:
  1. public class ScrollLayout extends LinearLayout implements GestureDetector.OnGestureListener{
  2.     private int offset;  //相对距离
  3.     private GestureDetector gestureDetector;  //手势事件
  4.     private int childWidth; //子View的宽度
  5.     private int childCount; //子视图的数量
  6.     private int defaultWindow; //默认窗口

  7.     private boolean setShareWindowFlag=false;    // 保证默认窗口的设置只执行一次

  8.     public ScrollLayout(Context context) {
  9.         super(context);
  10.         init();
  11.     }

  12.     public ScrollLayout(Context context, AttributeSet attrs) {
  13.         super(context, attrs);
  14.         init();
  15.         //获取定义的defaultWindow的值
  16.         TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyScrollWindow);
  17.         defaultWindow = typedArray.getInteger(R.styleable.MyScrollWindow_defaultWindow,0);
  18.     }

  19.     private void init(){
  20.         gestureDetector = new GestureDetector(this.getContext(),this);
  21.     }

  22.     //返回值为true 才能触发 手势事件
  23.     public boolean onDown(MotionEvent e) {
  24.         return true;
  25.     }

  26.     public void onShowPress(MotionEvent e) { }

  27.     public boolean onSingleTapUp(MotionEvent e) {
  28.         return false;  
  29.     }
  30.     //顺手势滑动
  31.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  32.         //获取滑动的距离
  33.         offset = (int) (offset - distanceX);
  34.         //防止滑出边界
  35.         if(offset>0){
  36.             offset=0;
  37.         }else if(offset < -1*childWidth*(childCount-1)){
  38.             offset= -1*childWidth*(childCount-1);
  39.         }
  40.         //重绘布局
  41.         requestLayout();
  42.         return true;
  43.     }

  44.     public void onLongPress(MotionEvent e) {
  45.         
  46.     }

  47.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  48.         return false;  
  49.     }

  50.     //设置布局文件的宽高和每个桌面的宽高
  51.     @Override
  52.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  53.         super.onMeasure(widthMeasureSpec,heightMeasureSpec);

  54.         //给每个桌面设置和屏幕相同的宽度和高度
  55.         int childCount = getChildCount();
  56.         for(int i=0;i<childCount;i++){
  57.             getChildAt(i).measure(widthMeasureSpec,heightMeasureSpec);
  58.         }

  59.     }

  60.     //设置布局
  61.     @Override
  62.     protected void onLayout(boolean changed, int l, int t, int r, int b) {

  63.         childCount = getChildCount();   //获取子视图数
  64.         childWidth = childCount > 0? getChildAt(0).getMeasuredWidth():0; //获取字节点的宽度
  65.         if(!setShareWindowFlag&&defaultWindow>=0&&defaultWindow<=childCount-1){
  66.             //设置默认窗口的左端距离
  67.             offset = -1*defaultWindow*childWidth;
  68.             setShareWindowFlag=true;
  69.         }
  70.         //设置距离(0,0)点X轴方向的初始距离
  71.         int left = 0+offset;
  72.         for (int i = 0; i < childCount ;  i ++){
  73.             //设置每个子视图在布局中的位置
  74.             View child = getChildAt(i);
  75.             if(child.getVisibility()!=View.GONE){
  76.                 child.layout(left,0,childWidth+left,child.getMeasuredHeight());
  77.                 left = left + childWidth;
  78.             }
  79.         }
  80.     }

  81.     //触屏事件
  82.     @Override
  83.     public boolean onTouchEvent(MotionEvent event) {
  84.        boolean result = gestureDetector.onTouchEvent(event);
  85.        if(event.getAction()==MotionEvent.ACTION_UP){
  86.            //当手指抬起来时 判断滑动距离显示整个子视图
  87.            showOneDesktop();
  88.        }
  89.        return result;
  90.     }

  91.     //判断当手指抬起时显示那个桌面
  92.     private void showOneDesktop(){
  93.        int index = Math.abs(offset)/childWidth;
  94.        if(Math.abs(offset)-index*childWidth>childWidth/2){
  95.            index++;
  96.        }
  97.        offset = offset > 0 ? index*childWidth : -1*index*childWidth;
  98.        requestLayout();
  99.     }
  100. }
复制代码
在这段代码中使用到的attrs.xml文件:
  1. <resources>
  2.         <declare-styleable name="MyScrollWindow">
  3.             <attr name="defaultWindow" format="integer"/>
  4.         </declare-styleable>
  5. </resources>
复制代码
可以和系统自带的布局文件一样使用
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <com.wxg.scroll_window.view.ScrollLayout
  3.         xmlns:android="http://schemas.android.com/apk/res/android"
  4.         xmlns:demo="http://schemas.android.com/apk/res/com.wxg.scroll_window"
  5.         android:id="@+id/testLayout"
  6.         android:layout_width="fill_parent"
  7.         android:layout_height="fill_parent"
  8.         demo:defaultWindow="1"
  9.         >
  10.         <FrameLayout
  11.             android:layout_height="fill_parent"
  12.             android:layout_width="fill_parent"
  13.             android:background="#cccccc">

  14.             <Button
  15.                     android:layout_height="wrap_content"
  16.                     android:layout_width="wrap_content"
  17.                     android:text="1"/>
  18.             </FrameLayout>
  19.         <FrameLayout
  20.             android:layout_height="fill_parent"
  21.             android:layout_width="fill_parent"
  22.             android:background="#ffffff">

  23.             <Button
  24.                     android:layout_height="wrap_content"
  25.                     android:layout_width="wrap_content"
  26.                     android:text="2"/>
  27.             </FrameLayout>
  28.         <FrameLayout
  29.             android:layout_height="fill_parent"
  30.             android:layout_width="fill_parent"
  31.             android:background="#bcbcbc">

  32.             <Button
  33.                     android:layout_height="wrap_content"
  34.                     android:layout_width="wrap_content"
  35.                     android:text="3"/>
  36.             </FrameLayout>


  37.         </com.wxg.scroll_window.view.ScrollLayout>

原创粉丝点击