简体   繁体   English

如何在Android自定义画布中使圆可绘制?

[英]How to make circle drawable in Android custom canvas?

I want to make in my app possibilty to draw circles by user. 我想让我的应用程序可以按用户绘制圆圈。 The drawing is quite simple - user just press on canvas somewhere and then predefined circle 绘图非常简单-用户只需在某处按画布,然后按预定的圆圈即可

The difficult part here is to draw it with some drawable (picture) as a fill. 这里最困难的部分是用一些可绘制的(图片)填充来绘制它。 It is quite simple when it is about rectangle. 关于矩形,这非常简单。 Then you just need to write: 然后,您只需要编写:

   Drawable drawable = getResources().getDrawable(R.drawable.my_background_picture);
   drawable.setBounds(myRectangle);
   drawable.draw(myCanvas);

Everything is done on onDraw() method of my custom view. 一切都在我的自定义视图的onDraw()方法上完成。

Unfortunatelly there isn't such simple method to make it with circle. 不幸的是,没有这么简单的方法可以制作成圆形。 The one that I've found is slight modification from Vogella's tutorial : 我发现的是Vogella的教程中的一些修改:

    InputStream resource = getResources().openRawResource(R.drawable.sand);
    Bitmap bitmap = BitmapFactory.decodeStream(resource);

    BitmapShader shader;
    shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    myCanvas.drawRoundRect(myRectangle, 120, 120, paint);

At first sight it looks ok, but it's not. 乍一看看起来还可以,但事实并非如此。 This commands make just something like frame on a picture below, so you move hollow circle on a picture and that's all. 该命令的效果类似于下面图片中的框架,因此您只需在图片上移动空心圆即可。 Not as with rectangle where you actually move rectangular bitmap. 与实际移动矩形位图的矩形不同。

So, my question is - Is there a way to make circle drawable that can be also moved/resized? 所以,我的问题是- 有没有办法使可绘制的圆也可以移动/调整大小?

Why make a drawable? 为什么要画一个画? You can easily draw a circle via the canvas.drawCircle command. 您可以通过canvas.drawCircle命令轻松绘制一个圆。 You can also easily make one via a Path object. 您也可以通过Path对象轻松创建一个。

Edit: 编辑:

If you need a drawable, try making a ShapeDrawable based off an OvalShape. 如果需要可绘制对象,请尝试基于OvalShape制作ShapeDrawable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM