如何在Android中通过canvas绘制圆圈? [英] How to draw circle by canvas in Android?

查看:206
本文介绍了如何在Android中通过canvas绘制圆圈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用画布画圆.这是我的代码:

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-视图/custom-drawing.html

这篇关于如何在Android中通过canvas绘制圆圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆