Workspace主要功能:完成多个屏幕的以及壁纸的显示,多个屏幕之间的切换和壁纸的添加。
/**
* Used to inflate the Workspace from XML.
*/
public Workspace(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//获取绘制轮廓的辅助类对象
mOutlineHelper = HolographicOutlineHelper.obtain(context);
mLauncher = (Launcher) context;
mStateTransitionAnimation = new WorkspaceStateTransitionAnimation(mLauncher, this);
final Resources res = getResources();
DeviceProfile grid = mLauncher.getDeviceProfile();
mWorkspaceFadeInAdjacentScreens = grid.shouldFadeAdjacentWorkspaceScreens();
mFadeInAdjacentScreens = false;
//获取Wallpaper管理器
mWallpaperManager = WallpaperManager.getInstance(context);
//获取自定义属性
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.Workspace, defStyle, 0);
//在allapp 应用程序菜单里拖动app时workspace的缩放比例
mSpringLoadedShrinkFactor =
res.getInteger(R.integer.config_workspaceSpringLoadShrinkPercentage) / 100.0f;
//长按桌面进入预览模式时的缩放比例
mOverviewModeShrinkFactor = grid.getOverviewModeScale(mIsRtl);
//开机时的屏幕
mOriginalDefaultPage = mDefaultPage = a.getInt(R.styleable.Workspace_defaultScreen, 1);
a.recycle();
//监听view层次的变化
setOnHierarchyChangeListener(this);
//打开触摸反馈
setHapticFeedbackEnabled(false);
//初始化workspace
initWorkspace();
// Disable multitouch across the workspace/all apps/customize tray
setMotionEventSplittingEnabled(true);
}
初始化workspace
/**
* Initializes various states for this workspace.
*/
protected void initWorkspace() {
//默认页
mCurrentPage = mDefaultPage;
LauncherAppState app = LauncherAppState.getInstance();
DeviceProfile grid = mLauncher.getDeviceProfile();
//保存应用图片的缓存
mIconCache = app.getIconCache();
setWillNotDraw(false);
setClipChildren(false);
setClipToPadding(false);
//设置子view绘图缓存开始
setChildrenDrawnWithCacheEnabled(true);
setMinScale(mOverviewModeShrinkFactor);
setupLayoutTransition();
//wallpaper偏移
mWallpaperOffset = new WallpaperOffsetInterpolator();
//获取屏幕大小
Display display = mLauncher.getWindowManager().getDefaultDisplay();
display.getSize(mDisplaySize);
mMaxDistanceForFolderCreation = (0.55f * grid.iconSizePx);
// Set the wallpaper dimensions when Launcher starts up
setWallpaperDimension();
setEdgeGlowColor(getResources().getColor(R.color.workspace_edge_effect_color));
}
workspace实现了DragSource和Dragtarget,说明它即是一个拖动的容器也是一个拖动的源,分析开始拖动方法:
public void startDrag(CellLayout.CellInfo cellInfo){
startDrag(cellInfo,false);
}
public void startDrag(CellLayout.CellInfo cellInfo,boolean accessible){
View child = cellInfo.cell;
// Make sure the drag was started by a long press as opposed to a long click.
if (!child.isInTouchMode()) {
return;
}
mDragInfo = cellInfo;
//原位置的item设置为不可见
child.setVisibility(INVISIBLE);
CellLayout layout = (CellLayout) child.getParent().getParent();
layout.prepareChildForDrag(child);
beginDragShared(child, this, accessible);
}
public void beginDragShared(View child, DragSource source,boolean accessible){
beginDragShared(child, new Point(), source, accessible);
}
public void beginDragShared(View child, Point relativeTouchPos,DragSource source, boolean accessible){
child.clearFocus();
child.setPressed(false);