---
title: "Android launcher workspace分析"
author: "venus_chen"
date: "2011-08-16T10:07:07Z"
tags: []
categories: ["移动开发"]
slug: "android-launcher-workspace"
draft: false
lastmod: "2011-08-16T10:07:07Z"
id: 1150157
url: "https://venus-chen.iteye.com/blog/1150157"
---

在Workspace.Java类实现，相关方法有：

　　1) computeScroll()：重写了父类的computeScroll();主要功能是计算拖动的位移量、更新背景、设置要显示的屏幕(setCurrentScreen(mCurrentScreen);)。

　　2) dispatchDraw()：重写了父类的dispatchDraw();主要功能是判断抽屉是否打开、绘制指定的屏幕，可以绘制当前一屏，也可以绘制当前屏幕和下一屏幕，也可以绘制所有的屏幕，这儿的绘制指显示屏幕上的child(例如：app、folder、Wiget)。和 computeScroll()中的setCurrentScreen(mCurrentScreen);方法配合使用可以实现屏幕的拖动多少显示多少的功能。

　　3) onMeasure()：重写了父类的onMeasure();主要功能是设置屏幕的显示大小。由每个child的measure()方法设置。

　　4) onLayout()：重写了父类的onLayout();主要功能是设置屏幕的显示位置。由child的layout()方法设置。

　　5) onInterceptTouchEvent()：重写了父类的onInterceptTouchEvent();主要功能是在onTouchEvent()方法之前处理touch事件。包括：down、up、move事件。

　　 用xy记录鼠标移动时的坐标值;

　　 mLastMotionX和mLastMotionY记录了上次鼠标按下时的XY值，在ACTION\_MOVE中赋值;

　　 xDiff和yDiff记录xy与mLastMotionX、mLastMotionY差值的绝对值;

　　 touchSlop为系统定义的标准值，初值为16;

　　 当xDiff和yDiff大于touchSlop时就认为界面拖动了足够大的距离，屏幕就可以移动了;

　　 当onInterceptTouchEvent()返回true时进入onTouchEvent()。

　　6) onTouchEvent()：重写了父类的onTouchEvent();主要功能是处理onInterceptTouchEvent()返回值为true时传递过来的touch事件。