I am creating a simple drawing of a circle using Canvas
and Paint
.
I noticed that when I create the variable myPaint
outside of init()
, everything works perfectly fine; illustrated by the following code:
public class Drawing extends View {
Paint myPaint;
public Drawing(Context context) {
super(context);
init();
}
public void init(){
myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
myPaint.setColor(Color.parseColor("yellow"));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(getMeasuredWidth() /2, getMeasuredHeight() /2 , 100f, myPaint);
super.onDraw(canvas);
}
}
However, when I do the exact same thing, but instead create myPaint
inside of init()
, I get an error for myPaint
in onDraw()
; illustrated by the following code:
public class Drawing extends View {
public Drawing(Context context) {
super(context);
init();
}
public void init(){
Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
myPaint.setColor(Color.parseColor("yellow"));
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(getMeasuredWidth() /2, getMeasuredHeight() /2 , 100f, myPaint);
super.onDraw(canvas);
}
}
Why is this so? Thanks!