c# Animation, using TranslateTransform and DoubleAnimation -
i have simple map , square move point d, through b , c. i've declared method animate:
public void animate(double[] firstpoint, double[] secondpoint, image img) { double x1 = firstpoint[0]; double x2 = secondpoint[0]; double y1 = firstpoint[1]; double y2 = secondpoint[1]; translatetransform trans = new translatetransform(); img.rendertransform = trans; doubleanimation anim1 = new doubleanimation(y1, y2, timespan.fromseconds(1)); doubleanimation anim2 = new doubleanimation(x1, x2, timespan.fromseconds(1)); trans.beginanimation(translatetransform.yproperty, anim1); trans.beginanimation(translatetransform.xproperty, anim2); }
the main problem when use method this:
obj.animate(obj.a, obj.b, car); obj.animate(obj.b, obj.c, car); obj.animate(obj.c, obj.d, car);
...there's animation shown point c d. when added messagebox.show
animate
method, displayed animation properly.
i feel might not understand concept behind using classes animate objects. thoughts?
you can use code sample understanding, how work multiple animations.
this full code mainwindow.xaml.cs.
public partial class mainwindow : window { private const string cartransform = "cartransform"; private image _car; public mainwindow() { initializecomponent(); loaded += mainwindow_loaded; } void mainwindow_loaded(object sender, routedeventargs e) { loaded -= mainwindow_loaded; // create , add car image layoutroot grid _car = new image(); _car.source = new bitmapimage(new uri("/car-icon-hi.png", urikind.relative)); _car.width = 64; _car.height = 64; _car.horizontalalignment = system.windows.horizontalalignment.left; _car.verticalalignment = system.windows.verticalalignment.top; _car.margin = new thickness(5); _car.rendertransform = new translatetransform(); layoutroot.children.add(_car); } private doubleanimation createanimationfor(double from, double to, timespan? begintime, string targetname, dependencyproperty propertypath) { doubleanimation da = new doubleanimation(); da.from = from; da.to = to; da.duration = new duration(timespan.frommilliseconds(1000)); if (begintime != null) da.begintime = begintime; storyboard.settargetname(da, targetname); storyboard.settargetproperty(da, new propertypath(propertypath)); return da; } private void startanimationclick(object sender, routedeventargs e) { translatetransform _trans = _car.rendertransform translatetransform; this.registername(cartransform, _trans); // register name translatetransform instance, action needed working storyboard multiple animations storyboard sb = new storyboard(); // b sb.children.add(createanimationfor(0, 100, null, cartransform, translatetransform.xproperty)); sb.children.add(createanimationfor(0, 0, null, cartransform, translatetransform.yproperty)); // b c sb.children.add(createanimationfor(100, 100, timespan.fromseconds(1), cartransform, translatetransform.xproperty)); sb.children.add(createanimationfor(0, 100, timespan.fromseconds(1), cartransform, translatetransform.yproperty)); // c d sb.children.add(createanimationfor(100, 300, timespan.fromseconds(2), cartransform, translatetransform.xproperty)); sb.children.add(createanimationfor(100, 250, timespan.fromseconds(2), cartransform, translatetransform.yproperty)); sb.begin(this); } }
result of code:
Comments
Post a Comment