¿Cómo almacenar lienzo en el método onDraw en Android?

I am trying to save and restore canvas in onDraw method in android but it is not work properly using this code I found that only last time line is shown not previously drown so please tell me.my code is below.

public void setMData(Float pData, Float cData, int i){


    this.pData = pData;
    this.cData = cData;
    this.i = i;

}

public void onDraw(Canvas canvas)

 {


    super.onDraw(canvas);
    canvas.restore();
    hs = getHeight();
    ws = getWidth();
    paint.setTextSize(15);
    paint.setColor(Color.parseColor("#00FFCC"));
    paint.setStrokeWidth(2);
    ox = 30;
    oy = hs - 30;
    canvas.drawLine(30, 30, 30, hs - 30, paint); // Vertical Line
    canvas.drawLine(30, hs - 30, ws - 30, hs - 30, paint); // Horizontal
    canvas.drawText("(0,0)", 10, hs - 10, paint);
    float secWidth = 20;// (ws-30)/mData.size();
    float pRatio = oy / 100 * pData;
    float cRatio = oy / 100 * cData;
    canvas.drawLine(secWidth * i, hs - pRatio, secWidth * (i + 1), hs - cRatio,paint);
    canvas.drawText("" + cData, secWidth * (i + 1), hs - cRatio, paint);
    canvas.save();
}

preguntado el 08 de noviembre de 11 a las 11:11

is 'canvas' a local variable or a field ? -

1 Respuestas

prueba esto :

int saveCount; //declare this as a field

saveCount = canvas.save();
.....
canvas.restoreToCount(saveCount);

respondido 08 nov., 11:15

where have you declared your 'canvas' ? I mean is it a field or a local variable? also try invalidate()/postInvalidate(); - devanshu_kaushik

sorry but it is solve without canvas.save() and canvas.restore() thanks lots for help - urveshpatel50

maybe you could share your solution with us? - Marqs

I am first make arraylist with float and store data and make this using invalidate() - urveshpatel50

no problem, but it is strange that these methods are not working, forcing you to a method which consumes additional memory. Report your case on the android developer's blog :) - devanshu_kaushik

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.