在Android的源代码中,屏幕之间的跳转是如何实现的呢?在workspace.java中开始。在这个类中,为实现屏幕切换主要重写了以下几个方法:onMeasure()、onLayout()、onInterceptTouchEvent()、onTouchEvent()方法,另外还是用了CustomScroller mScroller来平滑过渡各个页面之间的切换。
首先,我们看一下onMeasure()方法:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); final int width = MeasureSpec.getSize(widthMeasureSpec); final int widthMode = MeasureSpec.getMode(widthMeasureSpec); if (widthMode != MeasureSpec.EXACTLY) { throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); } final int heightMode = MeasureSpec.getMode(heightMeasureSpec); if (heightMode != MeasureSpec.EXACTLY) { throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); } // The children are given the same width and height as the workspace final int count = getChildCount(); for (int i = 0; i < count; i++) { getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); } //ADW: measure wallpaper when using old rendering if(!lwpSupport){ if (mWallpaperLoaded) { mWallpaperLoaded = false; mWallpaperWidth = mWallpaperDrawable.getIntrinsicWidth(); mWallpaperHeight = mWallpaperDrawable.getIntrinsicHeight(); } final int wallpaperWidth = mWallpaperWidth; mWallpaperOffset = wallpaperWidth > width ? (count * width - wallpaperWidth) / ((count - 1) * (float) width) : 1.0f; } if (mFirstLayout) { scrollTo(mCurrentScreen * width, 0); mScroller.startScroll(0, 0, mCurrentScreen * width, 0, 0); if(lwpSupport)updateWallpaperOffset(width * (getChildCount() - 1)); mFirstLayout = false; } /*int max = 3; int aW = getMeasuredWidth(); float w = aW / max; maxPreviewWidth=(int) w; maxPreviewHeight=(int) (getMeasuredHeight()*(w/getMeasuredWidth()));*/ }
在这里,得到屏幕的宽高,然后再枚举其中所有的子view,设置它们的布局(使他们的高和父控件一样),这样每一个子view就是充满屏幕可以滑动显示的其中一页。下面��onLayout()方法: