編輯:關於 Android 程式設計
對比兩個平台的 Launcher3 專案,雖然版本號都是 3,代碼卻已大不相同。新版本裡,DeviceProfile 從 DynamicGrid 中獨立了出來,成了單獨的類。
這帶來的直接後果是:以往加在 DynamicGrid 上的大量屬性不再適用,需要重新讀源碼逐項遷移,工作量著實不小。
舊結構中,網格的行列數、圖標尺寸、熱區圖標數等參數都掛在 DynamicGrid 上,DeviceProfile 更多扮演查詢載體的角色。新結構把這些字段全部收攏進 DeviceProfile 自身,並通過構造方法接收預設值,同時引入 invDistWeightedInterpolate() 做反距離加權插值——根據當前屏幕的 dp 尺寸,在若干預設 profile 之間算出最合適的行列與圖標大小。這也是它比舊版自適應能力更強的原因。
以下為改動後的 DeviceProfile.java 主體,註釋中保留了幾處個人定製痕跡:
package com.android.launcher3;
import android.appwidget.AppWidgetHostView;
import android.content.ComponentName;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class DeviceProfileQuery {
float widthDps;
float heightDps;
float value;
PointF dimens;
DeviceProfileQuery(float w, float h, float v) {
widthDps = w;
heightDps = h;
value = v;
dimens = new PointF(w, h);
}
}
public class DeviceProfile {
public static interface DeviceProfileCallbacks {
public void onAvailableSizeChanged(DeviceProfile grid);
}
String name;
float minWidthDps;
float minHeightDps;
float numRows;
float numColumns;
float numHotseatIcons;
private float iconSize;
private float iconTextSize;
private int iconDrawablePaddingOriginalPx;
private float hotseatIconSize;
boolean isLandscape;
boolean isTablet;
boolean isLargeTablet;
boolean isLayoutRtl;
boolean transposeLayoutWithOrientation;
int desiredWorkspaceLeftRightMarginPx;
int edgeMarginPx;
Rect defaultWidgetPadding;
int widthPx;
int heightPx;
int availableWidthPx;
int availableHeightPx;
int defaultPageSpacingPx;
int overviewModeMinIconZoneHeightPx;
int overviewModeMaxIconZoneHeightPx;
int overviewModeBarItemWidthPx;
int overviewModeBarSpacerWidthPx;
float overviewModeIconZoneRatio;
float overviewModeScaleFactor;
int iconSizePx;
int iconTextSizePx;
int iconDrawablePaddingPx;
int cellWidthPx;
int cellHeightPx;
int allAppsIconSizePx;
int allAppsIconTextSizePx;
int allAppsCellWidthPx;
int allAppsCellHeightPx;
int allAppsCellPaddingPx;
int folderBackgroundOffset;
int folderIconSizePx;
int folderCellWidthPx;
int folderCellHeightPx;
int hotseatCellWidthPx;
int hotseatCellHeightPx;
int hotseatIconSizePx;
int hotseatBarHeightPx;
int hotseatAllAppsRank;
int allAppsNumRows;
int allAppsNumCols;
int searchBarSpaceWidthPx;
int searchBarSpaceMaxWidthPx;
int searchBarSpaceHeightPx;
int searchBarHeightPx;
int pageIndicatorHeightPx;
float dragViewScale;
private ArrayList<DeviceProfileCallbacks> mCallbacks = new ArrayList<DeviceProfileCallbacks>();
DeviceProfile(String n, float w, float h, float r, float c,
float is, float its, float hs, float his) {
// 確保 hotseat 格子數為奇數(因為需要放置 all apps)
if (!LauncherAppState.isDisableAllApps() && hs % 2 == 0) {
throw new RuntimeException("All Device Profiles must have an odd number of hotseat spaces");
}
name = n;
minWidthDps = 1200;
minHeightDps = 1920;
numRows = 5;
numColumns = 6;
iconSize = 68;
iconTextSize = 14.4f;
numHotseatIcons = 7;
hotseatIconSize = 72;
}
DeviceProfile(Context context, ArrayList<DeviceProfile> profiles,
float minWidth, float minHeight,
int wPx, int hPx, int awPx, int ahPx, Resources res) {
DisplayMetrics dm = res.getDisplayMetrics();
ArrayList<DeviceProfileQuery> points = new ArrayList<DeviceProfileQuery>();
transposeLayoutWithOrientation =
res.getBoolean(R.bool.hotseat_transpose_layout_with_orientation);
minWidthDps = minWidth;
minHeightDps = minHeight;
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);
allAppsCellPaddingPx = res.getDimensionPixelSize(R.dimen.dynamic_grid_all_apps_cell_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;
overviewModeScaleFactor =
res.getInteger(R.integer.config_dynamic_grid_overview_scale_percentage) / 100f;
// 插值計算行數
for (DeviceProfile p : profiles) {
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numRows));
}
numRows = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
// 插值計算列數
points.clear();
for (DeviceProfile p : profiles) {
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numColumns));
}
numColumns = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
// 插值計算 hotseat 長度
points.clear();
for (DeviceProfile p : profiles) {
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.numHotseatIcons));
}
numHotseatIcons = Math.round(invDistWeightedInterpolate(minWidth, minHeight, points));
hotseatAllAppsRank = (int) (numHotseatIcons / 2);
// 插值計算圖標大小
points.clear();
for (DeviceProfile p : profiles) {
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconSize));
}
iconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
// AllApps 使用未縮放的原圖標尺寸
allAppsIconSizePx = DynamicGrid.pxFromDp(iconSize, dm);
// 插值計算圖標文字大小
points.clear();
for (DeviceProfile p : profiles) {
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.iconTextSize));
}
iconTextSize = invDistWeightedInterpolate(minWidth, minHeight, points);
iconDrawablePaddingOriginalPx =
res.getDimensionPixelSize(R.dimen.dynamic_grid_icon_drawable_padding);
allAppsIconTextSizePx = DynamicGrid.pxFromDp(iconTextSize, dm);
// 插值計算 hotseat 圖標大小
points.clear();
for (DeviceProfile p : profiles) {
points.add(new DeviceProfileQuery(p.minWidthDps, p.minHeightDps, p.hotseatIconSize));
}
hotseatIconSize = invDistWeightedInterpolate(minWidth, minHeight, points);
updateFromConfiguration(context, res, wPx, hPx, awPx, ahPx);
updateAvailableDimensions(context);
}
void addCallback(DeviceProfileCallbacks cb) {
mCallbacks.add(cb);
cb.onAvailableSizeChanged(this);
}
void removeCallback(DeviceProfileCallbacks cb) {
mCallbacks.remove(cb);
}
private void updateAvailableDimensions(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Resources resources = context.getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
// 動態網格需要考慮三種配置:豎屏、橫屏且導航列在底部、橫屏且導航列在側邊。
// 橫屏時高度取最小值,豎屏時取最大可能高度。
Point size = new Point();
Point smallestSize = new Point();
Point largestSize = new Point();
display.getSize(size);
display.getCurrentSizeRange(smallestSize, largestSize);
availableWidthPx = size.x;
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
availableHeightPx = smallestSize.y;
} else {
availableHeightPx = largestSize.y;
}
// 檢查圖標是否放得下,放不下則縮小
float scale = 1f;
int drawablePadding = iconDrawablePaddingOriginalPx;
updateIconSize(1f, drawablePadding, resources, dm);
float usedHeight = (cellHeightPx * numRows);
Rect workspacePadding = getWorkspacePadding();
int maxHeight = (availableHeightPx - workspacePadding.top - workspacePadding.bottom);
if (usedHeight > maxHeight) {
scale = maxHeight / usedHeight;
drawablePadding = 0;
}
updateIconSize(scale, drawablePadding, resources, dm);
for (DeviceProfileCallbacks cb : mCallbacks) {
cb.onAvailableSizeChanged(this);
}
}
private void updateIconSize(float scale, int drawablePadding,
Resources resources, DisplayMetrics dm) {
iconSizePx = (int) (DynamicGrid.pxFromDp(iconSize, dm) * scale);
iconTextSizePx = (int) (DynamicGrid.pxFromSp(iconTextSize, dm) * scale);
iconDrawablePaddingPx = drawablePadding;
hotseatIconSizePx = (int) (DynamicGrid.pxFromDp(hotseatIconSize, dm) * scale);
searchBarSpaceMaxWidthPx =
resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width);
searchBarHeightPx =
resources.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height);
searchBarSpaceWidthPx = Math.min(searchBarSpaceMaxWidthPx, widthPx);
searchBarSpaceHeightPx = searchBarHeightPx + getSearchBarTopOffset();
// 計算實際文字高度
Paint textPaint = new Paint();
textPaint.setTextSize(iconTextSizePx);
FontMetrics fm = textPaint.getFontMetrics();
cellWidthPx = iconSizePx;
cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top);
final float scaleDps = resources.getDimensionPixelSize(R.dimen.dragViewScale);
dragViewScale = (iconSizePx + scaleDps) / iconSizePx;
hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx;
hotseatCellWidthPx = iconSizePx;
hotseatCellHeightPx = iconSizePx;
folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx;
folderCellHeightPx = cellHeightPx + edgeMarginPx;
folderBackgroundOffset = -edgeMarginPx;
folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset;
Rect padding = getWorkspacePadding(isLandscape
? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
int pageIndicatorOffset =
resources.getDimensionPixelSize(R.dimen.apps_customize_page_indicator_offset);
allAppsCellWidthPx = allAppsIconSizePx;
allAppsCellHeightPx = allAppsIconSizePx + drawablePadding + iconTextSizePx;
int maxLongEdgeCellCount =
resources.getInteger(R.integer.config_dynamic_grid_max_long_edge_cell_count);
int maxShortEdgeCellCount =
resources.getInteger(R.integer.config_dynamic_grid_max_short_edge_cell_count);
int minEdgeCellCount =
resources.getInteger(R.integer.config_dynamic_grid_min_edge_cell_count);
int maxRows = (isLandscape ? maxShortEdgeCellCount : maxLongEdgeCellCount);
int maxCols = (isLandscape ? maxLongEdgeCellCount : maxShortEdgeCellCount);
// 定製:固定 all apps 的行列數
if (resources.getInteger(R.integer.cellCountX) > 0
&& resources.getInteger(R.integer.cellCountY) > 0) {
allAppsNumRows = 7;
allAppsNumCols = 4;
} else {
allAppsNumRows = (availableHeightPx - pageIndicatorHeightPx)
/ (allAppsCellHeightPx + allAppsCellPaddingPx);
allAppsNumRows = 7;
allAppsNumCols = 4;
}
}
void updateFromConfiguration(Context context, Resources resources,
int wPx, int hPx, int awPx, int ahPx) {
Configuration configuration = resources.getConfiguration();
isLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);
isTablet = resources.getBoolean(R.bool.is_tablet);
isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
isLayoutRtl = (configuration.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
} else {
isLayoutRtl = false;
}
widthPx = wPx;
heightPx = hPx;
availableWidthPx = awPx;
availableHeightPx = ahPx;
updateAvailableDimensions(context);
}
private float dist(PointF p0, PointF p1) {
return (float) Math.sqrt((p1.x - p0.x) * (p1.x - p0.x) + (p1.y - p0.y) * (p1.y - p0.y));
}
private float weight(PointF a, PointF b, float pow) {
float d = dist(a, b);
if (d == 0f) {
return Float.POSITIVE_INFINITY;
}
return (float) (1f / Math.pow(d, pow));
}
private float invDistWeightedInterpolate(float width, float height,
ArrayList<DeviceProfileQuery> points) {
float sum = 0;
float weights = 0;
float pow = 5;
float kNearestNeighbors = 3;
final PointF xy = new PointF(width, height);
ArrayList<DeviceProfileQuery> pointsByNearness = points;
Collections.sort(pointsByNearness, new Comparator<DeviceProfileQuery>() {
public int compare(DeviceProfileQuery a, DeviceProfileQuery b) {
return (int) (dist(xy, a.dimens) - dist(xy, b.dimens));
}
});
for (int i = 0; i < pointsByNearness.size(); ++i) {
DeviceProfileQuery p = pointsByNearness.get(i);
if (i < kNearestNeighbors) {
float w = weight(xy, p.dimens, pow);
if (w == Float.POSITIVE_INFINITY) {
return p.value;
}
weights += w;
}
}
for (int i = 0; i < pointsByNearness.size(); ++i) {
DeviceProfileQuery p = pointsByNearness.get(i);
if (i < kNearestNeighbors) {
float w = weight(xy, p.dimens, pow);
sum += w * p.value / weights;
}
}
return sum;
}
/** 返回搜索欄頂部偏移 */
int getSearchBarTopOffset() {
if (isTablet() && !isVerticalBarLayout()) {
return 4 * edgeMarginPx;
} else {
return 2 * edgeMarginPx;
}
}
/** 返回當前方向下的搜索欄邊界 */
Rect getSearchBarBounds() {
return getSearchBarBounds(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
}
Rect getSearchBarBounds(int orientation) {
Rect bounds = new Rect();
if (orientation == CellLayout.LANDSCAPE && transposeLayoutWithOrientation) {
if (isLayoutRtl) {
bounds.set(availableWidthPx - searchBarSpaceHeightPx, edgeMarginPx,
availableWidthPx, availableHeightPx - edgeMarginPx);
} else {
bounds.set(0, edgeMarginPx, searchBarSpaceHeightPx, availableHeightPx - edgeMarginPx);
}
} else {
if (isTablet()) {
int width = (orientation == CellLayout.LANDSCAPE)
? Math.max(widthPx, heightPx) : Math.min(widthPx, heightPx);
int gap = (int) ((width - 2 * edgeMarginPx - (numColumns * cellWidthPx))
/ (2 * (numColumns + 1)));
bounds.set(edgeMarginPx + gap, getSearchBarTopOffset(),
availableWidthPx - (edgeMarginPx + gap), searchBarSpaceHeightPx);
} else {
bounds.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
getSearchBarTopOffset(),
availableWidthPx - (desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right),
searchBarSpaceHeightPx);
}
}
return bounds;
}
/** 返回工作區頁碼指示器的邊界 */
Rect getWorkspacePageIndicatorBounds(Rect insets) {
Rect workspacePadding = getWorkspacePadding();
if (isLandscape && transposeLayoutWithOrientation) {
if (isLayoutRtl) {
return new Rect(workspacePadding.left, workspacePadding.top,
workspacePadding.left + pageIndicatorHeightPx,
heightPx - workspacePadding.bottom - insets.bottom);
} else {
int pageIndicatorLeft = widthPx - workspacePadding.right;
return new Rect(pageIndicatorLeft, workspacePadding.top,
pageIndicatorLeft + pageIndicatorHeightPx,
heightPx - workspacePadding.bottom - insets.bottom);
}
} else {
int pageIndicatorTop = heightPx - insets.bottom - workspacePadding.bottom;
return new Rect(workspacePadding.left, pageIndicatorTop,
widthPx - workspacePadding.right, pageIndicatorTop + pageIndicatorHeightPx);
}
}
Rect getWorkspacePadding() {
return getWorkspacePadding(isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT);
}
Rect getWorkspacePadding(int orientation) {
Rect searchBarBounds = getSearchBarBounds(orientation);
Rect padding = new Rect();
if (orientation == CellLayout.LANDSCAPE && transposeLayoutWithOrientation) {
if (isLayoutRtl) {
padding.set(hotseatBarHeightPx, edgeMarginPx, searchBarBounds.width(), edgeMarginPx);
} else {
padding.set(searchBarBounds.width(), edgeMarginPx, hotseatBarHeightPx, edgeMarginPx);
}
} else {
if (isTablet()) {
float gapScale = 1f + (dragViewScale - 1f) / 2f;
int width = (orientation == CellLayout.LANDSCAPE)
? Math.max(widthPx, heightPx) : Math.min(widthPx, heightPx);
int height = (orientation != CellLayout.LANDSCAPE)
? Math.max(widthPx, heightPx) : Math.min(widthPx, heightPx);
int paddingTop = searchBarBounds.bottom;
int paddingBottom = hotseatBarHeightPx + pageIndicatorHeightPx;
int availableWidth = Math.max(0, width - (int) ((numColumns * cellWidthPx)
+ (numColumns * gapScale * cellWidthPx)));
int availableHeight = Math.max(0, height - paddingTop - paddingBottom
- (int) (2 * numRows * cellHeightPx));
padding.set(availableWidth / 2, paddingTop + availableHeight / 2,
availableWidth / 2, paddingBottom + availableHeight / 2);
} else {
padding.set(desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.left,
searchBarBounds.bottom,
desiredWorkspaceLeftRightMarginPx - defaultWidgetPadding.right,
hotseatBarHeightPx + pageIndicatorHeightPx);
}
}
return padding;
}
int getWorkspacePageSpacing(int orientation) {
if ((orientation == CellLayout.LANDSCAPE && transposeLayoutWithOrientation)
|| isLargeTablet()) {
return defaultPageSpacingPx;
} else {
return Math.max(defaultPageSpacingPx, 2 * getWorkspacePadding().left);
}
}
Rect getOverviewModeButtonBarRect() {
int zoneHeight = (int) (overviewModeIconZoneRatio * availableHeightPx);
zoneHeight = Math.min(overviewModeMaxIconZoneHeightPx,
Math.max(overviewModeMinIconZoneHeightPx, zoneHeight));
return new Rect(0, availableHeightPx - zoneHeight, 0, availableHeightPx);
}
float getOverviewModeScale() {
Rect workspacePadding = getWorkspacePadding();
Rect overviewBar = getOverviewModeButtonBarRect();
int pageSpace = availableHeightPx - workspacePadding.top - workspacePadding.bottom;
return (overviewModeScaleFactor * (pageSpace - overviewBar.height())) / pageSpace;
}
Rect getHotseatRect() {
if (isVerticalBarLayout()) {
return new Rect(availableWidthPx - hotseatBarHeightPx, 0,
Integer.MAX_VALUE, availableHeightPx);
} else {
return new Rect(0, availableHeightPx - hotseatBarHeightPx,
availableWidthPx, Integer.MAX_VALUE);
}
}
boolean isPhone() { return !isTablet && !isLargeTablet; }
boolean isTablet() { return isTablet; }
boolean isLargeTablet() { return isLargeTablet; }
boolean isVerticalBarLayout() { return isLandscape && transposeLayoutWithOrientation; }
boolean shouldFadeAdjacentWorkspaceScreens() { return isVerticalBarLayout() || isLargeTablet; }
int calculateCellWidth(int width, int countX) { return width / countX; }
int calculateCellHeight(int height, int countY) { return height / countY; }
int getVisibleChildCount(ViewGroup parent) {
int visibleChildren = 0;
for (int i = 0; i < parent.getChildCount(); i++) {
if (parent.getChildAt(i).getVisibility() != View.GONE) {
visibleChildren++;
}
}
return visibleChildren;
}
int calculateOverviewModeWidth(int visibleChildCount) {
return visibleChildCount * overviewModeBarItemWidthPx
+ (visibleChildCount - 1) * overviewModeBarSpacerWidthPx;
}
public void layout(Launcher launcher) {
FrameLayout.LayoutParams lp;
Resources res = launcher.getResources();
boolean hasVerticalBarLayout = isVerticalBarLayout();
// 布局搜索欄
View searchBar = launcher.getSearchBar();
lp = (FrameLayout.LayoutParams) searchBar.getLayoutParams();
if (hasVerticalBarLayout) {
lp.gravity = Gravity.TOP | Gravity.LEFT;
lp.width = searchBarSpaceHeightPx;
lp.height = LayoutParams.WRAP_CONTENT;
searchBar.setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
LinearLayout targets = (LinearLayout) searchBar.findViewById(R.id.drag_target_bar);
targets.setOrientation(LinearLayout.VERTICAL);
} else {
lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
lp.width = searchBarSpaceWidthPx;
lp.height = searchBarSpaceHeightPx;
searchBar.setPadding(2 * edgeMarginPx, getSearchBarTopOffset(), 2 * edgeMarginPx, 0);
}
searchBar.setLayoutParams(lp);
// 布局工作區
PagedView workspace = (PagedView) launcher.findViewById(R.id.workspace);
lp = (FrameLayout.LayoutParams) workspace.getLayoutParams();
lp.gravity = Gravity.CENTER;
int orientation = isLandscape ? CellLayout.LANDSCAPE : CellLayout.PORTRAIT;
Rect padding = getWorkspacePadding(orientation);
workspace.setLayoutParams(lp);
workspace.setPadding(padding.left, padding.top, padding.right, padding.bottom);
workspace.setPageSpacing(getWorkspacePageSpacing(orientation));
// 布局 hotseat
View hotseat = launcher.findViewById(R.id.hotseat);
lp = (FrameLayout.LayoutParams) hotseat.getLayoutParams();
if (hasVerticalBarLayout) {
lp.gravity = Gravity.END;
lp.width = hotseatBarHeightPx;
lp.height = LayoutParams.MATCH_PARENT;
hotseat.findViewById(R.id.layout)
.setPadding(0, 2 * edgeMarginPx, 0, 2 * edgeMarginPx);
} else if (isTablet()) {
lp.gravity = Gravity.BOTTOM;
lp.width = LayoutParams.MATCH_PARENT;
lp.height = hotseatBarHeightPx;
hotseat.setPadding(edgeMarginPx + padding.left, 0,
edgeMarginPx + padding.right, 2 * edgeMarginPx);
} else {
// 手機上不設底部 margin,以留出文件夾空間
lp.gravity = Gravity.BOTTOM;
lp.width = LayoutParams.MATCH_PARENT;
lp.height = hotseatBarHeightPx;
hotseat.findViewById(R.id.layout)
.setPadding(2 * edgeMarginPx, 0, 2 * edgeMarginPx, 0);
}
hotseat.setLayoutParams(lp);
// 布局頁碼指示器
View pageIndicator = launcher.findViewById(R.id.page_indicator);
if (pageIndicator != null) {
if (hasVerticalBarLayout) {
pageIndicator.setVisibility(View.GONE);
} else {
lp = (FrameLayout.LayoutParams) pageIndicator.getLayoutParams();
lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
lp.width = LayoutParams.WRAP_CONTENT;
lp.height = LayoutParams.WRAP_CONTENT;
lp.bottomMargin = hotseatBarHeightPx;
pageIndicator.setLayoutParams(lp);
}
}
}
}
如果你之前在 DynamicGrid 裡改過行列數或圖標尺寸,遷到新結構後要改的地方變成了 DeviceProfile 的構造方法與 updateIconSize(),別再去找 DynamicGrid 裡的對應字段。另外 invDistWeightedInterpolate() 會覆蓋你寫死的數值,想固定行列數就必須像示例那樣,在插值之後再強制賦值,否則不同屏幕上會呈現不同結果。
Android 音樂播放器實作:已存在歌曲的歌詞下載與顯示
在本機音樂播放器開發中,歌詞顯示是提升使用體驗的重要功能之一。當歌曲已經存在於裝置中時,若能自動下載並載入對應歌詞,就能讓播放介面更完整。本文將說明如何在 Android
Android 消息處理機制源碼剖析:搞懂 Handler、Looper、Message 並實作圖片異步加載
引言 做 Android 開發時經常需要異步加載圖片、網頁或其他資源。要實現異步加載,本質上要解決的是執行緒間通信——而在 Android 中,Handler、Loope
Android 開發:為 PullToRefresh 列表加入點擊監聽,實作長按刪除 Item
本文為原創部落格分享,記錄了在專案中為下拉刷新列表加入點擊與長按刪除功能的實作過程與踩雷經驗。 截至目前,專案的賣家版本大致已完成,一路攻關下來速度也控制得不錯。從任務分
用 TextureMapFragment 打造模組化地圖:Android 百度地圖 MapFragment 實作
想讓地圖像積木一樣被輕鬆放進畫面的任何角落,還能自訂縮放、視角與羅盤顯示嗎?TextureMapFragment 正是為此而生——它是一個用來承載地圖片段的控件,能與 F