android 中的视图切换(launcher中的WorkSpace...

来源:互联网 发布:手机淘宝没有我要代理 编辑:程序博客网 时间:2024/05/19 13:55
  由于中间这张图是刚截的,昨天刚刷了机...所以app的ico有点不一样
其实就是是精简了lanucher中WorkSpace的代码。。。有源码的同学可以自己看看源码....
主要原理:
 覆盖onMeasure函数用于计算子view的高和宽
 @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    final int count = getChildCount();    //设置每个子视图的大小    for (int i = 0; i < count; i++) {      getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);    }  }
覆盖onLayout函数 用于设置子view的位置
其中childLeft += childWidth;使得每个子view从左到右依次排列

  @Override  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {    int childLeft = 0;    final int count = getChildCount();    //设置每个视图的布局位置    for (int i = 0; i < count; i++) {      final View child = getChildAt(i);      if (child.getVisibility() != View.GONE) {        final int childWidth = child.getMeasuredWidth();        child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());        childLeft += childWidth;      }    }  }
覆盖 onInterceptTouchEvent函数 关于这个函数的作用这篇文章解释的很清楚http://blog.csdn.net/ddna/article/details/5473293
简单点说此函数就是会终止TouchEvent函数的传递;
我们想要的效果是如果
MotionEvent.ACTION_MOVE的方向是延x轴的时候要特殊处理
 /**   * This method JUST determines whether we want to intercept the motion. If we return true, onTouchEvent will be called   * and we do the actual scrolling there.   */  //it handle when down      //return true intercept,   //return false not intercept  @Override  public boolean onInterceptTouchEvent(MotionEvent ev) {    /*     * Shortcut the most recurring case: the user is in the dragging state and he is moving his finger. We want to     * intercept this motion.     */    final int action = ev.getAction();    if ((action == MotionEvent.ACTION_MOVE) && (mTouchState == TOUCH_INTERCEPT)) {      return true;    }    final float x = ev.getX();    final float y = ev.getY();    switch (action) {    case MotionEvent.ACTION_DOWN:        // Remember location of down touch        mLastMotionX = x;        mLastMotionY = y;        /*         * If being flinged and user touches the screen, initiate drag; otherwise don't. mScroller.isFinished should be         * false when being flinged.         */        mTouchState = mScroller.isFinished() ? !TOUCH_INTERCEPT : TOUCH_INTERCEPT;        break;    case MotionEvent.ACTION_MOVE:        /*         * Locally do absolute value. mLastMotionX is set to the y value of the down event.         */        final int xDiff = (int) Math.abs(x - mLastMotionX);        final int yDiff = (int) Math.abs(y - mLastMotionY);        boolean xMoved = xDiff > mTouchSlop;        boolean yMoved = yDiff > mTouchSlop;        if (xMoved || yMoved) {          if (xMoved && !yMoved) {            // Scroll if the user moved far enough along the X axis            mTouchState = TOUCH_INTERCEPT;          }else{        mTouchState = !TOUCH_INTERCEPT;          }        }        break;      case MotionEvent.ACTION_CANCEL:      case MotionEvent.ACTION_UP:        mTouchState = !TOUCH_INTERCEPT;        break;    }
覆盖onTouchEvent函数达到移动x轴的时候 滚动视图代码有点长我就不贴了看demo把!
主要就是精简了下workSpace里的代码 ... demo 地址http://d.download.csdn.net/down/3484431/li6a209