简体   繁体   中英

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.

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 :

    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. You can also easily make one via a Path object.

Edit:

If you need a drawable, try making a ShapeDrawable based off an OvalShape.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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