What is it about?
Create a new Java class that should extend from View class. Override the onDraw() method. In this method, you can use Canvas class to draw the different shapes.
File name: MyView.java
public class MyView extends View
{
public MyView(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas)
{
// TODO Auto-generated method stub
super.onDraw(canvas);
int radius;
radius = 50;
Paint paint = newPaint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(150,200, radius, paint);
canvas.drawRoundRect(newRectF(20,20,100,100), 20, 20, paint);
canvas.rotate(-45);
canvas.drawText("TutorialRide", 40, 180, paint);
canvas.restore();
}
}
File name: MainActivity.java
Note: You have to pass the object of subclass that extends from View class in setContentView() method as given below. In our case the name of the subclass is MyView.
Public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}