【问题标题】:how to draw a circle on Canvas using java for android [duplicate]如何使用java for android在Canvas上绘制一个圆圈[重复]
【发布时间】:2013-12-19 03:35:56
【问题描述】:

我想在我的 Android 应用程序的画布上画一个圆圈。我进行了很多搜索,并意识到如果我需要一种可以随时更新的动态绘画形式,我需要使用画布而不是 imageView。

感谢任何帮助

这是我到目前为止编写的代码,但它不会在 android 设备屏幕上绘制任何内容:

    private void createBitMap() {
    Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);  //creates bmp
    bitMap = bitMap.copy(bitMap.getConfig(), true);     //lets bmp to be mutable
    Canvas canvas = new Canvas(bitMap);                 //draw a canvas in defined bmp

    Paint paint = new Paint();                          //define paint and paint color
    paint.setColor(Color.RED);
    paint.setStyle(Style.FILL_AND_STROKE);
    //paint.setAntiAlias(true);

    canvas.drawCircle(50, 50, 10, paint);
}

【问题讨论】:

标签: java android android-canvas draw geometry


【解决方案1】:

像这样更新你的 createBitMap 方法

private void createBitMap() {
        // Create a mutable bitmap
        Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);

        bitMap = bitMap.copy(bitMap.getConfig(), true);
        // Construct a canvas with the specified bitmap to draw into
        Canvas canvas = new Canvas(bitMap);
        // Create a new paint with default settings.
        Paint paint = new Paint();
        // smooths out the edges of what is being drawn
        paint.setAntiAlias(true);
        // set color
        paint.setColor(Color.BLACK);
        // set style
        paint.setStyle(Paint.Style.STROKE);
        // set stroke
        paint.setStrokeWidth(4.5f);
        // draw circle with radius 30
        canvas.drawCircle(50, 50, 30, paint);
        // set on ImageView or any other view 
        imageView.setImageBitmap(bitMap);

    }

【讨论】:

  • 非常感谢。有用。您向我展示了我必须使用 ImageView 并在其中设置图像。提前致谢
  • 对我有用。谢谢你!
【解决方案2】:

试试这个

创建 ImageView 并使用image.setImageBitmap(bitMap); 使位图可见。

公共类 MainActivity 扩展 Activity { ImageView 图像;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image=(ImageView)findViewById(R.id.imageView1);
        createBitMap();
    }

    private void createBitMap() {
        Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);  //creates bmp
        bitMap = bitMap.copy(bitMap.getConfig(), true);     //lets bmp to be mutable
        Canvas canvas = new Canvas(bitMap);                 //draw a canvas in defined bmp

        Paint paint = new Paint();
        // smooths
       paint.setAntiAlias(true);
        paint.setColor(Color.RED);
       paint.setStyle(Paint.Style.STROKE); 
        paint.setStrokeWidth(4.5f);
        // opacity
        //p.setAlpha(0x80); //
        canvas.drawCircle(50, 50, 30, paint);
        image.setImageBitmap(bitMap);
    }

【讨论】:

  • 感谢您的回复。我只想更改自己的代码,以便绘制圆圈。改变整体意味着从头开始阅读所有内容:)
  • 现在我根据您的需要更改了答案。
猜你喜欢
  • 2014-09-25
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多