Clutter学习(十二):Clutter的动态效果ClutterAnimation

  根据我们的学习资料Using AminationUsing Alpha FunctionsExmples ,以及clutter的联机文档。这个课题的学习本应在ClutterAnimation之前的。由于学习文档是0.9的文档,而我也相信有个问题可能是Clutter的小小的bug,我们在上面碰到一些问题,不过现在终于解决了。另外今天发现clutter已经公布的1.0.2的版本,也进行了更新。

  我们不再完全给出例子,因此一样是照例的建立stage,下里面放入actor(rect或者textrue)。这些在前面以及已经给了多次,我们主要集中在ClutterAnimation上。他可以提供在指定的时间内完成指定动作,功能上和ClutterBehaviour没有太大的区别。

ClutterAnimation * clutter_actor_animate (ClutterActor *actor,
                                                                            gulong             mode,
                                                                            guint                duration,
                                                                            const gchar  *first_property_name,
                                                                            ...);

  这是一个不定长参数的的例子。第一个参数表示参与动作的actor,第二个参数表示动作速率变化的方式,例如CLUTTER_LINEAR,第三个表示动作的持续时间。后面的参数表示何种动作,我们在下面具体学习。

ClutterAnimation * animation = clutter_actor_animate(rect, CLUTTER_EASE_OUT_BOUNCE,500,  "x", 100.0, "y", 100.0,NULL);
g_signal_connect_swapped(animation,"completed",G_CALLBACK(clutter_actor_hide),rect);

  在clutter_actor_animate的最后一个参数为NULL,表示后面没有其他参数。按CLUTTER_EASE_OUT_BOUNCE(在上一学习中详细说明)的方式,移动到(x,y)=(100.0,100.0)的位置上,同时在动作结束时触发clutter_actor_hide(rect),即隐藏rect的处理,这个触发函数是系统自带的。这两个语句可以合并为一个语句,如下

clutter_actor_animate(rect,CLUTTER_LINEAR,500, "x",100.0,"y",100.0, "signal-swapped::completed ",clutter_actor_hide,rect ,NULL);

  请注意singal-swapped::completed的书写方式,通过singal-swapped来表示触发的条件。

clutter_actor_animate(rect, CLUTTER_EASE_OUT_BOUNCE,4000,"opacity",0,NULL);

  上面是在4秒内逐渐变暗的例子。

clutter_actor_animate(rect, CLUTTER_EASE_OUT_BOUNCE,4000, "x",100.0,"y",100.0, "opacity",80,NULL);

  上面是同时处理多个动作的例子,同时进行移位和变暗的例子。

clutter_actor_animate(rect, CLUTTER_EASE_OUT_BOUNCE,4000, "width",200.0,"height",100.0, NULL);

  这是在指定时间内渐变actor大小的例子。这个例子折腾我很久。一开始,我使用了clutter_actor_animate(rect,  CLUTTER_EASE_OUT_BOUNCE, 4000,  "width", 200 ,"height", 100,  "opacity",90,NULL); 在参数传递中,用float来代替int是危险的,但是反过来只是格式变换的问题,本应行得通,但是在clutter1.0中,后一种写法是有问题,运行的时候产生core dumped,我想这是clutter的一个bug。由于非定长参数不会在编译过程中报告参数匹配的问题,我们需要非常注意到这点。这个问题查了我很久……,也是我为什么先完成ClutterBehaviour学习的原因。

clutter_actor_animate (rect, CLUTTER_EASE_IN_SINE, 4000, "rotation-angle-z", 360.0, NULL);

  根据Z轴进行旋转。我们将这个稍为复杂一点,我们学习多几个函数:

ClutterVertex * center = clutter_vertex_new(30.0,200.0,0);
ClutterAnimation * animation = clutter_actor_animate (rect,
                 CLUTTER_EASE_IN_SINE, 6000, "rotation-angle-z", 360.0,
                 "fixed::rotation-center-z", center , NULL);
clutter_animation_set_mode (animation,CLUTTER_EASE_OUT_BOUNCE);
clutter_animation_set_loop (animation,TRUE);
g_signal_connect (animation ,"completed", G_CALLBACK(on_animation_completed),rect);

  在这个旋转动作中,我们设定了x和y值(基本旋转的参数之一),使用了作为fixed::rotation-center-z参数。这些参数我们能填写什么值,可以去查看,clutter_actor_animate的不定长参数的格式为,参数名,参数值。我们可以在联机文档中查看acotr的属性(properties),基本是ClutterActor的属性,例如:file:///usr/share/gtk-doc/html/clutter/ClutterActor.html#ClutterActor.properties 。如果是textrue,我们还可以查看ClutterTextrue的属性。例如width为gfloat。fixed::表示上个动作收到这个参数的关联。如果链接文档做得更好,应该将有什么的fixed参数一起列出来。在这个例子中,我们使用g_signal_connect来加入完成工作的触发函数,如果我们设置为循环,则完成一个循环并不会触发回调函数。我不太知晓g_signal_connect和g_singnal_connect_swapped的区别,因此到网上查阅了一下。见http://oss.org.cn/ossdocs/gnu/gtk2-tut/x223.html 。他们作用是相同的,但是他们回调函数的参数是不一样。对于g_signal_connect,回调函数的格式为:void callback_func( GtkWidget *widget, gpointer callback_data );而g_signal_connet_swapped的回调函数的格式为:void callback_func( GtkObject *object );

ClutterVertex * center = clutter_vertex_new(30.0,200.0,300.0);
ClutterAnimation * animation = clutter_actor_animate (rect, CLUTTER_EASE_IN_BOUNCE, 4000, rotation-angle-z", 360.0,  "fixed::rotation-center-z", center, NULL);
ClutterTimeline * timeline = clutter_timeline_new(10000);
clutter_timeline_set_loop(timeline,TRUE);
clutter_animation_set_timeline (animation,timeline);
clutter_timeline_start(timeline);

  我可以用timeline来更好地控制动态动作发生的时间。将某个动态动作和timeline相关联,仅在timeline有效时间内发生动作,并周期设置同时间轴的设定。上里,有效的时间周期为10秒,而不是4秒。

ClutterTimeline * timeline = clutter_timeline_new(10000);
ClutterAlpha* alpha = clutter_alpha_new_with_func (timeline, &on_alpha, NULL, NULL);

ClutterAnimation * animation = clutter_actor_animate (rect, CLUTTER_EASE_IN_BOUNCE, 4000, "rotation-angle-z", 360.0, NULL);
clutter_animation_set_alpha(animation,alpha);
clutter_timeline_start(timeline);

……

 

gdouble on_alpha(ClutterAlpha * alpha,gpointer data){
        ClutterTimeline * timeline = clutter_alpha_get_timeline(alpha);
        return clutter_timeline_get_progress(timeline);
}

  我们还可以将动态动作animation和alpha想关联。其中on_alpha为触发函数,定义timeline的运动状态,返回只从0~1之间。我们可以根据时间轴的位置,给出相关的值,自行定义运动模式。一般简单地我们可以直接将alpha和某种运动方式捆绑就可以了例如clutter_alpha_new_full (timeline, CLUTTER_LINEAR)。

相关链接:
我的Clutter相关博客

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值