如何通过Android的画布上绘制圆?画布、Android

2023-09-12 22:32:21 作者:”╰+gǒが油メo

我想在画布上绘制圆。这是我的code:

I want to draw circle by canvas. Here is my code:

[MyActivity.java]:

[MyActivity.java]:

public class MyActivity extends Activity 
{
 public void onCreate(Bundle savedInstanceState) 
   {
      ...
      setContentView(new View(this,w,h));
   }

}

[View.java]:

[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);
    }
}

所以,我刚才黑屏无圈。 为什么它不工作?如何解决这个问题?

So i have just black screen without circle. Why it does not work? How to fix it?

推荐答案

您可以覆盖你的视图的OnDraw的方法,绘制圆。

You can override the onDraw method of your view and draw the circle.

protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);

 canvas.drawCircle(x, y, radius, paint);

}

有关绘制自定义视图一个更好的参考检查出的官方Android文档。

For a better reference on drawing custom views check out the official Android documentation.

http://developer.android.com/training/custom-views/custom-drawing.html

祝您好运!

 
精彩推荐
图片推荐