0

View グループから拡張された View を使用していますが、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 に答える