2011-06-07 57 views
1

我想在现有的drawable上绘制一个数字。就像电子邮件图标上的未读数。 drawable是按钮的顶部图标。这是我的代码:Drawable 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); 
} 

生成的drawable是emtpy。这段代码有什么问题吗?

在此先感谢。

回答

1

新的位图必须调用的setBounds(...)

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

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

相关问题