I know what it is the Calling the methods in java and before the question, I read about Java grammar very much.I know what it is the this in java, but I do not know, How is invoked the following code in MainActivity. java
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLUE);
canvas.drawCircle(120,120,40,paint);
}
Now. I put the full code:
MainActivity.java
public class MainActivity extends Activity {
Draw draw;
Cal cal;
TextView textView;
RelativeLayout linearLayout;
public void onCreate(Bundle s) {
super.onCreate(s);
setContentView(R.layout.activity_main);
linearLayout = (RelativeLayout) findViewById(R.id.t);
cal = new Cal(this);
cal.cal();
textView = new TextView(getApplicationContext());
textView.setText("" + cal.result);
textView.setTextColor(Color.RED);
draw = new Draw(this);
linearLayout.addView(textView);
linearLayout.addView(draw);
}
}
Cal.java
public class Cal extends View {
Cal(Context context){
super(context);
}
public double result;
double parameter = (Math.pow(40,2)) * 3.14;
public void cal(){
result = Math.sqrt(parameter);
}
}
Draw.java
public class Draw extends View {
Paint paint = new Paint();
Draw(Context context) {
super(context);
}
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLUE);
canvas.drawCircle(120,120,40,paint);
}
}
The public void cal(){ result = Math.sqrt(parameter);
} is invoked by cal.cal();
in MainActivity But I do not know, How is invoked the
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLUE);
canvas.drawCircle(120,120,40,paint);
}
in MainActivity.java ?? in MainActivity.java, we have only the draw = new Draw(this);
for calling the Draw(Context context) {super(context);}
in Draw.java
Your Draw class extends the android class View.
You are adding a view to your layout in the onCreateMethod():
linearLayout.addView(draw);
Android will call the onDraw() method in the draw object when the view is rendering.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments