onDraw() method doesnt work right

141 views Asked by At

So, iv created class that extends view. Here is my code:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (!mTablesList.isEmpty()) {
        drawTables(canvas);
    }

}

 private void drawTables(Canvas canvas) {
    for (Table table : mTablesList) {

            canvas.drawRect(mWidht * table.getLeft() / 100, mHeight * table.getTop() / 100, mWidht * table.getRight() / 100, mHeight * table.getBottom() / 100, m1Paint);
            saveInfo(table, mWidht * table.getLeft() / 100, mHeight * table.getTop() / 100, mWidht * table.getRight() / 100, mHeight * table.getBottom() / 100);

    }
}

private void saveInfo(Table table, int left, int top, int right, int bottom) {

    Rect rect = new Rect();
    rect.set(left, top, right, bottom);


    table.setRect(rect);
    mTablesListTouch.add(table);
}

Now, I have 2 objects that are needed to be drawn. The first object always gets drawn, but the second one doesn't. When my 2nd objects coordinates are left and top == left and top, it doesnt show, when the object is drawn below object 1 that is drawn, it shows. Can somebody give me a solution?

private void makeTables() {

    Table table = new Table();

    table.setLeft(20);
    table.setTop(7);
    table.setRight(40);
    table.setBottom(20);
    table.setAvailable(true);
    table.setID(1);
    listOfTables.add(table);

    Table table1 = new Table();

    table1.setLeft(20);
    table1.setTop(70);
    table1.setRight(60);
    table1.setBottom(20);
    table1.setAvailable(true);
    table1.setID(2);
    listOfTables.add(table1);



    mTables.setTables(listOfTables);
}

settin mTables.

ok, so, this is setTables() method:

 public void setTables(List<Table> tables) {
    mTablesList = tables;
    mPaint.setColor(getResources().getColor(android.R.color.black));
    invalidate();
}

after it it callse onMeasure() and onDraw()

 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    final int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);


    mWidht = width;
    mHeight = height;

    setMeasuredDimension(mWidht, mHeight);

}
1

There are 1 answers

0
Vulovic Vukasin On BEST ANSWER

You are not setting the coordinates right. table.setLeft(20) is the value from left margin to the left side of the object, table.setRight(30) is from left margin to the right side of the object. So, allways table.setRight> table.SetLeft same goes for top and bottom.