2012-02-02 33 views
5

Tôi đang cố gắng tạo và ứng dụng android với một số chế độ xem được vẽ động. Hiện tại, điều duy nhất thu hút lượt xem là một vòng tròn ở giữa chế độ xem.Tại sao chế độ xem của tôi không được vẽ?

Chế độ xem nằm trong chế độ xem lưới, nhưng dường như không thể vẽ chúng ngay.

Đây là những gì xảy ra khi tôi tải các màn hình:

enter image description here

Khối cam là quan điểm trong giao diện lưới có trọng tâm.

Tuy nhiên, nếu tôi sử dụng ngón tay của tôi (hoặc chuột) để kéo dọc theo xem, nó được vẽ một cách chính xác:

enter image description here

Tại sao điều này?

Làm cách nào để tôi có thể vẽ hình ảnh thứ hai mọi lúc.

Đây là mã Tôi đang sử dụng:

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


    } 

} 
+0

Bạn có thể thử gọi 'notifyDataSetChanged()' trên 'TableAdapter' sau khi bạn đã gọi' setTables() 'không? – Jave

Trả lời

1

Thay vì sử dụng canvas.getWidth()canvas.getHeight() hãy thử sử dụng this.getWidth()this.getHeight().

1

Tôi đoán các vấn đề phát sinh bởi vì ở cuộc gọi đầu tiên của onDraw() Canvas có zero width và height. Để xóa nó ra add khai thác gỗ để phương pháp này với kích thước của đầu ra 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()); 
    ... 
} 
+0

Ngoài a.ch. được đề cập trong câu trả lời, bạn cũng nên ghi đè 'onMeasure()' trong 'TableView' của bạn - mà không có hệ thống Xem nào sẽ không có phép đo chính xác kích thước của khung nhìn của bạn. – curioustechizen

+0

Nó nhận được chiều cao và chiều rộng chính xác: w = 320, h = 480 – Malfist

0

Gọi cho tôi là điên, nhưng điều gì sẽ xảy ra nếu bạn gọi invalidate() ở cuối onDraw()? Ví dụ: xem đoạn mã này từ APIDemos:

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

Việc thêm một hiệu ứng vô hiệu không có hiệu lực. – Malfist

Các vấn đề liên quan