我想在现有的可绘制对象上绘制一个数字。就像电子邮件图标上的未读计数一样。可绘制对象是按钮的顶部图标。这是我的代码:
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);
}
生成的可绘制对象是空的。这段代码有问题吗?
提前致谢。
最佳答案
新位图必须调用 setBounds(...)
BitmapDrawable dn = new BitmapDrawable(b);
dn.setBounds(d.getBounds());
button.setCompoundDrawables(null, dn, null, null);
关于android - 在 Drawable 上绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6264543/