2012-10-05 27 views
0

我正在使用從View組擴展的View,並且當我調用invalidate時onDraw沒有被調用。請任何人解釋? 的代碼下面給出當調用invalidate時OnDraw()沒有被調用

public class BarGraph extends View {  
private int viewHeight; 
private int viewWidth;       // height and width of bar graph dynamically calculated. 
private int mGraphColor;      // the colour of the bar graph. 

private ArrayList<Integer> mTodays;    // The array list that handles the input to draw the graph. 
private final int barCount = 20;    // Number of bars in the bar graph...here set as 20. 

/* 
* The maximum action value a bar can take. 
* This is calculated based on the action array 
* passed to the chart. 
*/ 

private int yMax = 0; 

private Paint graphColor; 

public BarGraph(Context context, int graphColor) { 
    super(context); 
    this.setWillNotDraw(false); 
    mGraphColor  = graphColor; 
    setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    initializePaintObjects(); 
} 

public BarGraph(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.setWillNotDraw(false); 
    initializePaintObjects(); 
} 

private void initializePaintObjects(){ 
    graphColor = new Paint(); 
    graphColor.setColor(mGraphColor);  
    graphColor.setStyle(Style.FILL); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    if(mTodays == null)      // If the array list is empty. 
     return; 
    if(yMax <= 0)      
     return; 
    int left = 0; 

    viewHeight = getHeight(); 
    viewWidth = getWidth(); 

    if((viewWidth % barCount) > 0){  //adjusting the view width so that bars correctly fits in 
     int newWidth = (int) (Math.floor(viewWidth/barCount) * barCount); 
     left   = (int) Math.floor(((viewWidth - newWidth)/2)); 
     viewWidth  = (int) Math.floor((viewWidth/barCount) * barCount); 
    } 

    int columnWidth  = 2; 
    columnWidth   = (int) Math.floor(viewWidth/barCount); 

    int xFactor   = 1;  
    xFactor    = (int) Math.ceil(columnWidth * 0.33); 

    int barWidth  = 1; 
    barWidth   = columnWidth - xFactor; 

    graphColor.setStrokeWidth(barWidth); 

    int j = 0; 

    for(int i = 0; i < mTodays.size() ; i++){ 
     int todayValue  = mTodays.get(i); 
     float todaysHeight; 
     if(todayValue == 0){ 
      todaysHeight = (float) (viewHeight-viewHeight*(.001)); 
     }else{ 
      todaysHeight = getYValue(todayValue); 
     } 

     canvas.drawLine(((j*columnWidth)+xFactor + left) , viewHeight, ((j*columnWidth)+xFactor + left), todaysHeight, graphColor); 
     j++; 
    } 

} 

public void setData(ArrayList<Integer>todays){ 

    mTodays   = todays; 

    yMax   = 0; 
    for (int val : mTodays){ 
     yMax  = yMax > val ? yMax : val; 
    } 
    invalidate(); 
} 

private int getYValue(int item){ 
    int percent = (item * 100)/yMax; 
    return (viewHeight) - ((percent * viewHeight)/100); 
} 

}

+0

檢查此鏈接http://stackoverflow.com/questions/11720199/invalidate- dont-recall-ondraw-method – nik431

+0

@ nik431 ...我已經閱讀過...(Addtnl info-我沒有得到一個線索究竟該做什麼..我沒有使用通過XML代碼的視圖...我需要當我調用函數setData()時調用onDraw)。 –

+0

代碼看起來沒問題。你是如何確認onDraw沒有被調用的? – Sameer

回答

0

感謝您的所有attempts.The問題的主要活動實際上是。 下面是正確的代碼

final Handler handler = new Handler(); 
       Timer timer2 = new Timer(); 
       TimerTask testing = new TimerTask() { 
        public void run() { 
         handler.post(new Runnable() { 
          public void run() { 
           for(int j=0;j<19;j++){ 
            todays.set(j, todays.get(j+1)); 
           } 
           Random diceRoller = new Random(); 
           todays.set(19, diceRoller.nextInt(100)*10+1); 
           MainActivity.this.runOnUiThread(new Runnable() { 

            @Override 
            public void run() { 
             // TODO Auto-generated method stub 
             bargraph.setData(todays);  
            } 
           }); 
          } 

         }); 

我已經不是第一次寫的runOnUiThread ...我的壞...

相關問題