Android13-Launcer3_图标和Hotseat添加选中背景

209 阅读1分钟

Workspace负责页面的布局

Workspace有多个CellLayout

CellLayout中的ShortcutAndWidgetContainer添加图标和Widget

在ShortcutAndWidgetContainer的onMeasure()结束后设置背景

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();

        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(widthSpecSize, heightSpecSize);

        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                measureChild(child);
            }
        }
    }

    public void measureChild(View child) {
        CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();
        final DeviceProfile dp = mActivity.getDeviceProfile();

        if (child instanceof NavigableAppWidgetHostView) {
            ((NavigableAppWidgetHostView) child).getWidgetInset(dp, mTempRect);
            final PointF appWidgetScale = dp.getAppWidgetScale((ItemInfo) child.getTag());
            lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX, mCountY,
                    appWidgetScale.x, appWidgetScale.y, mBorderSpace, mTempRect);
        } else {
            lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX, mCountY,
                    mBorderSpace, null);
            // Center the icon/folder
            int cHeight = getCellContentHeight();
            int cellPaddingY = dp.isScalableGrid && mContainerType == WORKSPACE
                    ? dp.cellYPaddingPx
                    : (int) Math.max(0, ((lp.height - cHeight) / 2f));

            // No need to add padding when cell layout border spacing is present.
            boolean noPaddingX =
                    (dp.cellLayoutBorderSpacePx.x > 0 && mContainerType == WORKSPACE)
                            || (dp.folderCellLayoutBorderSpacePx > 0 && mContainerType == FOLDER)
                            || (dp.hotseatBorderSpace > 0 && mContainerType == HOTSEAT);
            int cellPaddingX = noPaddingX
                    ? 0
                    : mContainerType == WORKSPACE
                            ? dp.workspaceCellPaddingXPx
                            : (int) (dp.edgeMarginPx / 2f);
            child.setPadding(cellPaddingX, cellPaddingY, cellPaddingX, 0);
        }
        int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.width, MeasureSpec.EXACTLY);
        int childheightMeasureSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
        child.measure(childWidthMeasureSpec, childheightMeasureSpec);
        //添加背景
        child.setBackgroundResource(R.drawable.cus_app_focus_bg);
    }

cus_app_focus_bg.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape>
            <solid android:color="#ff0000"></solid>
        </shape>
    </item>
    <item android:state_focused="true">
        <shape>
            <solid android:color="#ff0000"></solid>
        </shape>
    </item>
    <item android:state_selected="true">
        <shape>
            <solid android:color="#ff0000"></solid>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape>
            <solid android:color="#00ff0000"></solid>
        </shape>
    </item>
    <item android:state_focused="false">
        <shape>
            <solid android:color="#00ff0000"></solid>
        </shape>
    </item>
    <item android:state_selected="false">
        <shape>
            <solid android:color="#00ff0000"></solid>
        </shape>
    </item>
</selector>

效果
桌面焦点背景.jpg