最近项目想要做消息提醒功能,从图标提醒到导航栏到里边的模块图标每一级都要有提醒,桌面图标提醒比较容易,详细的模块需要自己定制,这里主要说说主页面的消息提醒如何实现,先看看效果图:

Android 桌面图标显示消息提醒_Group

首先是用TabHost实现主页面,这种方式的好处在于每个页面用的是activity而不是fragment,操作起来更加方便。

主页面xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0.0dip"
            android:layout_weight="1.0" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.0"
            android:visibility="gone" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorGray"/>

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <RadioGroup
                android:id="@+id/main_tab_group"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:background="@color/colorWhite"
                android:gravity="bottom"
                android:orientation="horizontal"
                >

                <RadioButton
                    android:id="@+id/home_tab_home"
                    style="@style/HomeButton"
                    android:layout_weight="1.0"
                    android:checked="true"
                    android:drawableTop="@drawable/home_radiobutton_home"
                    android:text="@string/home"
                    android:layout_gravity="center" />

                <RadioButton
                    android:id="@+id/home_tab_attention"
                    style="@style/HomeButton"
                    android:layout_weight="1.0"
                    android:drawableTop="@drawable/home_radiobutton_attention"
                    android:text="@string/attention"
                    android:layout_gravity="center" />

                <BadgeRadioButton
                    android:id="@+id/home_tab_all"
                    style="@style/HomeButton"
                    android:paddingTop="4dp"
                    android:layout_weight="1.0"
                    android:drawableTop="@drawable/home_radiobutton_all"
                    android:text="@string/all"
                    android:layout_gravity="center" />

                <RadioButton
                    android:id="@+id/home_tab_user"
                    style="@style/HomeButton"
                    android:layout_weight="1.0"
                    android:drawableTop="@drawable/home_radiobutton_person"
                    android:text="@string/user"
                    android:layout_gravity="center" />

            </RadioGroup>
        </FrameLayout>
    </LinearLayout>

</TabHost>

然后是主页面activity

import android.Manifest;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.Toast;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import cn.com.lljk.TrafficStatsUtils;
import cn.com.R;
import cn.com.common.ActivityCollector;
import cn.com.service.InitModuleService;
import cn.com.service.MessageService;
import cn.com.utils.SharedPreferencesUtil;
import cn.com.view.BadgeRadioButton;
import static cn.com.common.MyApplication.app;

/**
 *
 */
public class MainActivity extends TabActivity {

    @ViewInject(R.id.home_tab_home)
    private RadioButton rHome ;
    @ViewInject(R.id.home_tab_attention)
    private RadioButton rAtten ;
    @ViewInject(R.id.home_tab_all)
    private BadgeRadioButton rAll ;
    @ViewInject(R.id.home_tab_user)
    private RadioButton rUser ;
    private TabHost tabHost;
    private LocationManager locationManager;
    private String X = "1";
    private String Y = "2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.finishAll();
        ActivityCollector.addActivity(this);
        setContentView(R.layout.activity_main);
        x.view().inject(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
//            window.setFlags(
//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
//                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        TrafficStatsUtils.trafficMonitorEnd(getBaseContext());
        ActivityCollector.removeActivity(this);
    }

    private void init(){
        tabHost=this.getTabHost();
        TabHost.TabSpec spec;
        Intent intent;

        intent=new Intent().setClass(this, HomeActivity.class);
        spec=tabHost.newTabSpec(getResources().getString(R.string.home)).setIndicator(getResources().getString(R.string.home)).setContent(intent);
        tabHost.addTab(spec);

        intent=new Intent().setClass(this,AttentionActivity.class);
        spec=tabHost.newTabSpec(getResources().getString(R.string.attention)).setIndicator(getResources().getString(R.string.attention)).setContent(intent);
        tabHost.addTab(spec);

        intent=new Intent().setClass(this,AllActivity.class);
        spec=tabHost.newTabSpec(getResources().getString(R.string.all)).setIndicator(getResources().getString(R.string.all)).setContent(intent);
        tabHost.addTab(spec);

        intent=new Intent().setClass(this,PersonActivity.class);
        spec=tabHost.newTabSpec(getResources().getString(R.string.user)).setIndicator(getResources().getString(R.string.user)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

        RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab_group);
        initRadioButton(24);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                switch (checkedId) {
                    case R.id.home_tab_home: {      //主页
                        tabHost.setCurrentTabByTag(getResources().getString(R.string.home));
                        break;
                    }
                    case R.id.home_tab_attention: { //关注
                        tabHost.setCurrentTabByTag(getResources().getString(R.string.attention));
                        break;
                    }
                    case R.id.home_tab_all:{
                        tabHost.setCurrentTabByTag(getResources().getString(R.string.all));
                        break;
                    }
                    case R.id.home_tab_user:{
                        tabHost.setCurrentTabByTag(getResources().getString(R.string.user));
                        break;
                    }
                    default:
                        break;
                }
            }
        });
        Intent intent1 = new Intent(this, MessageService.class) ;
        startService(intent1) ;
        getBadge();
    }

    private void getBadge(){
        rAll.setBadgeNumber(10) ;    //设置显示的消息数
    }

    public void setTabHostPager(int position){
        tabHost.setCurrentTab(position);
    }

    //初始化radio图标大小
    private void initRadioButton(float height){
        Drawable drawable1 = getResources().getDrawable(R.drawable.home_radiobutton_home);
        drawable1.setBounds(0, 0, (int)getRawSize(height), (int)getRawSize(height));//第一0是距左右边距离,第二0是距上下边距离,第三69长度,第四宽度
        rHome.setCompoundDrawables(null, drawable1, null, null);//只放上面
        Drawable drawable2 = getResources().getDrawable(R.drawable.home_radiobutton_attention);
        drawable2.setBounds(0, 0, (int)getRawSize(height), (int)getRawSize(height));
        rAtten.setCompoundDrawables(null, drawable2, null, null);
        Drawable drawable3 = getResources().getDrawable(R.drawable.home_radiobutton_all);
        drawable3.setBounds(0, 0, (int)getRawSize(height), (int)getRawSize(height));
        rAll.setCompoundDrawables(null, drawable3, null, null);
        Drawable drawable4 = getResources().getDrawable(R.drawable.home_radiobutton_person);
        drawable4.setBounds(0, 0, (int)getRawSize(height), (int)getRawSize(height));
        rUser.setCompoundDrawables(null, drawable4, null, null);

    }

    //传入PX值得到dp,dip等
    public float getRawSize(float value) {
        Resources res = this.getResources();
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, res.getDisplayMetrics());
    }

}
BadgeRadioButton
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;


/**
 * <p>带标记的 RadioButton,目前仅支持 drawableTop 的按钮 right的没测试</p><br>
 *
 * 使用时直接 setBadgeNum
 * setBadgeTextSize -- 设置字体颜色
 * setBadgePadding -- 设置内边距
 * setBadgeShowShadow -- 设置是否显示阴影
 * setBadgeColorBackground -- 设置背景颜色
 * setBadgeColorBadgeText -- 设置字体颜色
 * setBadgeExact -- 设置是否截取图标
 * setBadgeOffX -- 设置X的偏移量
 * setBadgeOffY -- 设置Y的偏移量
 * setBadgeText -- 设置文本
 * setBadgeGravity -- 设置标记的位置
 */
public class BadgeRadioButton extends DrawableCenterRadioButton {
    /** 字体高度 */
    private float mFontHeight;
    /** 字体宽度 */
    private float mFontWidth;
    /** 位图集合 */
    private Drawable[] mDrawables;
    /** 位图 */
    private Drawable mDrawableBackground;
    /** 背景画笔 */
    private Paint mBadgeBackgroundPaint;
    /** 数字画笔 */
    private Paint mBadgeTextPaint;
    /** 数字字体大小 默认8sp */
    private float mBadgeTextSize;
    /** 内边距 默认4dp */
    private int mBadgePadding;
    /** x偏移量 */
    private int mBadgeOffX;
    /** Y偏移量 */
    private int mBadgeOffY;
    /** 数字 */
    private int mBadgeNumber;
    /** 数字文本 */
    private String mBadgeText;
    /** 是否存在阴影 默认存在 */
    private boolean mBadgeShowShadow;
    /** 背景颜色 默认红色 */
    private int mBadgeColorBackground;
    /** 字体颜色 默认白色 */
    private int mBadgeColorBadgeText;
    /** 是否截取数字 */
    private boolean mBadgeExact;
    /** 背景矩形 */
    private RectF mBadgeBackgroundRect;
    /** 位置,默认中心 */
    private int mGravity = Gravity.CENTER;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public BadgeRadioButton(Context context) {
        super(context);
        init();
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 属性
     */
    public BadgeRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 属性
     * @param defStyle 样式
     */
    public BadgeRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null != mDrawableBackground && null != mBadgeText) {
            showShadowImpl(mBadgeShowShadow, mBadgeBackgroundPaint);
            float maxPadding = getOffSize();
            if (mBadgePadding > maxPadding) {
                if (mBadgePadding - maxPadding > mBadgeOffY) {
                    mBadgeOffY = (int) (mBadgePadding - maxPadding);
                }
            }
            drawWithDrawable(canvas);
        } else if (null == mDrawableBackground && null != mBadgeText) {
            showShadowImpl(mBadgeShowShadow, mBadgeBackgroundPaint);
            float maxPadding = getOffSize();
            if (mBadgePadding > maxPadding) {
                if (mBadgePadding - maxPadding > mBadgeOffY) {
                    mBadgeOffY = (int) (mBadgePadding - maxPadding);
                }
            }
            drawWithNoDrawable(canvas);
        }
    }

    /**
     * 无图标时候,绘制标签
     *
     * @param canvas 画布
     */
    private void drawWithNoDrawable(Canvas canvas) {
        float textWidth = getPaint().measureText(getText().toString());
        if (mBadgeText.length() == 0) {
            //绘制圆
            if (mGravity == Gravity.CENTER) {
                canvas.drawCircle((getWidth() + textWidth) / 2 + mBadgeOffX, mFontHeight / 2 + mBadgeOffY, mFontHeight / 2 + mBadgePadding, mBadgeBackgroundPaint);
            } else if (mGravity == Gravity.RIGHT) {
                canvas.drawCircle(getWidth() - mFontWidth / 2 - mBadgePadding, mFontHeight / 2 + mBadgeOffY, mFontHeight / 2 + mBadgePadding, mBadgeBackgroundPaint);
            } else if (mGravity == Gravity.LEFT) {
                canvas.drawCircle(mFontWidth / 2 + mBadgePadding, mFontHeight / 2 + mBadgeOffY, mFontHeight / 2 + mBadgePadding, mBadgeBackgroundPaint);
            }
        } else {
            //多个字符,绘制椭圆
            mBadgeBackgroundRect.left = (getWidth() + textWidth) / 2 - mFontWidth / 2 - mBadgePadding + mBadgeOffX;
            mBadgeBackgroundRect.right = (getWidth() + textWidth) / 2 + mFontWidth / 2 + mBadgePadding + mBadgeOffX;
            mBadgeBackgroundRect.top = -mBadgePadding + mBadgeOffY;
            mBadgeBackgroundRect.bottom = mFontHeight + mBadgePadding + mBadgeOffY;
            if (mGravity == Gravity.RIGHT) {
                mBadgeBackgroundRect.left = getWidth() - mFontWidth - mBadgePadding * 2;
                mBadgeBackgroundRect.right = getWidth();
            } else if (mGravity == Gravity.LEFT) {
                mBadgeBackgroundRect.left = 0;
                mBadgeBackgroundRect.right = mFontWidth + mBadgePadding * 2;
            }
            canvas.drawRoundRect(mBadgeBackgroundRect, mFontHeight / 2 + mBadgePadding, mFontHeight / 2 + mBadgePadding, mBadgeBackgroundPaint);
        }
        //绘制文本
        if (mGravity == Gravity.RIGHT) {
            canvas.drawText(mBadgeText, getWidth() - mFontWidth - mBadgePadding, mFontHeight + mBadgeOffY, mBadgeTextPaint);
        } else if (mGravity == Gravity.LEFT) {
            canvas.drawText(mBadgeText, mBadgePadding, mFontHeight + mBadgeOffY, mBadgeTextPaint);
        } else {
            canvas.drawText(mBadgeText, (getWidth() + textWidth) / 2 - mFontWidth / 2 + mBadgeOffX, mFontHeight + mBadgeOffY, mBadgeTextPaint);
        }
    }

    /**
     * 有图标时候,绘制标签
     *
     * @param canvas 画布
     */
    private void drawWithDrawable(Canvas canvas) {
        if (mBadgeText.length() == 0) {
            //绘制圆
            if (mGravity == Gravity.CENTER) {
                canvas.drawCircle((getWidth() + mDrawableBackground.getIntrinsicWidth()) / 2 + mBadgeOffX, mFontHeight / 2 + mBadgeOffY, mFontHeight / 2 + mBadgePadding, mBadgeBackgroundPaint);
            } else if (mGravity == Gravity.RIGHT) {
                canvas.drawCircle(getWidth() - mFontWidth / 2 - mBadgePadding, mFontHeight / 2 + mBadgeOffY, mFontHeight / 2 + mBadgePadding, mBadgeBackgroundPaint);
            } else if (mGravity == Gravity.LEFT) {
                canvas.drawCircle(mFontWidth / 2 + mBadgePadding, mFontHeight / 2 + mBadgeOffY, mFontHeight / 2 + mBadgePadding, mBadgeBackgroundPaint);
            }
        } else {
            //多个字符,绘制椭圆
            mBadgeBackgroundRect.left = (getWidth() + mDrawableBackground.getIntrinsicWidth()) / 2 - mFontWidth / 2 - mBadgePadding + mBadgeOffX;
            mBadgeBackgroundRect.right = (getWidth() + mDrawableBackground.getIntrinsicWidth()) / 2 + mFontWidth / 2 + mBadgePadding + mBadgeOffX;
            mBadgeBackgroundRect.top = -mBadgePadding + mBadgeOffY;
            mBadgeBackgroundRect.bottom = mFontHeight + mBadgePadding + mBadgeOffY;
            if (mGravity == Gravity.RIGHT) {
                mBadgeBackgroundRect.left = getWidth() - mFontWidth - mBadgePadding * 2;
                mBadgeBackgroundRect.right = getWidth();
            } else if (mGravity == Gravity.LEFT) {
                mBadgeBackgroundRect.left = 0;
                mBadgeBackgroundRect.right = mFontWidth + mBadgePadding * 2;
            }
            canvas.drawRoundRect(mBadgeBackgroundRect, mFontHeight / 2 + mBadgePadding, mFontHeight / 2 + mBadgePadding, mBadgeBackgroundPaint);
        }
        //绘制文本
        if (mGravity == Gravity.RIGHT) {
            canvas.drawText(mBadgeText, getWidth() - mFontWidth - mBadgePadding, mFontHeight + mBadgeOffY, mBadgeTextPaint);
        } else if (mGravity == Gravity.LEFT) {
            canvas.drawText(mBadgeText, mBadgePadding, mFontHeight + mBadgeOffY, mBadgeTextPaint);
        } else {
            canvas.drawText(mBadgeText, (getWidth() + mDrawableBackground.getIntrinsicWidth()) / 2 - mFontWidth / 2 + mBadgeOffX, mFontHeight + mBadgeOffY, mBadgeTextPaint);
        }
    }

    /**
     * 设置显示数字
     *
     * @param badgeNumber 标记数字
     */
    public BadgeRadioButton setBadgeNumber(int badgeNumber) {
        mBadgeNumber = badgeNumber;
        if (mBadgeNumber < 0) {
            mBadgeText = null;
        } else if (mBadgeNumber > 99) {
            mBadgeText = mBadgeExact ? String.valueOf(mBadgeNumber) : "99+";
        } else if (mBadgeNumber > 0 && mBadgeNumber <= 99) {
            mBadgeText = String.valueOf(mBadgeNumber);
        } else if (mBadgeNumber == 0) {
            mBadgeText = "";
        }
        return invalidateLayout();
    }

    /**
     * 设置显示数字
     *
     * @param badge 标记文本
     */
    public BadgeRadioButton setBadgeText(String badge) {
        mBadgeText = badge;
        return invalidateLayout();
    }

    /**
     * 重新刷新界面
     */
    private BadgeRadioButton invalidateLayout() {
        if (!TextUtils.isEmpty(mBadgeText)) {
            measureText();
        }
        invalidate();
        return this;
    }

    /**
     * 测量文本高度和宽度
     */
    private void measureText() {
        mFontHeight = Math.abs(mBadgeTextPaint.getFontMetrics().descent + mBadgeTextPaint.getFontMetrics().ascent);
        mFontWidth = mBadgeTextPaint.measureText(mBadgeText);
    }

    /**
     * 为画笔设置阴影
     *
     * @param showShadow 是否显示
     * @param badgeBackgroundPaint 画笔
     */
    private void showShadowImpl(boolean showShadow, Paint badgeBackgroundPaint) {
        int x = dp2px(1);
        int y = dp2px(1.5f);
        badgeBackgroundPaint.setShadowLayer(showShadow ? dp2px(2f) : 0, x, y, 0x33000000);
    }

    @Override
    void init() {
        super.init();
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        mBadgeTextSize = dp2px(10);
        mBadgePadding = dp2px(4);
        mBadgeShowShadow = true;
        mBadgeColorBackground = Color.RED;
        mBadgeColorBadgeText = Color.WHITE;
        mBadgeOffX = 0;
        mBadgeOffY = 0;
        mFontWidth = 0;
        mFontHeight = 0;
        mBadgeBackgroundRect = new RectF(0, 0, 0, 0);

        //目前只支持drawableTop的RadioButton
        mDrawables = getCompoundDrawables();
        if (null != mDrawables[1]) {
            mDrawableBackground = mDrawables[1];
        }
        /* 理论上可以支持drawableRight,但是没测试
       else if (null != mDrawables[2]) {
            mDrawableBackground = mDrawables[2];
        }*/

        mBadgeTextPaint = new TextPaint();
        mBadgeTextPaint.setAntiAlias(true);
        mBadgeTextPaint.setSubpixelText(true);
        mBadgeTextPaint.setFakeBoldText(true);
        mBadgeTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        mBadgeTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
        mBadgeTextPaint.setDither(true);
        mBadgeTextPaint.setColor(mBadgeColorBadgeText);
        mBadgeTextPaint.setTextSize(mBadgeTextSize);

        mBadgeBackgroundPaint = new Paint();
        mBadgeBackgroundPaint.setAntiAlias(true);
        mBadgeBackgroundPaint.setStyle(Paint.Style.FILL);
        mBadgeBackgroundPaint.setDither(true);
        mBadgeBackgroundPaint.setColor(mBadgeColorBackground);
        showShadowImpl(mBadgeShowShadow, mBadgeBackgroundPaint);
    }

    /**
     * 设置字体大小,默认8dp
     *
     * @param badgeTextSize 内边距
     */
    public BadgeRadioButton setBadgeTextSize(float badgeTextSize) {
        mBadgeTextSize = badgeTextSize;
        mBadgeTextPaint.setTextSize(mBadgeTextSize);
        return invalidateLayout();
    }

    /**
     * 设置标记的位置
     * 目前仅支持Gravity.LEFT Gravity.RIGHT Gravity.CENTER,默认Gravity.CENTER
     *
     * @param gravity {@link Gravity}
     */
    public BadgeRadioButton setBadgeGravity(int gravity) {
        mGravity = gravity;
        return invalidateLayout();
    }

    /**
     * 设置内边距,默认4dp
     *
     * @param badgePadding 内边距
     */
    public BadgeRadioButton setBadgePadding(int badgePadding) {
        mBadgePadding = badgePadding;
        return invalidateLayout();
    }

    /**
     * 设置是否显示阴影,默认显示
     *
     * @param badgeShowShadow true - 显示
     */
    public BadgeRadioButton setBadgeShowShadow(boolean badgeShowShadow) {
        mBadgeShowShadow = badgeShowShadow;
        return invalidateLayout();
    }

    /**
     * 设置背景颜色,默认红色
     *
     * @param badgeColorBackground 背景颜色
     */
    public BadgeRadioButton setBadgeColorBackground(@ColorInt int badgeColorBackground) {
        mBadgeColorBackground = badgeColorBackground;
        mBadgeBackgroundPaint.setColor(mBadgeColorBackground);
        return invalidateLayout();
    }

    /**
     * 设置标记字体颜色,默认白色
     *
     * @param badgeColorBadgeText 字体颜色
     */
    public BadgeRadioButton setBadgeColorBadgeText(int badgeColorBadgeText) {
        mBadgeColorBadgeText = badgeColorBadgeText;
        mBadgeTextPaint.setColor(mBadgeColorBadgeText);
        return invalidateLayout();
    }

    /**
     * 设置X偏移量
     *
     * @param badgeOffX x偏移量
     */
    public BadgeRadioButton setBadgeOffX(int badgeOffX) {
        mBadgeOffX = badgeOffX;
        return invalidateLayout();
    }

    /**
     * 设置Y偏移量
     *
     * @param badgeOffY Y偏移量
     */
    public BadgeRadioButton setBadgeOffY(int badgeOffY) {
        mBadgeOffY = badgeOffY;
        return invalidateLayout();
    }

    /**
     * 是否截取字体,默认截取,如 100 -> 99+
     *
     * @param badgeExact true - 截取
     */
    public BadgeRadioButton setBadgeExact(boolean badgeExact) {
        mBadgeExact = badgeExact;
        return invalidateLayout();
    }

    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public int dp2px(float dpValue) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
DrawableCenterRadioButton
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;


public class DrawableCenterRadioButton extends android.support.v7.widget.AppCompatRadioButton {
    /** 字体高度 */
    private float mFontHeight;
    /** 位图集合 */
    private Drawable[] mDrawables;
    /** 字体偏移数据 */
    private float mOffSize;

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public DrawableCenterRadioButton(Context context) {
        super(context);
        init();
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 属性
     */
    public DrawableCenterRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param attrs 属性
     * @param defStyle 样式
     */
    public DrawableCenterRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }


    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        mDrawables = getCompoundDrawables();
        if (mDrawables[0] != null || mDrawables[2] != null) {
            // 左、右
            setGravity(Gravity.CENTER_VERTICAL | (mDrawables[0] != null ? Gravity.LEFT : Gravity.RIGHT));

        } else if (mDrawables[1] != null || mDrawables[3] != null) {
            // 上、下
            setGravity(Gravity.CENTER_HORIZONTAL | (mDrawables[1] != null ? Gravity.TOP : Gravity.BOTTOM));
            Paint.FontMetrics fm = getPaint().getFontMetrics();
            mFontHeight = (float) Math.ceil(fm.descent - fm.ascent);
        }
        super.onLayout(changed, left, top, right, bottom);
    }


    @Override
    protected void onDraw(Canvas canvas) {

        int drawablePadding = getCompoundDrawablePadding();
        if (mDrawables[0] != null) {
            // 左
            float textWidth = getPaint().measureText(getText().toString());
            int drawableWidth = mDrawables[0].getIntrinsicWidth();
            float bodyWidth = textWidth + drawableWidth + drawablePadding;
            if (TextUtils.isEmpty(getText())) {
                bodyWidth = drawableWidth;
            }
            mOffSize = (getWidth() - bodyWidth) / 2;
            canvas.translate(mOffSize, 0);

        } else if (mDrawables[2] != null) {
            // 右
            float textWidth = getPaint().measureText(getText().toString());
            int drawableWidth = mDrawables[2].getIntrinsicWidth();
            float bodyWidth = textWidth + drawableWidth + drawablePadding;
            if (TextUtils.isEmpty(getText())) {
                bodyWidth = drawableWidth;
            }
            mOffSize = (bodyWidth - getWidth()) / 2;
            canvas.translate(mOffSize, 0);
        } else if (mDrawables[1] != null) {
            // 上
            int drawableHeight = mDrawables[1].getIntrinsicHeight();
            float bodyHeight = mFontHeight + drawableHeight + drawablePadding;
            if (TextUtils.isEmpty(getText())) {
                bodyHeight = drawableHeight;
            }
            mOffSize = (getHeight() - bodyHeight) / 2;
            canvas.translate(0, mOffSize);
        } else if (mDrawables[3] != null) {
            // 下
            int drawableHeight = mDrawables[3].getIntrinsicHeight();
            float bodyHeight = mFontHeight + drawableHeight + drawablePadding;
            if (TextUtils.isEmpty(getText())) {
                bodyHeight = drawableHeight;
            }
            mOffSize = (bodyHeight - getHeight()) / 2;
            canvas.translate(0, mOffSize);
        }
        super.onDraw(canvas);
    }

    public float getOffSize() {
        return mOffSize;
    }

    /**
     * 初始化
     */
    void init() {

    }
}