Android Canvas绘图不一致

时间:2012-10-29 05:37:49

标签: android imageview android-canvas

我有一个ImageView子类,它应该在显示图像的下角显示一个徽标。我有这个onDraw代码在图像顶部绘制图标。它在ICS +上工作正常,但没有更低。有谁知道这个的原因?

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int canvasHeight = canvas.getHeight();
    int canvasWidth = canvas.getWidth();

    Drawable icon = getResources().getDrawable(R.id.icon);

    int width = icon.getIntrinsicWidth();
    int height = iconcon.getIntrinsicHeight();

    int x = canvasWidth - width - PADDING;
    int y = canvasHeight - height - PADDING;

    icon.setBounds(x, y, canvasWidth - PADDING, canvasHeight - PADDING);
    icon.draw(canvas);
}

1 个答案:

答案 0 :(得分:1)

您可能希望忽略canvas.getWidth()canvas.getHeight()并使用onSizeChanged(int w, int h, int oldw, int oldh)中收到的值,因为有时它们不匹配,而canvas.getWidth()/getHeight()会产生奇怪的结果。< / p>