2012-02-02 12 views
5

일부 동적으로 그려진보기로 안드로이드 앱을 만들려고합니다. 현재 뷰가 그린 유일한 것은 뷰 중간에있는 원입니다.내 조회수가 왜 그리지 않습니까?

보기는 격자보기 내부에 있지만보기가 옳지 않습니다.

이 나는 ​​화면을로드 할 때 발생하는 것입니다 :

enter image description here

오렌지 블록에 포커스가 격자보기에서보기입니다. 내가보기 함께 끌어 내 손가락 (또는 마우스)를 사용하는 경우

그러나, 제대로 그린 도착 : 왜이 ​​

enter image description here

입니까?

어떻게 두 번째 이미지를 항상 그릴 수 있습니까? onDraw()의 첫 번째 호출에 캔버스가 제로 너비와 높이를 가지고 있기 때문에

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

당신이 호출 할 수있는'notifyDataSetChanged()'당신'TableAdapter'에 당신이'setTables()'라는 한 후 예를 들어, ApiDemos에에서이 코드를 참조하십시오? – Jave

답변

1

을 대신 canvas.getWidth()canvas.getHeight()this.getWidth()this.getHeight()를 사용하려고 사용하는.

1

것 같아요 문제가 발생한다 : 여기

내가 사용하고 코드입니다. 캔버스 출력의 크기에이 방법을 추가 로깅을 그것을 지우려면 :

@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

a.ch. Answer에서 언급 한 바와 같이'ViewView '에서'onMeasure()'를 오버라이드해야한다. 뷰 시스템이 뷰의 차원을 정확하게 측정하지 못한다면. – curioustechizen

+0

올바른 높이와 너비가 표시됩니다. w = 320, h = 480 – Malfist

0

나를 괴롭히다.하지만 onDraw() 끝에서 invalidate()를 호출하면 어떻게됩니까?

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

무효화를 추가해도 아무 효과가 없습니다. – Malfist

관련 문제