0

I generate an ellipse and I try to reshape with respect to sweepAngle_speed that u can see below. This sweepAngle_speed come from MainActivity.java. In MainActivity.java, I create a seekbar and I use an algorithm between value of seekbar and sweepAngle_speed, therefore I expected a change in filled area in my ellipse. onDraw function is not called directly, so I use invalidate function in my getLog function which is created by me. However I cannot call onDraw function anyway. When I run the code, onDraw function is called directly by system 3 times, however when I change seekbar value, I do not call onDraw function anyway. My first question is that How onDraw function is called directly by system ? The second one is how I can call onDraw function during system is working. Thanks.

CustomView.java

public CustomView(Context context, @Nullable AttributeSet attrs) {
    super(context);
    m_Context = context;
    getLog();
    // create the Paint and set its color
}``


@Override
protected void onDraw(Canvas canvas) {
    //c=canvas;
    //super.onDraw(c);
    Paint p1 = new Paint();
    RectF rectF = new RectF(-750, 0, 750, 720);
    //p1.setColor(Color.parseColor("#34ebe2"));
    p1.setShader(new LinearGradient(0, -360, 0, getHeight(), Color.CYAN, Color.BLUE, Shader.TileMode.MIRROR));
    Log.d(TAG, "CANVAS: onDraw içine girdi ve Speed angle: " + sweepAngle_speed);
    canvas.drawArc(rectF, 90, -sweepAngle_speed, true, p1);
}


public void getLog () {
    paint = new Paint();
    paint.setColor(Color.BLUE);
    Log.d(TAG, "Speed geldi buraya ve invalidate yaptı");
    setWillNotDraw(false);
    //this.invalidate();
    this.invalidate();
}

}

utkuf
  • 1
  • 1
  • Do you have a repaint function? – Lajos Arpad Nov 11 '19 at 13:54
  • Actually I change sweepAngle_speed. It is defined top of code, I did not put it here. Also in log Log.d(TAG, "CANVAS: onDraw içine girdi ve Speed angle: " + sweepAngle_speed); I could see the change. However this change does not effect the angle, I mean I cannot call the onDraw function – utkuf Nov 11 '19 at 13:59
  • Instead of repaint function, I use invalidate but it is not working – utkuf Nov 11 '19 at 13:59

2 Answers2

0

You can't call onDraw directly. You can use the invalidate method which will redraw it

You can make a function inside CustomView class e.g:

public void setSpeed(int sweepAngle_speed){
    this. sweepAngle_speed = sweepAngle_speed;
    invalidate();  // This invalidate will call onDraw and draw your view again
}
Hamza Khan
  • 1,433
  • 13
  • 19
  • I try it, still I could not enter onDraw function and my view is not drawn again. – utkuf Nov 11 '19 at 14:14
  • In setSpeed, I create Canvas cnvs = new Canvas(); and onDraw(cnvs), and I entered onDraw function. onDraw function get correct sweepAngle_speed, however still it cannor draw new ellipse. WHY? – utkuf Nov 11 '19 at 14:25
  • You don't have to create new Canvas and call onDraw function. invalidate() will recall your onDraw function which is already providing canvas in parameter. – Hamza Khan Nov 11 '19 at 15:26
0

If you extend ViewGroup so you need to call setWillNotDraw(false) in the constructor of your ViewGroup