0

我正在使用从视图组扩展的视图,当我调用 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);
}

}

4

1 回答 1

0

感谢您的所有尝试。问题实际上出在主要活动中。以下是正确的代码

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 ......我的坏......

于 2012-10-05T05:23:37.497 回答