如何使用java for android在Canvas上画一个圆圈

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20673018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:30:07  来源:igfitidea点击:

how to draw a circle on Canvas using java for android

javaandroidandroid-canvasdrawgeometry

提问by Hossein Dolatabadi

I want to draw a circle on a canvas in my android app. I searched a lot and realized if I need a dynamic form of painting which can be updated time by time I need to use canvas instead of imageView.

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

any help is appreciated

任何帮助表示赞赏

this is the code that I wrote so far but it will not draw anything on the android device screen:

这是我到目前为止编写的代码,但它不会在 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);
}

采纳答案by Jitender Dev

Update your createBitMapmethod like this

像这样更新你的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);

    }

回答by Nambi

try this

尝试这个

create ImageView and use image.setImageBitmap(bitMap);to make a bitmap to visible.

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

public class MainActivity extends Activity { ImageView image;

公共类 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);
    }