Android 提款

Android 提款,android,drawable,Android,Drawable,我想在现有的可绘制图形上绘制一个数字。就像电子邮件图标上的未读计数。drawable是按钮的顶部图标。这是我的代码: BitmapDrawable d = (BitmapDrawable) button.getCompoundDrawables()[1]; if(d != null) { Bitmap src = d.getBitmap(); Bitmap b = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.g

我想在现有的可绘制图形上绘制一个数字。就像电子邮件图标上的未读计数。drawable是按钮的顶部图标。这是我的代码:

BitmapDrawable d = (BitmapDrawable) button.getCompoundDrawables()[1];
if(d != null) {
    Bitmap src = d.getBitmap();
    Bitmap b = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    Canvas c = new Canvas(b);

    Paint p = new Paint();
    p.setAntiAlias(true);
    p.setStrokeWidth(1);
    p.setStyle(Style.FILL_AND_STROKE);
    p.setColor(Color.rgb(254, 0, 1));

    c.drawBitmap(src, 0, 0, p);
    c.drawCircle(b.getWidth()-5, 5, 5, p);
    button.setCompoundDrawables(null, new BitmapDrawable(b), null, null);
}
由此产生的可拉伸性为emtpy。这个代码有什么问题吗


提前感谢。

我不确定您的实现,但另一种方法是将位图作为imageview添加到framelayout,然后添加带有偏移量的textview(适当样式);取决于您想要徽章的位置,例如右上角。

我不确定您的实现,但另一种方法是将位图作为图像视图添加到框架布局中,然后添加带有偏移的文本视图(适当样式);取决于您想要徽章的位置,例如右上角。

新位图必须调用setBounds(…)


新位图必须调用setBounds(…)

BitmapDrawable dn = new BitmapDrawable(b);
dn.setBounds(d.getBounds());
button.setCompoundDrawables(null, dn, null, null);