问题
I'm write a custom Drawable, a text over a bitmap:
@Override
public void draw(Canvas canvas) {
canvas.drawBitmap(badge, matrix, null);
canvas.drawText(count, size / 2, size / 2 + (text_size) / 2 , p);
}
When I use my drawable in an imageView, it's fine.

But if I use this with a textview (compound drawable) it's alignement is wrong.

I tried both setCompoundDrawablesWithIntrinsicBounds and setCompoundDrawables
Same results, what did I miss ?
Thanks.
回答1:
I manage to get the right alignment (my drawable is a circle/square so width == height):
In my Custom_Drawable ctor:
Custom_Drawable(int size) {
this.size = size;
}
onDraw method:
@Override
public void draw(Canvas canvas) {
// draw here
setBounds(0, 0, this.size, this.size);
}
When setting the drawable :
b.setCompoundDrawables(new Custom_Drawable(25), null, null, null);
来源:https://stackoverflow.com/questions/5855194/android-custom-drawable-bounds