move an object follow a user-defined path on Android - android

What algorithm or technique should I use to make an object follow the path that the user's drawing on the screen?

The below example creates a PATH for displying TEXT on Circle Path:
// create a path
Path circle = new Path();
circle.addCircle(centerX, centerY, radius, Direction.CW);
// set the color and font size
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(30);
paint.setAntiAlias(true);
// draw the text along the circle
canvas.drawTextOnPath(QUOTE, circle, 0, 30, paint);
You can refer full Example Here
And For Animation, There are mainly 4 Types of Animations comes up with Android SDK:
AlphaAnimation – transparency changes
RotateAnimation – rotations
ScaleAnimation – growing or shrinking
TranslateAnimation – position changes
For CREATING ANIMATION SEQUENCES, refer Example Here.
For Different Types of Animations Example such as Frame Animation (As in Flash), List Animation,etc. You can refer Animations Types EXample here .
Enjoy!!

I was just working on this in the last couple weeks for a game--how I did it was by getting each point from coordinates given by the touch event (when the user is drawing it to the screen) and then adding that to a list. I turned that list into a path to draw to the screen, and then just had the object update its location based on the list in the onDraw method for each frame.

Related

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.

How to compose multiple shapes and add emboss?

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 !

Android Circle Menu Like Catch Notes

I try to do circle menu like in this app.
In "expanded" mode i draw this component like follows:
<RelativeLayout android:id="#+id/bigCircle">
<!--color full borders-->
<my.custom.component android:id="#+id/middleCircle">
<!--circle for buttons-->
<RelativeLayout android:id="#+id/smallCircle">
<!--minus button-->
</RelativeLayout>
</my.custom.component>
</RelativeLayout>
In onDraw method of my.custom.component i divide circle on 8 parts by using android.graphics.Path with android.graphics.Paint and some math.
Visually i have exactly as shown in the screenshot. But when i press on part of circle, i need redraw this part in another color to show user what something going on.
How i can redraw part of component's canvas cutting off from another part of canvas by android.graphics.Path for example. In another word i know what redraw canvas i should do in onDraw method, i know that i can show some bitmap from drawables painted in photoshop and have some "multiscreen trouble", i know how i can determine part which user pressed. But i don't know how i can select part of canvas and redraw it.
Developer of Catch here. If I'm understanding your issue, you're having trouble understanding how to specifically draw the highlight/selection indicator on a section of your circular menu.
While there are plenty of different ways one could implement it, what you're leaning towards (using android.graphics.Path) is how we did it. In the view hierarchy of our capture button, there's an element that serves as the canvas on which the selection highlight color (if there is an active selection) is drawn.
If you had a similar custom View in your layout, you could duplicate this behavior like so. First, you'll need the Path that defines the selection for a particular circle segment. Using Path.addArc(RectF, float, float) we can get the pizza-slice-shaped path we need:
private Path getPathForSegment(float startAngle, float sweep) {
Point center = new Point(getWidth() / 2, getHeight() / 2);
RectF rect = new RectF(0f, 0f, getWidth(), getHeight());
Path selection = new Path();
selection.addArc(rect, startAngle, sweep);
selection.lineTo(center.x, center.y);
selection.close();
return selection;
}
The getWidth() and getHeight() above are for the enclosing custom view object, so they define the bounding box that contains the circle on which the selection is drawn.
Then, in your custom view's onDraw(Canvas), if your code has determined a selection should be drawn for a segment:
#Override
protected void onDraw(Canvas canvas) {
// Assume one has the rest of these simple helper functions defined
if (shouldDrawSelection()) {
float startAngle = getStartAngleOfSelectedSegment();
float sweep = getSweepAngle();
Paint paint = getPaintStyleForSelectedSegment();
Path path = getPathForSegment(startAngle, sweep);
canvas.drawPath(path, paint);
}
// ...
super.onDraw(canvas);
}
In the other areas of your code that are tracking touches, just call invalidate() on the custom view so that it will redraw (or not) the selection path based on changes in input or state.
Remember that it's good practice to avoid newing objects in onDraw(), so most of these building blocks (Paths, Paints, etc.) can be constructed ahead of time (or once, on first occurrence) and reused.
Hope this is close to what you were asking!

Magnifying part of the canvas when touched

I have a custom view that extends SurfaceView. I am drawing an image on the canvas. When the user touches a point on the canvas, i want to show a magnified view of the area around the touched co-ordinates. And preferably, as the finger moves around, i would like to update the magnified view's content accordingly.
I was wondering if the android platform supports such a functionality natively. If not, can one of you point me to a good example that can get me started or share ideas on how to implement it. I don't do much 2D or 3D graphics of this sort and am still trying to understand the Canvas and Matrix Classes to see what i can be use.
I searched the forum for a similar question but could not find any. So, pls don't flag me for asking a question that already exists.
And no - i am not using OpenGL-ES or any such 3rd party library (yet).
Thanks All.
To zoom the image you're drawing on the canvas:
Create a BitmapShader (using the bitmap of the image you're drawing), a Matrix and a Paint:
shader = new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP);
matrix = new Matrix();
shaderPaint = new Paint();
shaderPaint.setShader(shader);
On a touch event record the touch position (e.g. in a PointF):
zoomPos.x = event.getX();
zoomPos.y = event.getY();
...and set up the shader's matrix (I do this on each touch, there's probably a better way):
matrix.reset();
matrix.postScale(2f, 2f);
matrix.postTranslate(-zoomPos.x, -zoomPos.y);
shader.setLocalMatrix(matrix);
Then in the drawing code, draw a circle using the shader Paint.
canvas.drawCircle(zoomPos.x, zoomPos.y, size_of_the_circle, shaderPaint);
Edit
The two lines:
matrix.postScale(2f, 2f);
matrix.postTranslate(-zoomPos.x, -zoomPos.y);
Can be replaced with one:
matrix.postScale(2f, 2f, zoomPos.x, zoomPos.y);
This allows the scale factor to be changed without breaking the offset.

Animated Dashed Border in Android

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...

Categories

Resources