Malfist
Malfist

Reputation: 31815

Why aren't my views drawing?

I'm trying to make and android app with some dynamically drawn views. Currently, the only thing the views draw is a circle in the middle of the view.

The views are inside a grid view, but it doesn't seem to be drawing them right.

This is what happens when I load the screen:

enter image description here

The orange block is the view in the grid view that has the focus.

However, if I use my finger (or mouse) to drag along the view, it gets painted correctly:

enter image description here

Why is this?

How can I make it draw the second image all the time.

Here is the code I'm using:

public class ChooseTablePanel extends GamePanel {
    TableAdapter adapter;

    public ChooseTablePanel(Context context, GamePanel nextPanel,
            GamePanel failurePanel) {
        super(context, nextPanel, failurePanel);
        initialize();
    }

    public ChooseTablePanel(Context context, AttributeSet attrs,
            GamePanel nextPanel, GamePanel failurePanel) {
        super(context, attrs, nextPanel, failurePanel);
        initialize();
    }

    private void initialize() {     
        adapter = new TableAdapter(getContext());
        adapter.setTables(new int[] {5,4,3,2,1,6});

        GridView gridView = new GridView(getContext());
        gridView.setAdapter(adapter);
        gridView.setNumColumns(adapter.getCount()/3);

        this.addView(gridView);
        this.invalidate();
    }


    class TableAdapter extends BaseAdapter {
        private Context context;
        private TableView[] tables;

        public TableAdapter(Context context) {
            this.context = context;
        }

        public void setTables(int[] tables) {
            this.tables = new TableView[tables.length];

            for(int i = 0; i < tables.length; i++){
                this.tables[i] = new TableView(tables[i], context);
            }
        }

        public int getCount() {
            return tables.length;
        }

        public Object getItem(int position) {
            return tables[position];
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            TableView tableView;
            if (convertView == null) {  // if it's not recycled, initialize some attributes
                tableView = new TableView(1, this.context);
                tableView.setLayoutParams(new GridView.LayoutParams(85, 85));
                tableView.setPadding(8, 8, 8, 8);
            } else {
                tableView = (TableView) convertView;
            }

            tableView.invalidate();
            return tableView;
        }
    }

    class TableView extends View {
        private int seats;

        public TableView(int Seats, Context context) {
            super(context);
            this.seats = Seats;

            if(seats < 1){
                throw new IllegalArgumentException("Number of seats must be greater than one.");
            }
        }

        public int getSeats() {
            return seats;
        }

        @Override
        protected void onDraw(Canvas canvas){
            int tableWidth = canvas.getWidth() / 2;
            ShapeDrawable tableShape = new ShapeDrawable(new OvalShape());          
            tableShape.setBounds(canvas.getWidth()/2 - tableWidth/2, canvas.getHeight()/2 - tableWidth/2, canvas.getWidth()/2 + tableWidth/2, canvas.getHeight()/2 + tableWidth/2 );

            tableShape.draw(canvas);
            canvas.drawText(seats + "", 0, 0, new Paint());
        }


    }

}

Upvotes: 5

Views: 335

Answers (3)

Sparky
Sparky

Reputation: 8477

Call me crazy, but what happens if you call invalidate() at the end of onDraw()? For example, see this code snippet from APIDemos:

    @Override protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        mDrawable.draw(canvas);
        invalidate();
    }

Upvotes: 0

silly
silly

Reputation: 7887

Instead of using canvas.getWidth() and canvas.getHeight() try using this.getWidth() and this.getHeight().

Upvotes: 1

a.ch.
a.ch.

Reputation: 8390

I guess the problem arises because at the first call of onDraw() the Canvas has zero width and height. To clear it out add logging to this method with the size of Canvas output:

@Override
protected void onDraw(Canvas canvas) {
    int tableWidth = canvas.getWidth() / 2;
    Log.i("onDraw", "w=" + canvas.getWidth() + ", h=" + canvas.getHeight());
    ShapeDrawable tableShape = new ShapeDrawable(new OvalShape());
    ...
}

Upvotes: 1

Related Questions