下面列出了android.appwidget.AppWidgetHostView#getDefaultPaddingForWidget ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Bundle getDefaultOptionsForWidget(Context context, PendingAddWidgetInfo info) {
Rect rect = new Rect();
AppWidgetResizeFrame.getWidgetSizeRanges(context, info.spanX, info.spanY, rect);
Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context,
info.componentName, null);
float density = context.getResources().getDisplayMetrics().density;
int xPaddingDips = (int) ((padding.left + padding.right) / density);
int yPaddingDips = (int) ((padding.top + padding.bottom) / density);
Bundle options = new Bundle();
options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH,
rect.left - xPaddingDips);
options.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,
rect.top - yPaddingDips);
options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH,
rect.right - xPaddingDips);
options.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT,
rect.bottom - yPaddingDips);
return options;
}
void initSpans(Context context) {
InvariantDeviceProfile idp = LauncherAppState.getIDP(context);
Point paddingLand = idp.landscapeProfile.getTotalWorkspacePadding();
Point paddingPort = idp.portraitProfile.getTotalWorkspacePadding();
// Always assume we're working with the smallest span to make sure we
// reserve enough space in both orientations.
float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min(
idp.landscapeProfile.widthPx - paddingLand.x,
idp.portraitProfile.widthPx - paddingPort.x),
idp.numColumns);
float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min(
idp.landscapeProfile.heightPx - paddingLand.y,
idp.portraitProfile.heightPx - paddingPort.y),
idp.numRows);
// We want to account for the extra amount of padding that we are adding to the widget
// to ensure that it gets the full amount of space that it has requested.
Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
context, provider, null);
spanX = Math.max(1, (int) Math.ceil(
(minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
spanY = Math.max(1, (int) Math.ceil(
(minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
minSpanX = Math.max(1, (int) Math.ceil(
(minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
minSpanY = Math.max(1, (int) Math.ceil(
(minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
}
private void initSpans() {
LauncherAppState app = LauncherAppState.getInstance();
InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
// We only care out the cell size, which is independent of the the layout direction.
Rect paddingLand = idp.landscapeProfile.getWorkspacePadding(false /* isLayoutRtl */);
Rect paddingPort = idp.portraitProfile.getWorkspacePadding(false /* isLayoutRtl */);
// Always assume we're working with the smallest span to make sure we
// reserve enough space in both orientations.
float smallestCellWidth = DeviceProfile.calculateCellWidth(Math.min(
idp.landscapeProfile.widthPx - paddingLand.left - paddingLand.right,
idp.portraitProfile.widthPx - paddingPort.left - paddingPort.right),
idp.numColumns);
float smallestCellHeight = DeviceProfile.calculateCellWidth(Math.min(
idp.landscapeProfile.heightPx - paddingLand.top - paddingLand.bottom,
idp.portraitProfile.heightPx - paddingPort.top - paddingPort.bottom),
idp.numRows);
// We want to account for the extra amount of padding that we are adding to the widget
// to ensure that it gets the full amount of space that it has requested.
Rect widgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(
app.getContext(), provider, null);
spanX = Math.max(1, (int) Math.ceil(
(minWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
spanY = Math.max(1, (int) Math.ceil(
(minHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
minSpanX = Math.max(1, (int) Math.ceil(
(minResizeWidth + widgetPadding.left + widgetPadding.right) / smallestCellWidth));
minSpanY = Math.max(1, (int) Math.ceil(
(minResizeHeight + widgetPadding.top + widgetPadding.bottom) / smallestCellHeight));
}
static int[] getSpanForWidget(Context context, ComponentName component,
int minWidth, int minHeight) {
Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context,
component, null);
// We want to account for the extra amount of padding that we are adding
// to the widget
// to ensure that it gets the full amount of space that it has requested
int requiredWidth = minWidth + padding.left + padding.right;
int requiredHeight = minHeight + padding.top + padding.bottom;
return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
}
public DeviceProfile(Context context, InvariantDeviceProfile inv,
Point minSize, Point maxSize,
int width, int height, boolean isLandscape) {
this.inv = inv;
this.isLandscape = isLandscape;
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
// Constants from resources
isTablet = res.getBoolean(R.bool.is_tablet);
isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
isPhone = !isTablet && !isLargeTablet;
// Some more constants
transposeLayoutWithOrientation =
res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
ComponentName cn = new ComponentName(context.getPackageName(),
this.getClass().getName());
defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
desiredWorkspaceLeftRightMarginPx = edgeMarginPx;
pageIndicatorHeightPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
pageIndicatorLandGutterLeftNavBarPx = res.getDimensionPixelSize(
R.dimen.dynamic_grid_page_indicator_gutter_width_left_nav_bar);
pageIndicatorLandWorkspaceOffsetPx =
res.getDimensionPixelSize(R.dimen.all_apps_caret_workspace_offset);
pageIndicatorLandGutterRightNavBarPx = res.getDimensionPixelSize(
R.dimen.dynamic_grid_page_indicator_gutter_width_right_nav_bar);
defaultPageSpacingPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
topWorkspacePadding =
res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_top_padding);
overviewModeMinIconZoneHeightPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
overviewModeMaxIconZoneHeightPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
overviewModeBarItemWidthPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
overviewModeBarSpacerWidthPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
overviewModeIconZoneRatio =
res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
iconDrawablePaddingOriginalPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
dropTargetBarSizePx = res.getDimensionPixelSize(R.dimen.dynamic_grid_drop_target_size);
workspaceSpringLoadedBottomSpace =
res.getDimensionPixelSize(R.dimen.dynamic_grid_min_spring_loaded_space);
hotseatBarHeightPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_height);
hotseatBarTopPaddingPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_top_padding);
hotseatBarBottomPaddingPx = 0;
hotseatLandGutterPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_hotseat_gutter_width);
// Determine sizes.
widthPx = width;
heightPx = height;
if (isLandscape) {
availableWidthPx = maxSize.x;
availableHeightPx = minSize.y;
} else {
availableWidthPx = minSize.x;
availableHeightPx = maxSize.y;
}
// Calculate the remaining vars
updateAvailableDimensions(dm, res);
computeAllAppsButtonSize(context);
// This is done last, after iconSizePx is calculated above.
mBadgeRenderer = new BadgeRenderer(context, iconSizePx);
}
public DeviceProfile(Context context, InvariantDeviceProfile inv,
Point minSize, Point maxSize,
int width, int height, boolean isLandscape) {
this.inv = inv;
this.isLandscape = isLandscape;
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
// Constants from resources
isTablet = res.getBoolean(R.bool.is_tablet);
isLargeTablet = res.getBoolean(R.bool.is_large_tablet);
isPhone = !isTablet && !isLargeTablet;
// Some more constants
transposeLayoutWithOrientation =
res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
ComponentName cn = new ComponentName(context.getPackageName(),
this.getClass().getName());
defaultWidgetPadding = AppWidgetHostView.getDefaultPaddingForWidget(context, cn, null);
edgeMarginPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_edge_margin);
desiredWorkspaceLeftRightMarginPx = 2 * edgeMarginPx;
pageIndicatorHeightPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_page_indicator_height);
defaultPageSpacingPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_workspace_page_spacing);
overviewModeMinIconZoneHeightPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_min_icon_zone_height);
overviewModeMaxIconZoneHeightPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_max_icon_zone_height);
overviewModeBarItemWidthPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_item_width);
overviewModeBarSpacerWidthPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_overview_bar_spacer_width);
overviewModeIconZoneRatio =
res.getInteger(R.integer.config_dynamic_grid_overview_icon_zone_percentage) / 100f;
iconDrawablePaddingOriginalPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
// AllApps uses the original non-scaled icon text size
allAppsIconTextSizePx = Utilities.pxFromDp(inv.iconTextSize, dm);
// AllApps uses the original non-scaled icon size
allAppsIconSizePx = Utilities.pxFromDp(inv.iconSize, dm);
// Determine sizes.
widthPx = width;
heightPx = height;
if (isLandscape) {
availableWidthPx = maxSize.x;
availableHeightPx = minSize.y;
} else {
availableWidthPx = minSize.x;
availableHeightPx = maxSize.y;
}
// Calculate the remaining vars
updateAvailableDimensions(dm, res, isLandscape);
computeAllAppsButtonSize(context);
// Search Bar
searchBarVisible = isSearchBarEnabled(context);
searchBarSpaceWidthPx = Math.min(searchBarSpaceWidthPx, widthPx);
defaultSearchBarSpaceHeightPx = getSearchBarTopOffset()
+ res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
searchBarSpaceHeightPx = 2 * edgeMarginPx + (searchBarVisible ?
defaultSearchBarSpaceHeightPx - getSearchBarTopOffset() : 3 * edgeMarginPx);
}
public AppWidgetResizeFrame(Context context,
LauncherAppWidgetHostView widgetView, CellLayout cellLayout, DragLayer dragLayer) {
super(context);
mLauncher = (Launcher) context;
mCellLayout = cellLayout;
mWidgetView = widgetView;
mDragLayer = dragLayer;
final AppWidgetProviderInfo info = widgetView.getAppWidgetInfo();
int[] result = Launcher.getMinSpanForWidget(mLauncher, info);
mMinHSpan = result[0];
mMinVSpan = result[1];
setBackgroundResource(R.drawable.widget_resize_frame_holo);
setPadding(0, 0, 0, 0);
LayoutParams lp;
mLeftHandle = new ImageView(context);
mLeftHandle.setImageResource(R.drawable.widget_resize_handle_left);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.LEFT | Gravity.CENTER_VERTICAL);
addView(mLeftHandle, lp);
mRightHandle = new ImageView(context);
mRightHandle.setImageResource(R.drawable.widget_resize_handle_right);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.RIGHT | Gravity.CENTER_VERTICAL);
addView(mRightHandle, lp);
mTopHandle = new ImageView(context);
mTopHandle.setImageResource(R.drawable.widget_resize_handle_top);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.TOP);
addView(mTopHandle, lp);
mBottomHandle = new ImageView(context);
mBottomHandle.setImageResource(R.drawable.widget_resize_handle_bottom);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
addView(mBottomHandle, lp);
Rect p = AppWidgetHostView.getDefaultPaddingForWidget(context,
widgetView.getAppWidgetInfo().provider, null);
mWidgetPaddingLeft = p.left;
mWidgetPaddingTop = p.top;
mWidgetPaddingRight = p.right;
mWidgetPaddingBottom = p.bottom;
final float density = mLauncher.getResources().getDisplayMetrics().density;
mBackgroundPadding = (int) Math.ceil(density * BACKGROUND_PADDING);
mTouchTargetWidth = 2 * mBackgroundPadding;
// When we create the resize frame, we first mark all cells as unoccupied. The appropriate
// cells (same if not resized, or different) will be marked as occupied when the resize
// frame is dismissed.
mCellLayout.markCellsAsUnoccupiedForView(mWidgetView);
}
public AppWidgetResizeFrame(Context context,
LauncherAppWidgetHostView widgetView, CellLayout cellLayout, DragLayer dragLayer) {
super(context);
mLauncher = (Launcher) context;
mCellLayout = cellLayout;
mWidgetView = widgetView;
mResizeMode = widgetView.getAppWidgetInfo().resizeMode;
mDragLayer = dragLayer;
final AppWidgetProviderInfo info = widgetView.getAppWidgetInfo();
int[] result = Launcher.getMinSpanForWidget(mLauncher, info);
mMinHSpan = result[0];
mMinVSpan = result[1];
setBackgroundResource(R.drawable.widget_resize_frame_holo);
setPadding(0, 0, 0, 0);
LayoutParams lp;
mLeftHandle = new ImageView(context);
mLeftHandle.setImageResource(R.drawable.widget_resize_handle_left);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.LEFT | Gravity.CENTER_VERTICAL);
addView(mLeftHandle, lp);
mRightHandle = new ImageView(context);
mRightHandle.setImageResource(R.drawable.widget_resize_handle_right);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.RIGHT | Gravity.CENTER_VERTICAL);
addView(mRightHandle, lp);
mTopHandle = new ImageView(context);
mTopHandle.setImageResource(R.drawable.widget_resize_handle_top);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.TOP);
addView(mTopHandle, lp);
mBottomHandle = new ImageView(context);
mBottomHandle.setImageResource(R.drawable.widget_resize_handle_bottom);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
addView(mBottomHandle, lp);
Rect p = AppWidgetHostView.getDefaultPaddingForWidget(context,
widgetView.getAppWidgetInfo().provider, null);
mWidgetPaddingLeft = p.left;
mWidgetPaddingTop = p.top;
mWidgetPaddingRight = p.right;
mWidgetPaddingBottom = p.bottom;
if (mResizeMode == AppWidgetProviderInfo.RESIZE_HORIZONTAL) {
mTopHandle.setVisibility(GONE);
mBottomHandle.setVisibility(GONE);
} else if (mResizeMode == AppWidgetProviderInfo.RESIZE_VERTICAL) {
mLeftHandle.setVisibility(GONE);
mRightHandle.setVisibility(GONE);
}
final float density = mLauncher.getResources().getDisplayMetrics().density;
mBackgroundPadding = (int) Math.ceil(density * BACKGROUND_PADDING);
mTouchTargetWidth = 2 * mBackgroundPadding;
// When we create the resize frame, we first mark all cells as unoccupied. The appropriate
// cells (same if not resized, or different) will be marked as occupied when the resize
// frame is dismissed.
mCellLayout.markCellsAsUnoccupiedForView(mWidgetView);
}