android - 如何在应用程序中添加通知徽章?

标签 android button

我想在应用程序中添加一个通知徽章,如下所示:

buttons

如何实现徽章。请指导。

最佳答案

如果你想给图标添加徽章

BadgeDrawable is a custom drawable

package com.wisilica.cms.utitlty.viewHelper;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;

import com.wisilica.cms.R;

/**
 * Created by Godwin Joseph on 02-11-2016 11:49 for Cms_Android application.
 */


public class BadgeDrawable extends Drawable {

private Paint mBadgePaint;
private Paint mBadgePaint1;
private Paint mTextPaint;
private Rect mTxtRect = new Rect();

private String mCount = "";
private boolean mWillDraw;

public BadgeDrawable(Context context) {
    float mTextSize = context.getResources().getDimension(R.dimen.badge_text_size);

    mBadgePaint = new Paint();
    mBadgePaint.setColor(Color.RED);
    mBadgePaint.setAntiAlias(true);
    mBadgePaint.setStyle(Paint.Style.FILL);
    mBadgePaint1 = new Paint();
    mBadgePaint1.setColor(ContextCompat.getColor(context.getApplicationContext(), R.color.white));
    mBadgePaint1.setAntiAlias(true);
    mBadgePaint1.setStyle(Paint.Style.FILL);

    mTextPaint = new Paint();
    mTextPaint.setColor(Color.WHITE);
    mTextPaint.setTypeface(Typeface.DEFAULT);
    mTextPaint.setTextSize(mTextSize);
    mTextPaint.setAntiAlias(true);
    mTextPaint.setTextAlign(Paint.Align.CENTER);
}

@Override
public void draw(Canvas canvas) {


    if (!mWillDraw) {
        return;
    }
    Rect bounds = getBounds();
    float width = bounds.right - bounds.left;
    float height = bounds.bottom - bounds.top;

    // Position the badge in the top-right quadrant of the icon.

        /*Using Math.max rather than Math.min */

    float radius = ((Math.max(width, height) / 2)) / 2;
    float centerX = (width - radius - 1) + 5;
    float centerY = radius - 5;
    if (mCount.length() <= 2) {
        // Draw badge circle.
        canvas.drawCircle(centerX, centerY, (int) (radius + 7.5), mBadgePaint1);
        canvas.drawCircle(centerX, centerY, (int) (radius + 5.5), mBadgePaint);
    } else {
        canvas.drawCircle(centerX, centerY, (int) (radius + 8.5), mBadgePaint1);
        canvas.drawCircle(centerX, centerY, (int) (radius + 6.5), mBadgePaint);
//              canvas.drawRoundRect(radius, radius, radius, radius, 10, 10, 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);
    if (mCount.length() > 2)
        canvas.drawText("99+", centerX, textY, mTextPaint);
    else
        canvas.drawText(mCount, centerX, textY, mTextPaint);
}

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

    // Only draw a badge if there are notifications.
    mWillDraw = !count.equalsIgnoreCase("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;
}
}

并添加代码 { BadgeDrawable 徽章;

    // Reuse drawable if possible
    Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
    if (reuse != null && reuse instanceof BadgeDrawable) {
        badge = (BadgeDrawable) reuse;
    } else {
        badge = new BadgeDrawable(mContext);
    }
    badge.setCount(count);
    icon.mutate();
    icon.setDrawableByLayerId(R.id.ic_badge, badge);

 }

您可以在任何 View 中设置 Drwabale

关于android - 如何在应用程序中添加通知徽章?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163888/

相关文章:

android中蓝色的HTML按钮

swift - 让按钮图像贴在左侧 swift xcode

javascript - 按钮需要双击才能显示图像?

java - 如何在ScrollView的底部添加字符串

android - RecyclerView : Inconsistency detected. 无效的项目位置

android - Media.setLooping无法正常使用[Android]

java - 如何在 Realm Android [4.2.0] 中实例化 RealmQuery 对象

android - 在没有自定义 View 的情况下将 editText 放在 AlertDialog 中?

javascript - Phonegap 文件传输插件导致 Android 构建失败

android - 方法setImageDrawable/setBackgroundDrawable图像大小问题