为什么我的观点没有画出来?

时间:2012-02-02 03:31:52

标签: java android layout

我正在尝试使用一些动态绘制的视图制作Android应用程序。目前,视图中唯一绘制的是视图中间的圆圈。

视图位于网格视图中,但似乎没有正确绘制它们。

加载屏幕时会发生这种情况:

enter image description here

橙色块是网格视图中具有焦点的视图。

但是,如果我用手指(或鼠标)沿着视图拖动,它会被正确绘制:

enter image description here

为什么会这样?

如何让它始终绘制第二张图像。

以下是我正在使用的代码:

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


    }

}

3 个答案:

答案 0 :(得分:1)

我猜问题就出现了,因为在onDraw()的第一次调用时,Canvas的宽度和高度为零。要清除它,请使用Canvas输出的大小添加日志记录到此方法:

@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());
    ...
}

答案 1 :(得分:1)

而不是使用canvas.getWidth()canvas.getHeight()尝试使用this.getWidth()this.getHeight()

答案 2 :(得分:0)

叫我疯了,但是如果你在onDraw()的末尾调用invalidate()会怎么样?例如,请参阅APIDemos的以下代码段:

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