Android的自定义绘制对象范围

Android的自定义绘制对象范围

问题描述:

我写一个自定义绘制对象,在一个位图文字:

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);
}

当我用我的绘制在ImageView的,它的罚款。

When I use my drawable in an imageView, it's fine.

但是,如果我用这个有一个TextView(化合物绘)这是alignement是错误的。

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

我都尝试setCompoundDrawablesWithIntrinsicBounds和setCompoundDrawables

I tried both setCompoundDrawablesWithIntrinsicBounds and setCompoundDrawables

同样的结果,我错过了什么?

Same results, what did I miss ?

感谢。

我设法得到右对齐(我是绘制一个圆/平方米这么宽==高):

I manage to get the right alignment (my drawable is a circle/square so width == height):

在我Custom_Drawable构造函数:

In my Custom_Drawable ctor:

Custom_Drawable(int size) {
    this.size = size;
}

的onDraw方法:

onDraw method:

@Override
public void draw(Canvas canvas) {
    // draw here
    setBounds(0, 0, this.size, this.size);
}

在设置绘制:

 b.setCompoundDrawables(new Custom_Drawable(25), null, null, null);