Launcher3源码分析(DeviceProfile)

时间:2021-02-27 09:19:11

DeviceProfile定义了Launcher的很多属性,比如图标的大小、间距等。在Luancher.java的onCreate方法中初始化

//方向
mDeviceProfile = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ? app.getInvariantDeviceProfile().landscapeProfile : app.getInvariantDeviceProfile().portraitProfile;
//设置控件摆放位置
mDeviceProfile.layout(this);

代码太多,只分析几个经常改动相关的方法。

Public class DeviceProfile{

/**计算所有应用程序菜单的按钮尺寸*/
private void computeAllAppsButtonSize(Context context){

}
/**更新可用的间距*/
private void updateAvailableDimensions(DisplayMetrics dm, Resources res){
//调用updateIconSize方法
}

/**重点分析这个方法,Launcher的搜索栏、热门应用、文件夹、文字的宽高的计算
*如果想增加行列数,可以适当的调整图标大小、间距。
*/

private void updateIconSize(float scale, int drawablePadding, Resources res,
DisplayMetrics dm) {
iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize, dm) * scale);
iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize, dm) * scale);
iconDrawablePaddingPx = drawablePadding;
hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize, dm) * scale);

// Search Bar
searchBarSpaceWidthPx = Math.min(widthPx,
res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width));
searchBarSpaceHeightPx = getSearchBarTopOffset()
+ res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
// Calculate the actual text height
Paint textPaint = new Paint();
textPaint.setTextSize(iconTextSizePx);
FontMetrics fm = textPaint.getFontMetrics();
cellWidthPx = iconSizePx;
//cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top) + 2 * edgeMarginPx;
final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
dragViewScale = (iconSizePx + scaleDps) / iconSizePx;

// Hotseat
hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
hotseatCellWidthPx = iconSizePx;
hotseatCellHeightPx = iconSizePx;

// Folder
folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
//folderCellHeightPx = cellHeightPx + edgeMarginPx;
folderCellHeightPx = cellHeightPx + 3 * edgeMarginPx;
folderBackgroundOffset = -edgeMarginPx;
folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
}