How to draw paths of decreasing width in android canvas? - android

I wanted to draw path effect in android canvas like, the path should give the actual feel like a brush. The path should not start or end with linear width. While starting, width should be increasing linearly up to given width and while ending the reverse, width should be decreasing. It would be helpful if we can do the same for the opacity of the line. The path effect should be as following.
Is there any direct way to do it in android? Please let me know.
Thank you.

Maybe a better way to go about accomplishing this would be to render a (filled) polygon instead? Create a new path using moveTo lineTo lineTo, etc to define your brush stroke. Once completed and closed, fill the triangle with the appropriate command. Android also supports passing your vertices and colors in an array using the drawVertices method. Good luck~!

Related

How to create custom view with rounded border in Android

Hi,
I'm trying to understand how to make custom view (with green background) on Android like represented on attached image. Please suggest, should I use Path for this or something else?
A good starting point would be http://tips.androidhive.info/2013/09/android-layout-rounded-corner-border/ . Then you can play with the attribute values to get the desired background. Hope this helps.
You have two good options for accomplishing this.
Bitmap
Create the desired image in Photoshop, Gimp or something similar. Export it as a png file then import it as a bitmap resource into your Android application.
This is by far the simpler and faster of the two methods, but here are two significant problems. First, you should supply bitmaps for each screen density bucket. Second, each bitmap resource you add increases the install size of your app. This can get out of hand quickly.
Path
As you mentioned in your question, you can draw the desired shape directly onto a canvas using a Path. More specifically, you'll use the cubicTo() method to create bezier curves.
I recommend using your image editor's "path" tool to learn how the control points affect the curve. Once you've drawn the shape in the image editor, write down the coordinates for every point on your path as well as the corresponding control points. Then do a little math to convert the points to ratios of the image's overall width and height.
You can implement the shape in your app as a View or as a Drawable. In either case, you'll be drawing to a Canvas. The important step will be to set up the path by multiplying the ratios with the size of your drawing area. After you draw the fancy edge, be sure to draw the straight edges via lineTo() and to close the shape with close().

Android: Is it possible to create such type of circle?

I google it but don't know about this which was possible or not to draw circle like this in android using canvas. If possible then how what the way to do this. below is the image.
I don't know any way to create circle in piece format using canvas
I believe that this is possible using Canvas.drawArc with the usecenter parameter set true.
take a look at the android docs
I can think of 3 options:
Create it as a bitmap which you store in your Drawables or Assets then draw it to the canvas. You can scale it as needed when you load it or by scaling the canvas.
You could draw a circle, then draw 3 lines in a different colour to create the "Y" shape, adjusting the thickness of the lines as you need (or use rectangles)
Use an algorithm to calculate the segment then use drawPath to create the segments individually.
[EDIT] Doh! Elemental's solution is much better...
Create 3 different Bipmap
hdip
ldip
mdip
instead of searching or XML

Android - Puzzle Piece

I'm trying to create a jigsaw puzzle app for Android. I am fairly far into the coding, and I am kind of stuck with one issue.
I need a way to change a Bitmap into a bunch of puzzle pieces. My current code simply cuts the image into rectangles, and it works pretty well, but now I need a way to create more complex piece shapes.
I had a couple of ideas:
Use a separate bitmap file that contains only black and white pixels, and use that to cut up the picture. I thought this was a pretty good plan, until I went to code it. I really had no idea how to do it.
Use a Path object to create the border. This would probably work, except I'm not sure how to keep track of the sides so that the pieces connect with each other.
Any ideas? I'm open to any suggestions.
You can use Path and/or Region to set a clip for your Canvas when drawing a Bitmap.
Take a look at this example. Here are some ways of clipping your drawing to any shape.
You could try making squares or rectangles fitted inside complex figures that can still be pieced toguether, when there's a match, the full rectangle covers the space. Imagine it like a 9 patch, when two sides match, you show the border rectangle.
This is not a explicit solution but I wonder if it would be possible to use bezier curves or paths to create lines along x and y , in conjunction with a parameter(fed with random value) to control the amount of deviation from a straight line and how much in a given distance ie; pixels/ per inch - this would be to create tongues on the pieces. Then use Region to extract the resulting shape at a given side of an intersection. Have the shape object get its center xy coordinate at instantiation and make it so that piece cannot be set if its current coordinate does not match the one it had when it was created.

Android: How do you fill a bitmap with color by volume?

I have a bitmap image that is of irregular shape that I fill with a certain color to simulate a "meter" of sorts. My current method uses a ColorMatrixColorFilter to replace color on the original bimtap, up to a percentage by bitmap height. What I'd really like to do is replace the color by volume.
You could draw a filled bitmap overtop of the unfilled one, but use a clipping path based on the meter's level. If that can be done with a rectangular clip, it will be quite straightforward with Canvas.clipRect(float,float,float,float). Otherwise, you may have to specify a more general path and use Canvas.clipPath(Path). However, the clipPath is not anti-aliased, so it can look a bit lousy.
Another option is to draw the overlay directly with a custom Path, possibly using SVG path primitives for convenience. See this project: http://code.google.com/p/svg-android/ and specifically this method: http://svg-android.googlecode.com/svn/trunk/svgandroid/docs/com/larvalabs/svgandroid/SVGParser.html#parsePath(java.lang.String).

Different behavior in Path fill for clockwise and counterclockwise paths

It seems the path FILL_AND_STROKE behaves differently depending on which way I draw the path.
How can I make these fill completely? First one is clockwise, second is counterclockwise.
image
Easy hack to make it work: instead of FILL_AND_STROKE, draw it twice using FILL first and then STROKE:
canvas.drawPath(mPath, paintFill);
canvas.drawPath(mPath, paintStroke);
Thanks for the hint. If there is just a single path then FILL_AND_STROKE works fine, but when adding additional paths to it then the direction matters.
I've had the same problem and managed to solve it by using
Direction.CCW
when adding an additional shape to the path.
Also this is combined with
path.setFillType(FillType.EVEN_ODD)
so that one can create a "hole" in another path.

Categories

Resources