在新位置绘制矩形多次不起作用

时间:2012-08-09 19:13:14

标签: android dynamic android-asynctask draw shapes

每次我用这个代码创建一个新的矩形它不起作用,我只能绘制到指定的位置,如果我使用一个变量来改变执行时的位置,它不会绘制任何东西。

在Asynctask方法中:

rect = new desenho(main.this, x, y);

Wich称之为:

public class desenho extends View{

    int x, y;
    Paint mPaint = new Paint();

    public desenho(Context context, int x, int y) {
        super(context);
        this.x = x;
        this.y = y;
        mPaint.setStrokeWidth(3);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLACK);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(width, y);
    }

    @Override
    protected void onDraw(Canvas c) {
        // TODO Auto-generated method stub
        super.onDraw(c);
        c.drawRect(5, y, width-5, y+x, mPaint);
    }
}

1 个答案:

答案 0 :(得分:0)

在我看来,你希望尺寸与位置无关。为此,必须在Canvas.drawRect(左,上,右,底部,绘画)中满足这些要求:

  • left - right = a
  • top - bottom = b

其中a,b是常数。例如:

c.drawRect(xPos, yPos, xPos + width - 1, yPos + height - 1, mPaint);

您在此示例中看到

  • left - right = xPos - (xPos + width - 1)= 1 - width
  • top - bottom = yPos - (yPos + height - 1)= 1 - height

两者都是常数→大小是恒定的。