簡體   English   中英

為什么我的觀點沒有畫出來?

[英]Why aren't my views drawing?

我正在嘗試使用一些動態繪制的視圖制作和Android應用程序。 目前,視圖中唯一繪制的是視圖中間的圓圈。

視圖位於網格視圖中,但似乎沒有正確繪制它們。

這是我加載屏幕時發生的情況:

在此輸入圖像描述

橙色塊是網格視圖中具有焦點的視圖。

但是,如果我使用我的手指(或鼠標)沿視圖拖動,它會被正確繪制:

在此輸入圖像描述

為什么是這樣?

如何讓它一直繪制第二張圖像。

這是我正在使用的代碼:

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


    }

}

我猜問題就出現了,因為在第一次調用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());
    ...
}

而不是使用的canvas.getWidth()canvas.getHeight()嘗試使用this.getWidth()this.getHeight()

叫我瘋了,但是如果你在onDraw()的末尾調用invalidate()會發生什么? 例如,請參閱APIDemos的以下代碼段:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM