Android image animation - moving on a trajectory -
i'd have animated image in activity: white circle moving on trajectory (black line).

what best way it?
- translate animation
- frameanimation
- canvas
the implementation be:
- the white circle small imageview transparent background. placed on top of imageview (black curve).
- frameanimation: each position of circle there separate png-image of whole screen, frame of animation.
- use drawcircle(), , restorebackgroundimage() each movement of white dot.
so far tried frameanimation, outofmemoryerror 10 frames.
the following code implements canvas way. efficient , no oom. need change trajectory path object.
public class testview extends view { private path path; private paint pathpaint; private paint dotpaint; private long begintime; private long duration = 3000; private float dotradius = 3; private pathmeasure pm; public testview(context context) { super(context); init(); } public testview(context context, attributeset attrs) { super(context, attrs); init(); } public testview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } private void init() { path = new path(); path.moveto(0, 100); path.lineto(100, 200); path.lineto(200, 50); //todo: put path here pm = new pathmeasure(path, false); pathpaint = new paint(); pathpaint.setargb(255, 0, 0, 0); pathpaint.setstrokewidth(2); pathpaint.setstyle(paint.style.stroke); dotpaint = new paint(); dotpaint.setargb(255, 255, 255, 255); dotpaint.setstyle(paint.style.fill); begintime = 0; } @override protected void ondraw(canvas canvas) { canvas.drawargb(0, 0, 0, 0); canvas.drawpath(path, pathpaint); long currenttime = system.currenttimemillis(); float currentdistance; if (begintime == 0) { begintime = currenttime; currentdistance = 0; } else if (begintime > 0 && currenttime - begintime < duration) { currentdistance = (float) (currenttime - begintime) / (float) duration * pm.getlength(); } else { begintime = -1; return; } float pos[] = new float[2]; pm.getpostan(currentdistance, pos, null); canvas.drawcircle(pos[0], pos[1], dotradius, dotpaint); invalidate(); } }
Comments
Post a Comment