MainActivity.java
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CanvasView view = new CanvasView(this);
setContentView(view); }
}CanvasView.java
public class CanvasView extends View {
private ShapeDrawable mDrawable;//这里绘制的是椭圆 public CanvasView(Context context) {
super(context);
int x = 10;
int y = 10;
int width = 300;
int height = 50; mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
} protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
mDrawable.draw(canvas);
}}