I want to draw something similar to this shape on android canvas:
I think the best way is to define this shape as the intersection of 2 circles and a line. What's the proper way to create this with android canvas?
UPDATE
I am now using PorterDuff as smith324 suggested:
*Note: code slightly simplified for clarity.
darkPaint = new Paint();
darkPaint.setColor(Color.rgb(50, 50, 50));
lightPaint = new Paint();
lightPaint.setColor(Color.rgb(200, 200, 200));
atopPaint= new Paint(GameDrawingPanel.darkPaint.getColor());
atopPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP));
atopPaint.setAntiAlias(true);
atopPaint.setStrokeWidth(2);
_boardCanvas.drawCircle(x, y, radius, darkPaint);
_boardCanvas.drawCircle(x, y, radius_small, lightPaint);
_boardCanvas.drawRect(0,0,height,width,atopPaint);
I forget exactly which modes would be of use here (XOR should work), but essentially you need to use a PorterDuff transfer mode to achieve this. Try drawing the concentric circles atop one another, then a dividing rectangle on one side.
http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html
http://www.svgopen.org/2005/papers/abstractsvgopen/
Related
Imagine that I have a rectangle image. How could I create a style like the next one?
I mean, cropping the image into a circle, add the border, the shadow and the gross /shine effect. Until now, I only have tried this snippet code to crop the image: Cropping circular area from bitmap in Android but just that. I have no idea how to do the remaining components in Android.
An easy way to achieve this effect is to use Canvas.drawCircle() and a BitmapShader:
BitmapShader s = new BitmapShader(myPhoto, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint p = new Paint();
p.setShader(s);
myCanvas.drawCircle(centerX, centerY, radius, p);
To do the shadow, simply call Paint.setShadowLayer() on the paint (this will only work if you draw the effect into an offscreen Bitmap or if your View uses a software layer – set by calling View.setLayerType() –).
The border can be drawn by drawing another circle on top, using the Paint.Style.STROKE style (that you can set by calling Paint.setStyle()).
Finally you can draw the gloss by drawing a circle, oval or Path on top of your very first circle. You'll need to use a LinearGradient shader on your paint and you'll also need to clip the gloss. You can do this in two ways:
If you are drawing the entire effect into a Bitmap, which is what I would recommend, simply set the paint's Xfermode to a new PorterDuffXfermode(PorterDuff.Mode.SRC_IN).
If you are drawing the effect directly on screen you can simply use Canvas.clipPath() to set a circular clip. Note that this will work with hardware acceleration only as of Android 4.3.
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 goal is to create a composite shape and add an emboss effect. I can successfully create the shape as illustrated below.
woodPaint = new Paint();
woodPaint.setAntiAlias(true);
woodBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.wood);
woodShader = new BitmapShader(woodBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
woodPaint.setShader(woodShader);
...
#Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(handleX, radius, radius, woodPaint);
canvas.drawRoundRect(baseRectF, 25, 25, woodPaint);
super.onDraw(canvas);
}
Image:
Then I add an EmbossMaskFilter
paintEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.1f, 8f, 5f);
woodPaint.setMaskFilter(paintEmboss);
Image:
As you can see the emboss mask is applied to the two shapes separately. How can I compose the shapes together and apply the emboss to the entire object? I've tried setting the xfer mode to some flavor off porter duff but this doesn't effect the fact that the emboss mask is applied to each shape separately.
Thanks for any help!
Edit:
As illustrated by Orabîg, you must draw one path with the paint that you've set an emboss filter on. NOTE: the method setMaskFilter() is one of the handful of methods that don't work when hardware acceleration is turned on. I resolved an issue I was having with a phone running jelly bean by disabling hardware acceleration for the activity. You can disable hardware acceleration at any level you choose:
ApplicationActivityWindowView
Cheers!
Well, you need only one emboss effect, so you should ony draw one shape.
So you should use the Canvas.drawPath() method.
You'll just have to define a Path object, with the following methods :
Start by defining 3 RectF objects, which will be the bounding boxes of the left-most round (imagine the circle behind it), the right-most one, and the sliding one :
You'll maybe have to do some additional maths to determine the correct angles to use for box2 (they depend on the respective size of the circle and the whole rectangle)
Good luck !
I have this image that comes back from an API, which represents the users avatar:
However, my graphics department has designed the app to mask the image to make it look like this at runtime (to match our existing design of sharp edges, etc):
Notice the small edge cutout on the bottom left?
I'd love to be able to create a custom ImageView that handled this for me. Unfortunately I'm not sure how to go about doing that. How can I create the bottom image in a custom ImageView. Is this possible? Do I mask it? If so, how?
Thanks!
Using Path and xfer modes to draw on canvas can do the trick. Check this answer how to draw pic to Closed curve area
I think the easiest way to do is to use 2 ImageViews, one with the photo and other above it with a mask for the photo, in your case it would be all transparent except the bottom left to create the cutout with the background color.
You may be able to use android.graphics.Path to draw the complex shape you want. I found this very helpful for a simple custom View, but it seems like you can do a lot with it:
http://developer.android.com/reference/android/graphics/Path.html
Simple code sample for a shaded rectangle:
private Path mRectanglePath;
...
// draw the path
mRectanglePath = new Path();
mRectanglePath.addRect(mLeft, mTop, mRight, mBottom, Path.Direction.CW);
// draw the fill
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setAlpha(64);
canvas.drawPath(mRectanglePath, paint);
How do you create an animated dashed or dotted border of an arbitrary shape in Android? In XML (preferred) or programmatically.
See picture below for an example.
Have you seen the PathEffects API demo?
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/PathEffects.html
It produces precisely an animated line and you can just adjust the path to the edge of your view to create a border. For example:
Define a path by your view parameters / arbitrary shape:
Path path = new Path();
path.addRect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), Path.Direction.CW);
Then create a dashed PathEffect with:
PathEffect pe = new DashPathEffect(new float[] {10, 5, 5, 5}, phase);
Then set the associate it with a Paint object and draw:
mPaint.setPathEffect(pe);
canvas.drawPath(path, mPaint);
EDIT: The animated effect comes from continuously changing the phase and redrawing. In the API demo it calls invalidate() in the onDraw() method (which triggers onDraw()...)
XML... I guess not possible. But you can use a custom view or a SurfaceView and handle the drawing by yourself. Have fun with that :)
Could you use some form of two 9patch images as a background frame around the image file you want to present, one in each of two layouts. The images would differ in terms of the placement of the dashed elements. Interchange the views rapidly (might need a delay) and you might get the effect you want. Don't really know how effective that would be though in terms of being able to let the user continue using the app and chewing battery...