Android Custom Drawable bounds

纵饮孤独 提交于 2019-12-10 20:34:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!