Moving between two points in canvas - android

I have 2 points(x1,y1) and (x2,y2). I need to move the image from (x1,y1) to (x2,y2). Please let me know if there is any methods in android SDK to achieve this.

If you are doing a custom view that handles it's on onDraw method you could:
Create a path object:
Move to position 1 using the moveTo method
line to position 2 using the line2 method
Create a path measure object.
User the path measure object to animate to the position.(Here is an example of that--note you will have to do it a little differently as the code doesn't show View.onDraw & View.invalidate being called).
Alternatively if you aren't handling your own on draw, you might be able to simply use a TranslateAnimation.

Looks like all you need is an animation.
Search this site for Animation Drawable and you'll get tons of examples :)

Related

Using a Specific Animation in ViewPager

today i was looking for a good way to create animation on ViewPager and i found a good library in github named SparkleMotion . i saw a animation in their demo that a paper playn fly and leave a trace behind . Here's how its look
https://github.com/IFTTT/SparkleMotion/blob/master/art/sparklemotion.gif
in fact i only need the trace animation behind the plane and not other animation , can anyone tell me what is this animation and how to use it ?
the github link of the project is :
https://github.com/IFTTT/SparkleMotion
The source code in that SparkleMotion project you will be most interested in is the PathAnimation class in the main project and the PaperPlaneView class in the demo project.
The animation is centered on the android.graphics.Path class. With the Path class, you can string together straight lines, arcs, rectangles, bezier curves, etc. to form a continuous line.
To render a path, the Canvas class has a drawPath() method. It also has a related method setPathEffect() where you could for example set a DashPathEffect to get the dashed lines.
What makes the animation work is a class called PathMeasure. A PathMeasure object takes a Path in its constructor. This allows you to do measuring operations on the Path. By calling PathMeasure.getSegment() you can get a portion of the path, so by getting longer and longer segments, you can animate the path in motion. And the demo uses the PathMeasure.getPosTan() method to get the position and tangent of the end of the segment. These values are used to position and rotate the paper airplane graphic to look like it is traveling on the path.

Android spinning wheel (pizza)

I am trying to create a circle wheel divided in a fixed number of sections. Each section should be clickable.
How I approach this? Should I make an image and set as a background or is there a way to draw all parts in java?
Shortly:
Create .png with pizza.
Create new widget, extending View.
Override onDraw() and draw it to canvas with some rotation. Optionally you can draw lines with java if it's margeritta not a pepperoni.
If necessary - change rotation call invalidate() to redraw view
Add onTouch() listener, get position ot tap, calculate what sector was touched.
One Solution would be to draw different paths for each piece on a Canvas (path.lineTp and path.addCircle - think so). Then you can add ClickListeners to the paths and they will be clickable....

Motion to draw numbers on android

im new to this android things. And i have to develop an application that can help an autism to learn numbers. I have a few ideas and I've been trying to learn and implement the code. But it's failed. The question is how can i apply the motion code or sprite to draw a numbers or letter? For example like this, i wanna make the penguin move through the line and draw a number nine.
There is example from mybringback.com which is the image move to draw a rectangle. How can i implement it to draw a number? Im sorry if i asking too much, i just trying to get some ideas.
I think that you should first build an utility program, in order to create the "path vector".
What I mean by path vector is simply a vector of Points (where a point has x value, and y value). And your utility should let you draw whatever you want, with a simple pen. You should draw on surface and store points when mouse is down, and ignore points when mouse is up.
Then, in the main program, you will just have to read at the path of your number/letter.
I've tried to implement something like this for the Sugar OLPC platform, without serializing path into files : I was able to draw, and to view the animation. And I used the process I've just described you.
Hope it can help you.
P.S : I used the word mouse, but you guessed that I talk about finger ...
There are various ways to achieve animation effects. One approach that is quite versatile involves creating a custom View or SurfaceView in which you Override the onDraw method. Various tutorials can be found on this; the official Android discussion of it is here:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#on-view
Your implementation will look something like this:
// Find elapsed time since previous draw
// Compute new position of drawable/bitmap along figure
// Draw bitmap in appropriate location
// Add line to buffer containing segments of curve drawn so far
// Render all segments in curve buffer
// Take some action to call for the rendering of the next frame (this may be done in another thread)
Obviously a simplification. For a very simplistic tutorial, see here:
http://www.techrepublic.com/blog/software-engineer/bouncing-a-ball-on-androids-canvas/1733/
Note that different implementations of this technique will require different levels of involvement by you; for example, if you use a SurfaceView, you are in charge of calling the onDraw method, whereas subclassing the normal View lets you leave Android in charge of redrawing (at the expense of limiting your ability to draw on a different thread). In this respect, Google remains your friend =]

How to draw a shape in Android as one of other layout objects?

I've been working on this for a while but can't find anything that exactly addresses my question (at least not something easy to understand).
I have a main layout XML file where I define various layout objects like a Button or a TextView (and I know I can add SurfaceView, View, and view and other things too). I want to draw a shape (in my case it's an arc) in just one of these objects so it doesn't take up the whole screen and so I can position it relative to other things.
(In my case it will ultimately re-draw the arc kind of like a circle with a gap in a different position every time I call a method depending on a value I pass to the method, but that's separate from my basic question.)
I know the answer will have something to do with a canvas, an onDraw method, maybe Paint, probably a view. I have been able to draw a circle from a custom View object by setting the main java file's layout as that View (as opposed to R.layouts.main), but that takes up the whole screen, and I'm unsure how I might be able to have that dynamically draw with modifications.
A really clear explanation or better yet an actual example would just be awesome.
As i see it u need to draw a specific shape on widget and not on complete screen. Try using layer List.
you can refer this link for sample Link

Custom Overlay MKMapOverlay iOS port of Android method

I have an android app I have written, that basically has a custom overlay, in this overlay I have overwritten the DRAW method to draw something on a canvas and at the end of my DRAW override, I call the super DRAW passing in the canvas I have painted.. the result is, every time the map is modified in anyway my custom overlay is redrawn as expected.
Now, I am trying to accomplish the same thing on iPhone and am getting a little confused.
Basically I need something drawn in the top right corner of the map every time a redraw occurs of the map. The drawin will change change with every update as well so can't simply be put a view over the map and pass touches through etc.
So, I guess the question is, what is the euivalent of the DRAW method in iOS.. how would I roughly accomplish this same thing? There are MKOverlayView in the API, but there seem to be some significant differences.. So, how do I put something say at 10x10 over the map, whos size is variable and make sure every time the map is moved, scaled or otherwised interacted with this object is redrawn at location 10x10 on the screen.
The docs for MKMapView state that you should not subclass MKMapView, so you can't / shouldn't override the drawRect method. You could add a UIView, though, and override drawRect to do your custom drawing.

Categories

Resources