我想按画布画圆。这是我的代码:
[MyActivity.java]:
public class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { ... setContentView(new View(this,w,h)); } }
[View.java]:
public class View extends SurfaceView { public View(Context context, int w, int h) { super(context); Canvas grid = new Canvas(Bitmap.createBitmap(h,w, Bitmap.Config.ARGB_8888)); grid. drawColor(Color.WHITE); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); grid.drawCircle(w/2, h/2 , w/2, paint); } }
所以我只有黑屏,没有圆圈。为什么不起作用?如何解决?
您可以覆盖视图的onDraw方法并绘制圆。
protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(x, y, radius, paint); }
有关绘制自定义视图的更好参考,请查看官方的Android文档。
http://developer.android.com/training/custom-views/custom- drawing.html