将按钮绘制成圆形
class MyButton extends Button { // 这个是继承必须写的,带参构造器,所以要重写 public MyButton(Context context) { super(context); // TODO Auto-generated constructor stub } // 在按钮中画图 @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); // 画笔 Paint paint = new Paint(); paint.setColor(Color.GREEN); canvas.drawCircle(30, 30, 30, paint); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyButton my = new MyButton(this); LinearLayout lay = new LinearLayout(this); lay.addView(my); setContentView(lay); }
使用画图写游戏的建议继承View
class GameView extends View { private Paint paint = null; public GameView(Context context) { super(context); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); paint.setColor(Color.RED); paint.setStrokeWidth(5);//画笔粗 canvas.drawLine(0, 0, 100, 111, paint); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GameView my = new GameView(this); LinearLayout lay = new LinearLayout(this); lay.addView(my); setContentView(lay); }