2012-02-02 18 views
5

Sto provando a creare app Android con alcune viste disegnate dinamicamente. Attualmente, l'unica cosa che le viste disegnano è un cerchio nel mezzo della vista.Perché le mie viste non vengono disegnate?

Le viste si trovano all'interno di una vista a griglia, ma sembra che non le stiano disegnando correttamente.

Questo è ciò che accade quando carico video:

enter image description here

Il blocco arancione è la vista nella vista griglia lo stato attivo.

Tuttavia, se uso il mio dito (o il mouse) per trascinare la vista, esso viene dipinto in modo corretto:

enter image description here

Perché è questo?

Come posso farlo disegnare la seconda immagine tutto il tempo.

Ecco il codice che sto utilizzando:

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

Puoi provare a chiamare 'notifyDataSetChanged()' 'sul TableAdapter' dopo aver chiamato' setTables() '? – Jave

risposta

1

Invece di usare canvas.getWidth() e canvas.getHeight() provare a utilizzare this.getWidth() e this.getHeight().

1

Credo che il problema deriva dal fatto che alla prima chiamata del onDraw() della tela è la larghezza e l'altezza pari a zero. Per cancellare la registrazione fuori metti in questo metodo con le dimensioni di uscita della tela di canapa:

@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

Oltre a ciò a.ch. menzionato nella risposta, dovresti anche eseguire l'override di 'onMeasure()' nel tuo 'TableView' - senza il quale il sistema View non avrà una misurazione accurata delle dimensioni della tua vista. – curioustechizen

+0

Sta ottenendo l'altezza e la larghezza corrette: w = 320, h = 480 – Malfist

0

Chiamami pazzo, ma cosa succede se chiami invalidate() alla fine di onDraw()? Ad esempio, vedere questo frammento di codice da APIDemos:

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

L'aggiunta di un invalidato non ha avuto alcun effetto. – Malfist

Problemi correlati