为ActionBar Home Icon添加徽章

时间:2015-03-04 14:35:12

标签: android

我需要在操作栏主页按钮(活动图标)中添加徽章计数。 我有一个抽屉布局,当我按下主页按钮时打开 如果发生某种行为,我需要添加徽章。 我搜索了如何添加徽章计数到按钮但找不到我想要的东西。(我知道如何为menu.xml中的图标执行此操作)。 任何帮助,将不胜感激。 我尝试了什么:

BitmapDrawable d1 = (BitmapDrawable) getResources().getDrawable(R.drawable.ic_launcher);
        Drawable drawableArray[]= new Drawable[]{d1};
        LayerDrawable layerDraw = new LayerDrawable(drawableArray);
        NewMessageIcon.setBadgeCount(this,layerDraw,getActionBar(),3);

setbadgecountfunction:

public static void setBadgeCount(Context context, LayerDrawable icon, ActionBar action,int countunread) {
        BadgeDrawable badge=null;
        Drawable reuse;

        reuse = icon.findDrawableByLayerId(R.id.ic_badge_fornewmessage);



        if (reuse != null && reuse instanceof BadgeDrawable) {
            reuse.invalidateSelf();
            badge = (BadgeDrawable) reuse;
        }

        else {
            badge = new BadgeDrawable(context);
        }
        badge.setCount(countunread);

        if (countunread>0)
        {
            badge.mBadgePaint.setColor(Color.parseColor("#F00000"));
        }
        else
        {
            badge.mBadgePaint.setColor(Color.parseColor("#ffae19"));
        }

        icon.mutate();

        icon.setDrawableByLayerId(R.id.ic_badge_fornewmessage, badge);
        action.setIcon(icon); /////SHOULD CHANGE BUT NOT CHANGING!
    }

徽章课(我在MENU.XML及其工作中使用了这个类别的课程)

public class BadgeDrawable extends Drawable {

    private float mTextSize;
    public Paint mBadgePaint;
    private Paint mTextPaint;
    private Rect mTxtRect = new Rect();

    private String mCount = "";
    private boolean mWillDraw = false;

    public BadgeDrawable(Context context) {
        mTextSize = context.getResources().getDimension(R.dimen.badge_text_size); //GET THE PREDIFINED TEXT SIZE

        //FOR THE CIRCLE
        mBadgePaint = new Paint();
        mBadgePaint.setColor(Color.parseColor("#ffae19")); 
        mBadgePaint.setAntiAlias(true);
        mBadgePaint.setStyle(Paint.Style.FILL);

        //FOR THE NUMBER WITHIN THE CIRCLE
        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    @Override
    public void draw(Canvas canvas) {
        if (!mWillDraw) {
            return;
        }

        CREATING THE CIRCLE SHAPE
        Rect bounds = getBounds();
        //float width = bounds.right - bounds.left;
        //float height = bounds.bottom - bounds.top;
        float width = (bounds.right - bounds.left) + 20;
        float height = (bounds.bottom - bounds.top) + 20;

        // Position the badge in the top-right quadrant of the icon.
        float radius = ((Math.min(width, height) / 2) - 1) / 2;
        float centerX = width - radius - 1;
        float centerY = radius + 1;

        // Draw badge circle.
        canvas.drawCircle(centerX, centerY, radius, mBadgePaint);

        // Draw badge count text inside the circle.
        mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect);
        float textHeight = mTxtRect.bottom - mTxtRect.top;
        float textY = centerY + (textHeight / 2f);
        canvas.drawText(mCount, centerX, textY, mTextPaint);
    }

    /*
    Sets the count (i.e notifications) to display.
     */
    public void setCount(int count) {
        mCount = Integer.toString(count);

        // Only draw a badge if there are notifications.
        mWillDraw = count > 0;
        invalidateSelf();
    }

    @Override
    public void setAlpha(int alpha) {
        // do nothing
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        // do nothing
    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }
}

ic_menu_newmessage.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/ic_newmessage_notification"
        android:drawable="@drawable/ic_launcher"
        android:gravity="center" />

    <item
        android:id="@+id/ic_badge_fornewmessage"
        android:drawable="@drawable/ic_launcher" />
</layer-list>

0 个答案:

没有答案