2010-12-23 86 views
2

在下面的片段中onDraw方法被調用大約10-15次。任何人都可以解釋這種行爲嗎?Android onDraw調用大量次

LinearLayout ll = new LinearLayout(this); 
     View v = new View(this) { 
      @Override 
      protected void onDraw(Canvas canvas) { 
       // TODO Auto-generated method stub 
       System.out.println("large view on draw called"); 
       super.onDraw(canvas); 
      } 
     }; 
     v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2000)); 
     LinearLayout ll2 = new LinearLayout(this); 
     ll2.addView(v); 
     ScrollView sv = new ScrollView(this); 
     sv.addView(ll2); 
     ll.addView(sv); 
     LinearLayout ll1 = new LinearLayout(this); 
     ll1.addView(ll); 

回答

3

每次你做一些可能影響小部件的事情時,小部件都會被重繪。將小部件放在容器中可能會影響它。將容器放在另一個容器中可能會影響它。

你應該不會假設onDraw()會被調用多少次,除了它會被調用很多,因此需要很快。

+0

其實如果我不按照上面的層次結構,那麼最終的視圖似乎並沒有提供一個可滾動的視圖。除了第一次渲染視圖之外,還會發生10-15次onDraw調用。有什麼辦法來優化上述佈局。 – pankajagarwal 2010-12-24 04:53:54