Launcher图标角标

前言

桌面图标的角标,看着是个很简单的功能,就是在应用的右上角显示当前有几个未读消息;在网上查了资料之后,发现很多同行说,Android原生是没有此功能,平时使用的手机都有该功能,其实是国内手机厂商自己定制的桌面图标角标,且不同厂商之间,方案还不尽相同;但是此次我还是要看Android源生代码究竟是怎么显示应用有新的未读消息,此篇文章是以为记。
在这里插入图片描述

很直观,它是显示在应用的右上角的,那么我们要查看它,得先在代码中找到单个应用显示的类,根据上篇文章介绍,应用是在CellLayout平均分配的矩形中显示,可以先查看CellLayout的源码,在CellLayout的源码中,找到了下面的方法:

    public boolean addViewToCellLayout(View child, int index, int childId, LayoutParams params,
            boolean markCells) {
   
        final LayoutParams lp = params;

        // Hotseat icons - remove text
        if (child instanceof BubbleTextView) {
   
            BubbleTextView bubbleChild = (BubbleTextView) child;
            bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
        }

        child.setScaleX(mChildScale);
        child.setScaleY(mChildScale);

        // Generate an id for each view, this assumes we have at most 256x256 cells
        // per workspace screen
        if (lp.cellX >= 0 && lp.cellX <= mCountX - 1 && lp.cellY >= 0 && lp.cellY <= mCountY - 1) {
   
            // If the horizontal or vertical span is set to -1, it is taken to
            // mean that it spans the extent of the CellLayout
            if (lp.cellHSpan < 0) lp.cellHSpan = mCountX;
            if (lp.cellVSpan < 0) lp.cellVSpan = mCountY;

            child.setId(childId);
            if (LOGD) {
   
                Log.d(TAG, "Adding view to ShortcutsAndWidgetsContainer: " + child);
            }
            mShortcutsAndWidgets.addView(child, index, lp);

            if (markCells) markCellsAsOccupiedForView(child);

            return true;
        }
        return false;
    }

该方法是将子View添加到CellLayout中;而我们知道CellLayout中要么显示应用的快捷方式,要不显示桌面小部件,如果是Hotseat里么的CellLayout,那么它就只能显示应用的快捷方式,且不显示它的名称,从代码得知应用的快捷方式的类就是BubbleTextView。

        // Hotseat icons - remove text
        if (child instanceof BubbleTextView) {
   
            BubbleTextView bubbleChild = (BubbleTextView) child;
            bubbleChild.setTextVisibility(mContainerType != HOTSEAT);
        }

在BubbleTextView源码中,确实有看到在该View的右上角画标记,但是没有发现有设置未读消息的代码,经过修改代码运行得知,此标记仅表示有新消息,但是没有消息的条数,具体修改代码如下:

    protected void drawBadgeIfNecessary(Canvas canvas) {
   
        //设置mBadgeScale以满足条件,画出右上角标记信息
        mBadgeScale = 1.0f;
        if (!mForceHideBadge && (hasBadge() || mBadgeScale > 0)) {
   
            getIconBounds(mTempIconBounds);
            mTempSpaceForBadgeOffset.set((getWidth() - mIconSize) / 2, getPaddingTop());
            final int scrollX = getScrollX();
            final int scrollY = getScrollY();
            canvas.translate(scrollX, scrollY);
            mBadgeRenderer.draw(canvas, mBadgeColor, mTempIconBounds, mBadgeScale,
                    mTempSpaceForBadgeOffset);
            canvas.translate(-scrollX, -scrollY);
        }
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值